What is a best practice for doing step and direction to drive a stepper motor?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

I am designing a small injection mold machine that uses 4 Technic servos (34's).  I have never driven such motors before using a PSoC.  After reading the data sheet on the motors all I need to do is give them three digital bits of information (Step, Direction, Enable).  I also read they can be driven using quadrature inputs (A,B,Enable).  I am reaching out to see if maybe there is  a most effective way getting this type of data to these motors.  I was planing on using a CY8CKIT-059 to start my design with.  My question is mostly directed do i use step and direction or quadrature and how is it best implemented in code?

Thanks

Scott

1 Solution

You might be interested in this blog:

PSoC 5 Port Of the Grbl 1.1 CNC Controller     (  Here is the code on GitHub )

There is an option with USB but I have not tested it.
Evgeniy

View solution in original post

0 Likes
23 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Scott,

Either type is implementable on a PSoC.   The CY8CKIT-059 is an excellent kit to prototype your design.

In my opinion, the step/direction might be slightly easier to implement.  Do you have datasheets on the motors you are looking at with the two types?

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

I am using the SDSK 34 size model from Tecknic.  I will attach the manual of the motor i am using.  They are nice motors with built in drivers right on the motor.  In the manual look at pages 151-156

Scott

0 Likes

You might be interested in this blog:

PSoC 5 Port Of the Grbl 1.1 CNC Controller     (  Here is the code on GitHub )

There is an option with USB but I have not tested it.
Evgeniy

0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Scott,

Here's a TopDesign schematic of a step and direction circuit.  It's a starting point.  You might have to tailor it to your application

pastedImage_1.png

Timer_step is used to set the time between step pulses.  65536 = maximum time and 1 = minimum time.  This controls the speed of the motor step.  To change the time, set the Timer_step_WritePeriod(new_value).

The Timer_step tc output is the actual step.  Note: I've set the timer_clock to 900 Hz this will provide about a 1.1ms pulse for every step.

Timer_step_cnt is used to count how many step pulses you want the motor to move before it stops stepping.  This is to allow your SW to control the distance traveled.  Therefore with Timer_step and Timer_step_cnt you can set the speed of movement and the maximum distance allowed in the desired direction.  Note:  If you want the motor to continuously spin, disable Timer_step_cnt by using Timer_step_cnt_Disable().  This will prevent the tc output from resetting the SR_FF which would stop the stepping.

The Outputs are the motor controls (step and direction) and 4 other signals for debugging purposes only.  These last 4 can be eliminated once the debugging is completed.

The CR_step_dir register is used to set direction (CR_step_dir[0]) and to start the stepping operation (CR_step_dir[1]).

The Limit_Switches inputs are set up as pull-ups with switches that pull low when active.  This is intended for SW intervention if a limit switch becomes active.  If you are controlling the motor because it is attached to a x axis table. you might have limit switches when the table has reached it end-stops in either direction.  Another example is an emergency stop button.

Limit_Switches inputs can be directly read for switch status.  I've set up an interrupt to occur (isr_limit_sw) on a falling edge of any switch.  Your application can react quickly to a limit switch going active and stop the motor via SW.

There is more that can be done in HW with this implementation depending on your application.  It is a starting point in the design.

I've attached the project as shown above.  This circuit builds.  I have not tested it.  I have not created any application or test code.

Len

Len
"Engineering is an Art. The Art of Compromise."

Evgeniy and Len,

Thank you for your input, it refreshing to have at-least starting point to review and consider.  I am intrigued by Evgeniy post about the blog posted with the GRBL/PSoC5 port over, I found that very interesting.   From what I have read so far is the gentleman has ported over an arduino type interface to the power of a PSoC5 device, which is very cool to be able to control a CNC type machine with some free GRBL software.  I notice the gentleman who did the port over is using a PSoC resister component and a clock to control step time and I believe a timer to control amount of steps ... guessing.  I have never used a register component in PSoC so I need to do a bit of reading to try and understand how he is controlling the step length and still not sure how he controls amount of steps. In reading though it seems it must be pretty straight forward.

Len thank you for your suggested application i will also review it in detail also. This status and register thing is very new to me and how i use them in coding.  I will do some studying and report back.

0 Likes

Scott,

I'm glad I could be of help.

Here's a quick summary on the use of control and Status registers:

A control register is the component that allows the SW to directly control the digital and analog HW in a Topdesign.

A status register is the component that allows the SW to directly read the HW in a TopDesign.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

At this stage of my project I needed to actually connect up actual  wires to motors.  I also needed a display, control switches, two thermal couples and a 5 volt supply that could handle 80 volt inputs from my power supply used to drive my Tecknic servos.  I found there was really no simple cheap controllers available to do what I wanted to do so I decided I would just make a low cost one using a piggybacked CY8CKIT-059 prototyping board, were i could just snap off the programmer when I am done. In my reading and searching i did not see were anyone has available a GBRL to PSoC5 CNC Ctrl board so I also made this board able to have all the GBRL inputs and outputs for a 3 axis CNC controller.  Below is a shot of the board I made.  It has all the GBRL inputs and outputs, two thermocouples, OLED display, RGB Encoder/Switch, and a on board 10-80 volt input 500mA  5 Volt power supply. I will make a case for it as soon as I get it completed so I can mount to my molder.

pastedImage_0.png

Below is a schematic of this layout.

pastedImage_7.png

I am now starting to do some code and in reading over the post Evgeniy ( about GLRL to PSoc5) shared with me it is not clear to me how this GBRL code controls multiple actions on two or more motors at one time.  In my application I need to control two motors at various times going various directions and speeds at the same time.  Note I do not want to use this GBRL code (I would like to learn from it) I want to write my own code to work with only the CY8CKIT-059 as my controller (no PC).  On the post shared he states the heart of the GBRL is the Stepper interrupt but with my very poor C skills it is not clear to me how he deals with multiple motor moves in a typical CNC move.  Can anyone help me out with how multiple motors are moved effectively and dealt with in a PSoC5.

Thanks

Scott

0 Likes

For testing, I sent circle milling to the PSoC with the help of the Candle.exe program.

All axes have different pitch / mm settings and a common Stepper_Enable_Pin.

I am also not an expert on C, so I can offer you only such a picture

Evgeniycircle.png

0 Likes

Len,

Any thoughts as to how i possibly get multiple movements on two or more axis at the same time?

Scott

0 Likes

Scott,

If you're using Evgeniy's suggested design, I'm not that familiar with it.  If you include your project, I can have a look at it.

Having said that:  If each axis has a separate X, Y (and Z) control circuit with a PWM each, then it's a matter of setting the PWMs for each axis you want to control.  Since they would be separate PWMs, they can be set virtually simultaneously.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

If you send the command $ J = G21G91X1Y5.000Z0.200F500 \ r to the COM port

(X, Y, Z - movement in mm; if 0 - no movement)

You will see the movement. It can be seen that all the axes begin and end the movement at the same time.

I could not explain how the impulses are distributed along the axes.

It seems the used functions system_execute_line (string) and protocol_exec_rt_system ()

and default settings.

XYZ.png

0 Likes

Len,

After getting my errors at least gone for the moment I can now try to get these motors running.  I am going to try the PWM suggestion along with a counters or times to count my steps.  This design direction I can understand in with my current skills,  I also really like trying to do as much as possible in hardware.  Question:1 Is it better to use a timer or a counter to do the step counting?    Question:2 How do I stop the PWM with hardware, do i use the kill input or do i use the enable pin as you showed in your original suggestion using a timer? Thanks again for helping me, this is all very new to me but I am willing to learn.

I need to do some math based on my motor configuration to see quantity of steps i require and the speed.

Scott

FYI this is what i am trying to control with this effortpastedImage_0.png

0 Likes

Scott,

Thanks for sharing your pic of the stepper motor configuration.

Which is better: a Timer or Counter component?  It depends on what features you need.  In essence, they are virtually identical.  On some PSoCs there is a Timer_Counter component.

Q1)  I usually find a Timer simpler to use (doesn't need a count input) but sometimes I need to upgrade to the Counter if I'm needing better input captures.

Q2) I have not tested my logic circuit.  However the intent is that you load the intended speed in Timer_step's period register and the intended distance to travel in Timer_step_cnt's period register.  As you indicated this distance needs to be calculated.

