By continuing to use this site, you agree to our use of cookies. Find out more
Forum sponsored by:
Forum sponsored by Allendale Jan 24th

Stepper Motor Controls

All Topics | Latest Posts

Search for:  in Thread Title in  
SillyOldDuffer17/02/2019 11:33:11
5767 forum posts
1230 photos
Posted by ChrisH on 16/02/2019 14:19:24:

... Jason - I await MEW 279 dropping through the letter box!

Dave (S O D) - ... Look forward to future posts from you on this, hopefully???

Next is wiring the black box to a controller and power.

To power the motor, I used a cheap ebay/amazon PSU as sold to drive LEDs. Not ideal for stepper motors but easy to get:


These are available in various voltages and power outputs. Get one with watts to suit your motor. I used a 12v PSU on my table; although it's fine a 24V supply would have been better.

Next wiring an ArduinoUno as a controller. It's job is to ENAble the driver, set DIRection, and PULse the motor, ie tell it to step. Though this can be done with conventional electronics, it's more flexible with a small computer like an Arduino. But swapping conventional electronics for a computer means learning how to program the computer.

Wiring the black box driver to an Arduino is simple, this shows signal HIGH = ON, LOW = OFF:


ENA-, DIR-, PUL- all connect to GND on the Arduino, leaving + signals doing the controlling. ENA+. DIR+, and PUL+ are each wired to an Arduino input-output pin. There are 20 of these, some with special characteristics. Pins A0 to A5 (red box below) have extra analogue capabilities, but this circuit uses them in bog-standard ON/OFF mode.


Connections to a UNO are made by plugging breadboard jumper leads or pin headers into the sockets. Headers need soldering. If soldering is preferred go Nano, they're cheaper.

When an Arduino is plugged into a PC, it's powered by the PC. However, you might not want to power it that way in a workshop. Instead either plug the Arduino into a USB telephone charger, or buy a wallwart in the range 7 to 12vdc to plug into the black socket top left. 7.5Vdc preferred.


Next post: programming.


Edited By SillyOldDuffer on 17/02/2019 11:36:27

OuBallie17/02/2019 12:33:23
1149 forum posts
661 photos

It was interesting seeing the results when trying all combinations of settings on the 'driver' unit when I first ventured into stepper motor control.

Geoff - Still learning.

SillyOldDuffer17/02/2019 19:17:09
5767 forum posts
1230 photos

Before leaping into programming, it might help to explain what an Arduino is. It's a family of small electronics boards containing a microcontroller plus the wherewithal to: power it; manage a serial connection to a Personal Computer; and allow a program to be installed. It comes with hardware and software designed to simplify using the microcontroller. There are a number of different Arduino models, the Uno is the work horse of the family and easiest to experiment with.

A microcontroller is a tiny computer designed to interface with other electronics. They don't do general purpose computing like a PC. Typically a microcontroller is kept so simple it doesn't have an operating system and it only runs one program dedicated to a single purpose. The program starts when the machine is turned on, and it stops only when switched off. They are idea for monitoring sensors, including internal timers, and controlling the electronics used to manage lights, motors, valves, servos, heaters or whatever. The inputs might include a simple keyboard, or just a few switches, and the outputs might include a simple display, or perhaps just a few LEDS.

A microcontroller program runs in a permanent loop, allowing forever the logic: 'if an INPUT then calculate an OUTPUT'. Within the loop it's possible to read inputs, detect events, store information, perform arithmetic and logical operations, make comparisons, and write outputs. The program is a list of instructions followed one after the other, except there are instructions that can, optionally, cause the computer to jump to elsewhere in a sequence.

Consider a problem like a traffic light controller at a 4-way junction. There are specific rules to be followed rigorously: only one signal may be green at a time, never two or more together! To keep traffic moving each road must get a green signal in turn, and the green signal must stay on for a useful length of time. To warn slow-reacting drivers lights go from red to green via a short amber, and - because they need time to stop - lights change from green to red after a longer amber period. A simple system might give each road an equal amount of green time, but a real world controller would more likely adjust go time depending on the proportional number of cars waiting on each road. To do this, the microcontroller has to keep count of cars detected by sensors, and recalculate green time as necessary. In the event of a fault, the system should restart automatically, starting with all roads at red. All this is getting a little complicated, especially if the system has to allow for filter lanes, or another nearby signal controlled junction, or has a manual override, or is only activated during heavy traffic, or reports traffic density to a control centre, or is sometimes managed remotely, or detects headlamps at night so cars aren't stopped unnecessarily.

Quite a design challenge, but most of this can be done expensively with relays and mechanical timers. Later, semi-conductors and integrated circuits provided cheaper and more reliable equivalent circuitry. Today, a £5 microcontroller can do all this and more, though it has to be said that designing fail-safe control systems like traffic signals or fly-by-wire aircraft is not simple!

The extra power of microcontrollers can be used to improve all manner of mechanical systems. For example, a car engine's timing, fuel quantity, and fuel mix can all be changed depending on air and engine temperatures, knock detection, engine load, humidity, air pressure and exhaust content. At the press of a button the driver can optimise for fuel economy or performance - rather than needing a mechanic to retune the engine in a garage, a microcontroller can do it in motion.

Microcontrollers can be programmed to manage almost any electro-mechanical machine, and programs can be upgraded to fix bugs or add new features without having to rewire anything.

The microcontroller used by the Arduino Uno isn't particularly powerful, but it has 20 pins that can used for input or output and enough oomph to manage a wide range of basic and not so basic control requirements.

In the case of a stepper motor, turning the motor is easy. The pin connected to ENA+ is declared an OUTPUT, and set HIGH. This tells the black box to obey pulses. Then the pin connected to DIR+ is declared an OUTPUT and set to HIGH or LOW depending on the required direction. The controller adjusts it's output to turn the motor either clockwise or anticlockwise. Finally the pin connected to PUL+ is declared an OUTPUT and sent a sequence of HIGH LOW HIGH LOW HIGH LOW signals. Each HIGH  steps the motor once. With this hard coded in the program the motor would run in one direction forever.

Mostly though the user wants to stop and start the motor on command, and decide which direction it turns, and determine how many steps the motor will take, and the rpm. And perhaps automatically stopping the motor if it hits an end stop. Achieving this involves declaring other pins as INPUTS, connecting switches, and then writing code to detect if a switch is ON or OFF.

Hope that makes sense!



Edited By SillyOldDuffer on 17/02/2019 19:23:21

ChrisH17/02/2019 22:17:25
857 forum posts
29 photos

Dave - all making sense, thanks you, a light is beginning to show at the end of the tunnel.!

Thanks you also for taking the time to write such lengthy explanations, and add the photos and links, I know none of it is a quick "five minute" job; your labours are most appreciated.


David George 118/02/2019 08:21:19
1221 forum posts
415 photos

Hi on my z axis I have controlled it with the motor driver two switches and a pulse generator with adjustable speed. One switch to to start wired to enable one switch wired to direction and the pulse generator wired to pulse. I switch on set direction and turn pulse until required speed is set very simple and no programming is required, only my head is there any thing wrong with this setup.


John Haine18/02/2019 08:50:39
3081 forum posts
162 photos

Absolutely nothing, a good approach. But is you use a processor you can get more flexibility, for example being able to repeat a given move at the touch of a button.

SillyOldDuffer18/02/2019 09:51:55
5767 forum posts
1230 photos
Posted by David George 1 on 18/02/2019 08:21:19:

Hi on my z axis I have controlled it with the motor driver two switches and a pulse generator with adjustable speed. One switch to to start wired to enable one switch wired to direction and the pulse generator wired to pulse. I switch on set direction and turn pulse until required speed is set very simple and no programming is required, only my head is there any thing wrong with this setup.


Nothing wrong with that at all. I'd even argue that the simplest solution that does the job is the best!

The only downside is the major redesign needed if more control of the z-axis is required. John gives one example, moving the head automatically to a given height is another. As is, David's pulse generator gives his mill a motorised head (and jolly useful that is!), but he still has to put it at the right height himself. A micro-controller could get close to a wanted position simply by counting steps, and excellent accuracy is achievable by having it read a DRO. But it may not be worth the extra trouble.

In my workshop, doing what I do, there's little advantage in fitting machines right left and centre with steppers and clever controllers. The only exception so far is cutting gears on a rotary table. The manual set-up for stepping a rotary table is cumbersome, the maths difficult, and I'm not good at keeping count while cranking round the index plates, even assuming I've fitted the right one! An automated rotary table is much easier to use and it doesn't make stupid mistakes!


Edited By SillyOldDuffer on 18/02/2019 09:54:41

SillyOldDuffer18/02/2019 13:12:14
5767 forum posts
1230 photos

More on Arduinos...

Computers work with numbers stored in base 2, binary. All modern machines work with 8-bit numbers called bytes. An 8 bit computer processes single bytes, a 32-bit computer processes 4 bytes at a time, and a 64-bit computer works on blocks of 8-bytes. But under the bonnet TRUE, FALSE, integers, characters, floating point, inputs, outputs, instructions, flags, dates, times, internet addresses and everything else are all represented by bytes, either singly or in groups. The only difference between types of data is the way they are encoded, and the way they are manipulated by the programmer.

Working with data at hardware level is outrageously difficult. As a result huge effort has been put into developing tools allowing ordinary humans to cope. Software tools allow problems to be expressed in human-friendly ways, then translated them into a form understood by a computer, and then loaded into the computer. Even though much of computing is about hiding detail and complexity, there is still a lot to learn. The Arduino is designed to get people started relatively painlessly and the software used to program it is free. It runs on Windows, MAC, or Linux. Also, if you don't want to download and install more software on your computer a Web based version is also available.

All Arduino programs start with this default template:


Bottom right shows an Arduino Uno plugged in to my computer has been recognised.

Believe it or not the empty template is a valid program. Pressing the ✓button compiles the instructions in the template (ie translates) into code that can be downloaded into an Arduino, and which works. It creates two functions: setup() is called once when the Uno is powered up; then loop() is called afresh every time it finishes, forever.



The lower pane (in black and white) shows no errors, and tells me how much space my program will consume on the Arduino, which being a tiny computer makes it necessary to keep an eye on this. (Note: normally the Arduino IDE is set to minimise detail in the lower pane, but it can be set to tell all, which is occasionally useful when debugging.

Pressing the right-arrow button downloads the compiled program into the Arduino where it is installed and starts running. Success or failure messages appear in the lower pane again:


At this point I have an Arduino Uno busy executing a program that does absolutely nothing! Next post will put instructions into setup() and loop() capable of driving a stepper motor driver in the most basic way.



Edited By SillyOldDuffer on 18/02/2019 13:14:51

dcosta18/02/2019 15:55:44
484 forum posts
206 photos

Hello ChrisH!
Hello Brian!

I have been away from home and just today I was able to prepare myself to begin the work of drawing the links between the Arduino tb6560 driver and the power supplies.
Thankfully I read the news in the forum first.

Dave's very well-done work (SillyOldDuffer) far surpasses everything I could do. What's more, it uses a driver (tb6600) which seems to me to be more complete and sturdier than what I use (tb6560).
I'm really tempted to get a tb6600 driver and follow Dave's instructions for assembly.
If, after all, I can be of service to you, please let me know.

Dias Costa

SillyOldDuffer18/02/2019 17:01:58
5767 forum posts
1230 photos
Posted by dcosta on 18/02/2019 15:55:44:


If, after all, I can be of service to you, please let me know.

Dias Costa

Too kind Dias! Please shout if anything isn't clear. I'm aiming for a 'get you started' tutorial and am happy to explain more if anyone gets stuck.

Next post coming shortly...


Martin Connelly18/02/2019 17:42:43
1368 forum posts
159 photos

The main reason for using the microprocessor with a shaper (as I see it) is to synchronised the movement to the shaper stroke. I imagine there will need to be a sensor to detect when the cutting stroke is complete and only move the workpiece on the return stroke. It may be possible to use a simple interupt on the enable signal of the simple system using a pulse generator but the variable number of steps per stroke that could result may marr the finish.

Martin C

SillyOldDuffer18/02/2019 17:43:09
5767 forum posts
1230 photos

Before plunging into code, it's worth setting up the Arduino so we can see if it's working. Not necessary to connect it to a driver, motor and power supply at this stage, building them can be left until later. All we need do is test the signals output on pins A0, A1 and A2 are as planned.

The outputs can be tested with a multimeter, or an oscilloscope, but LEDs and a few resistors on a solderless breadboard will do the same job. These provide lines of connector strips into which components can be plugged to make temporary circuits.


All that's needed to test the Arduino program (or 'Sketch' in their jargon), is three small LEDs, with resistors arranged to drop 5V output by the Arduino to roughly1.5V on the LED. Any value between 220 ohms and 1000ohms should do the job. Boards and resistors available from ebay / amazon etc. Don't mess about sourcing three resistors - buy a selection box. Other values may be useful later. Ditto LEDs, get a small selection.

Plugged into the board and connected to the Arduino:


Not very clear in the photo but the Black jumper goes to a socket on the Uno marked GND, and the Yellow jumper goes to A0.

The pink LED connected to A0 (ie ENA+) is lit. This was achieved with these instructions:


Lines starting // are comments. Not used by the Arduino but helpful reminders for the programmer.

I could have used A0 in the raw, but instead "#define ENABLE_PIN A0" creates a meaningful alias. Not necessary In a small obvious program but a useful trick for complicated code, especially if you wrote it 5 years ago and can't remember how it works. It's easier to remember what ENABLE_PIN is than what A0 is connected to.

In setup() the program starts with a pin_mode() command that declares ENABLE_PIN (that is A0) to be an OUTPUT to be used later. This happens in the next statement: digitalWrite() is set HIGH, and 5V appears on the socket marked A0. It causes the LED to light, and it would enable a stepper motor controller when one's connected later.

Next, motor direction is set by adding similar instructions for pin A1:


I used #define to create meaningful aliases for CLOCKWISE and ANTICLOCKWISE rather than remember HIGH means clockwise. And if I get these the wrong way round and the motor runs in reverse, the problem is fixed at the #define - I wouldn't have to check every HIGH scattered throughout the code to see if was setting direction or doing something else.

As expected compiling and installing this causes the red LED to come on as well.


Coming next, the instructions needed to output pulses on A2, aka PULSE_PIN, and eventually to be wired to PUL+ on the stepper driver.



Edited By SillyOldDuffer on 18/02/2019 17:48:30

Andy Carruthers19/02/2019 11:46:51
279 forum posts
23 photos

Good work Dave - appreciated!

SillyOldDuffer19/02/2019 13:14:41
5767 forum posts
1230 photos

The program so far just allocates two pins as outputs and switches them on. As this is only one once, it's convenient to put the instructions into setup().

Sending pulses to the motor driver is a repeated operation and it makes sense to put the code into loop(). loop() is called automatically when setup() finishes, and it is called again each time loop() finishes. The instructions in loop() will be applied over and over again until the power is turned off. The logic in loop() typically does something like:

IF something_happened THEN
do this_function()
do another_function()

To send a stream of pulses all that's necessary is to set an output pin to HIGH and follow that with another instruction to set the pin LOW. Each time loop() is run, it sends HIGH LOW to the motor driver.ard_pulsefast.jpg

Checking message pane shows the program has used a trivial amount of resources - there's plenty of room for expansion. (790 bytes used of 32256 available)

Now all three of the test LEDs on the breadboard are lit:


Not obvious in the photo but the Green LED connected to PULSE_PIN is dimmer than the other two. That's because it's ON for half the time and OFF for the remainder. The pulses are far too fast to see individually, but an oscilloscope reveals all:


The oscilloscope shows each pulse lasts 6.87 microseconds, and there are about 145000 pulses per second, each one of which would step the motor once. Assuming the motor, power supply and stepper driver could physically cope with that, a motor set to 200 steps per revolution would spin at 43500 rpm, and the same motor set to 6400 micro-steps would spin at 1359 rpm. In the real world, the electronics in the stepper driver might only be up to 50000 pulses per second, and - especially if accurate high torque stepping is wanted - a stepper motor should be run much slower than that. Voltage is important to the speed at which a stepper motor will turn reliably, as is the time a PSU takes to deliver a current pulse. (A supply designed to deliver 4A continuous might not be so good at 4A bursts.)

For those reasons, the pulse rate has to be slowed down. And because, the motor power supply combination was chosen for convenience rather than selected to a careful design specification, it pays to find the best speed/reliability rate for a particular motor and power supply by experiment. The pulse rate is set by putting a delay between digitalWrite(PULSE_PIN, HIGH); and digitalWrite(PULSE_PIN, LOW);

More on that in the next post.




Edited By SillyOldDuffer on 19/02/2019 13:19:23

Edited By SillyOldDuffer on 19/02/2019 13:22:44

SillyOldDuffer19/02/2019 16:23:10
5767 forum posts
1230 photos

A slower pulse rate is achieved by ordering the Arduino to wait for a short time between HIGH and LOW. Changes inside Red boxes.


To provide flexibility, the delay is stored in a variable. That used, called 'pulseDelay' , is declared as a signed integer, which on the Arduino is ±32768. I set it to 50 and note they are going to be microseconds. Further down the code, the variable is used in a new function called 'delayMicroseconds()'. Given a number, this instruction waits for a time. The time delay could be hard coded into the program, but using a variable makes changing the pulse rate easy. For example to provide speed control.

Functions like digitalWrite(), delayMicroseconds() and many others are provided with the Arduino. They are fully described in the online documentation, with examples. There are also many tutorials available. The Arduino is programmed in 'C' (actually a variant of C++), and most of the standard C and C++ functions are also available, organised into 'libraries'. Functions can be written by the user, and there many user provided libraries as well. With luck someone else will have done most of the hard work for you already. There is a simple built-in stepper library worth looking at and a much more sophisticated library here. It adds features like acceleration which is needed to get the very best out of a motor. Not needed on a rotary table or head lifter, but a CNC application would benefit.

Arduino can also exchange messages with the host PC. These are useful for debugging because they allow your code to report the values it's working with, and you can check they're right. Next example adds a Serial link back to the PC and uses it to show that a switch INPUT has been detected.


Apologies for the first red box - it should be around the declaration below.  My mistake.

Second red box - this code adds the instruction 'Serial.begin( 9600 ). It opens a USB serial connection with the PC running at 9600 bits per second. Opening the Arduino IDE's built in terminal will connect to the Uno and the two can exchange data. There's no obligation to use the connection.

Also added is an unsigned long integer called 'pulseCount' set to zero. On an Arduino Uno, unsigned long can hold an integer between 0 and 4,294,967,296 In the next red box one is added to pulseCount every time a pulse is sent to the stepper driver.

The program also allocates A3, aka DEBUG_PIN, as an INPUT_PULLUP. INPUT_PULLUP makes the pin HIGH by default, and a switch wired to ground the pin can thus be detected. The state of the input is tested with digitalRead(). If the read is equal to LOW, the value of pulseCount is printed to the Serial line, and sent to the PC. Serial.print() doesn't throw a new-line on the remote terminal, Serial.println() does.  Beginner booby trap, '=' is an assignment, the equality test is '=='.  Don't mix them up!

If A3 is connected to ground, a stream of numbers appears on the terminal. Otherwise the program doesn't bother. Note that the time taken to communicate with the PC adds more delay and reduces the pulse rate slightly. Example terminal screen, yes it soon gets up to 2 million:




Edited By SillyOldDuffer on 19/02/2019 16:29:22

donkey19/02/2019 16:43:38
73 forum posts
5 photos

thanks all.I now have to assemble everything and give it a go..


All Topics | Latest Posts

Please login to post a reply.

Magazine Locator

Want the latest issue of Model Engineer or Model Engineers' Workshop? Use our magazine locator links to find your nearest stockist!

Find Model Engineer & Model Engineers' Workshop

Latest Forum Posts
Support Our Partners
Eccentric July 5 2018
Allendale Electronics
Eccentric Engineering
Subscription Offer

Latest "For Sale" Ads
Latest "Wanted" Ads
Get In Touch!

Do you want to contact the Model Engineer and Model Engineers' Workshop team?

You can contact us by phone, mail or email about the magazines including becoming a contributor, submitting reader's letters or making queries about articles. You can also get in touch about this website, advertising or other general issues.

Click THIS LINK for full contact details.

For subscription issues please see THIS LINK.

Digital Back Issues

Social Media online

'Like' us on Facebook
Follow us on Facebook

Follow us on Twitter
 Twitter Logo

Pin us on Pinterest