Timer_step is used to set the time between step pulses.  65536 = maximum time and 1 = minimum time.  This controls the speed of the motor step.  To change the time, set the Timer_step_WritePeriod(new_value).

The Timer_step tc output is the actual step.  Note: I've set the timer_clock to 900 Hz this will provide about a 1.1ms pulse for every step.

Timer_step_cnt is used to count how many step pulses you want the motor to move before it stops stepping.  This is to allow your SW to control the distance traveled.  Therefore with Timer_step and Timer_step_cnt you can set the speed of movement and the maximum distance allowed in the desired direction.  Note:  If you want the motor to continuously spin, disable Timer_step_cnt by using Timer_step_cnt_Disable().  This will prevent the tc output from resetting the SR_FF which would stop the stepping at the desired count.

You can incorporate a kill input as a human emergency stop input or as a SW stop based on reading limit switches.

I would suggest disconnecting the belt to the servo at first while you are trying to debug the stepper HW/SW.  This would allow you to enter a distance ("x") and speed ("y") and roughly see that the servo spins for "x" rotations at "y" relative speed.

To test the Limit Switches, you can set the servo to spin for a long time (distance in this case) and see if tripping the right Limit Switch stops the servo via SW.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Len,

I have been trying to get my hardware routine to drive my motors.  I have done all my math and have been able to generate the steps but i can not seem to get the tc pin to ever trip when the number of counts are reached.  I have put a scope on the tc output of the counter and i only ever see it go hi when i reset the part.  I have tried it with both a timer and a counter and never seem to see it trip.  Can you see any thing were i have gone wrong.  From the JR flip flop the r pin dominates the output but with out it going high the PWM never shuts off when it is supposed to.

pastedImage_2.png

In my main routine is wrote some simple code just to test the upper section only.  I tied a few pins on my board to both the tc pin and the s pin of the flip flop.

pastedImage_3.png

I just don't see why i do not get a signal on the terminal count line.  Maybe i am missing something!

0 Likes

Scott,

You attached a new zip file but it virtually has nothing in it.  I'll gladly look at your project but you'll have to include more.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

I don know what my deal is with my bundling ... i need to get this figured out.  On it trying fix this.

0 Likes

Len,

I don't get it, I down loaded the zip file i sent to the community that Creator produced, and it comes in just fine for me.  Is there something i have not set right that you my know of in Creator?

Scott

0 Likes

Here is my project ... but i zipped it in windows right from my directory let me know if you don't get all the necessary files.  I do not know why Creator does not work right for me to zip the proper files. The setup in Top Design is almost working properly, but at the end of my step count which I have set to a low number (5) just see it on my scope the last pulse is chopped off I don't get a full pulse length.  Is there a way to set the counter so it will give me a tc output on the negative edge instead of positive edge.

pastedImage_2.png

0 Likes

Mystery solved I was not able to see the tc pin because I tied it to P3.2 which I believe has a cap  (Ext Vref) tied to this pin on CY8CKIT-059.  I found it by activating the reset option on my Control_Step Register, and when I did so I saw that it reset on terminal count so it had to be working.  I then moved it to another pin and I could see the tc pin activate.  I also had to put the Control_Step Register in pulse mode so the particular pin would pulse instead of run in direct or sync mode.

0 Likes

Scott,

I'm glad you got it to work!  Good troubleshooting skills.

FYI.  I tested my original project I submitted and found some improvements.  Attached is this modified project.

Since you have yours working, you may not gain much insight.  However, I'll place my modified version here for others to use as a starting point.

Here's a scope output of the "working" project.

pastedImage_0.png

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Len,

I have tried a different approach to do the step and direction using a PWM and a Counter in hardware. I have been testing it on a CY8CKIT-059 and it works perfectly every time when i set speed, steps, direction and enable.  Yet when I do two or more setups in a row in code it does not work properly.  I was hoping someone could help me explain what I am doing wrong with this hardware setup or code that i did not do correctly.

pastedImage_0.png

For example below is output from my Saleae logger, it is showing a single set up with running a speed of about 12kHz and 8 steps. It also has enable direction operating correctly.  I can change the steps and the speed and other values to any selection and it works perfectly every time.

pastedImage_1.png

Yet when i try to run two or more motor routines in a row in code for example, as in the case below I have set the code up to run a 12kHz at 8 steps and a 6kHz at 4 steps. But if you note below everything is backwards even my step and direction.  I have tried to fix this but i don't understand how.  I am sure hoping I can get some help to fix this issue.

pastedImage_2.png

I will attach my project so others can review it.

thanks

Scott

0 Likes

I was able to solve my problem with the inverted output, I had the counter in down count mode instead of up count.  I will attach a working copy of my project for others to use and learn from.  This project allows for adjusting motor speed, and direction as well as enabling the motors.  Multiple motors can be  synchronized and moved with this set up.  All that is needed is to add more outputs to the control registers.  I will also attach a spreed sheet that I did that I find helpful to calculate motor feeds and speeds based on screws and gears. Thanks for the help I can now get my project moving with what i have learned so far.  I am sure i will have some issues but at this point I am good to go.

Below is the hardware setup I used and a picture of the output of 3 speeds and changing directions on one servo motor.

pastedImage_1.png

pastedImage_0.png

Scott,

Bravo!  I was glad to assist even in a small way.  I'm glad you were able to troubleshoot the problem yourself.  It's always a good idea to build up those skills.  It not only helps with robust designs but also informs future design better.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes