Stepper motors and Arduino

Back in the 80s steppers where commonly used in most consumer products requiring precise control of moving pats. That’s why old printers, typewriters, faxes are a great source of still usable stepper motors waiting for somebody to give them a use.

Controlling Stepper motors with arduino have been always a good way of having things precisely positioned at all times. Their way of working is pretty simple, you send electronic pulses and the stepper motor moves one step degree at a time (hence the name “steppers”). So if we know how many steps we can do in 380 degrees we will know how many degree we move in one step. Then we can keep track of the position or angle of our stepper motor.

Nowadays consumer electronics use better ways of controlling position with the use of encoders. Encoders allow the use of standard DC motors ,which have better speed and acceleration times, instead of the standard stepper motors. The bad thing about encoder systems is that they are processor hungry, they need constant feedback from micro controllers in order to know in which position they are. This makes it hard for most of us (and specially Arduino and other low speed micro controllers) as this technology requires great skills of programming. And that’s the main reason for stepper motors being widespread within the open source community instead of encoders.


My first encounter with stepper motors was as usual more difficult than expected, so I gathered as much information as I could and started testing and tinkering around.

When controlling stepper motors from Arduino it’s very easy to move it back and forth and play around with it. Arduino has a basic library that will do the job for any basic stepper motor you have around, but it is not going to be of much help if you use step/direction drivers like Easydriver. Instead, you can follow this nice tutorial on how to get started with an easy to use stepper driver.

The main think about steppers is that you have to switch pins at a steady rate in order to keep the motor running at a certain velocity. This means that doing anything else in the code  needs to respect this rate in order to keep the motor running smoothly. And not only that, if we just make the motor run at a high speed from the beginning we will have to accelerate the motor before reaching the speed, otherwise the motor will just “miss” all steps. At hi speeds we don’t have enough momentum to move superate the initial inertia, so the motor just keeps still. Sending steady speed right away might work well at low speeds but at higher speeds even if we do a  variation in speed we juts might loose some steps (if not all) and lose track completely of the position of the motor.

The proper way of moving stepper motors fast and accurate is to accelerate them. from zero to our desired speed (or max physical speed)


This process of acceleration has to be accurately calculated in order to be as smooth as possible. To do that we can make use of complex formulas for calculate the exact timing. Which almost for sure will lead us to use timers to achieve this timings. The use of timers is already for advanced users and still not so user friendly in the Arduino IDE.

Instead I used a different approach. What I did is create my own formulas using delays and simple calculations for creating an approximation of acceleration.

I have a little a draft to illustrate what was my initial idea. My draft is cryptical, dirty and almost completely useless without further explanation, I know, but still…


The fact that is easier for us to do steady speeds rather than accelerations is the basic idea in which I based my function.

What I did is to increment the steady speeds in a way it would appear to accelerate. Each increment is calculated on the fly by my own function. So I tell the function which is my start speed, my max speed, and the time to get there. Also I tell the functions how many steps do I want a create to achieve acceleration so it will calculate (with delays) the time that needs to wait until the next velocity step.

All this is calculated prior of the movement of the motor. All the delays and steps and the rest of variables are calculated and stored in a matrix for further usage. The next intelligible piece of code illustrates the process I’m describing:




Looking closer to the code and also to the picture draft above, we can see that every thing is more complex than it seems. This is because we have to add to the equation an extra layer of complexity. Microstepping!

Yes! Microstepping is what you don’t want a hear when creating a function to move a motor. Because you will want to use it!! And that complicates everything.

There is plenty information on microstepping on the web, but to keep it simple here is like the gears on a car. You have a short gear with alot of torke but low velocity, and then you have the higher gear with alot of velocity but less torke.

So we have to create all this increments of velocity for each gear and be sure that when we change the gear we can’t have a too big difference of velocity or we would lose control over the stepper motor or loose some steps.

After all previous calculations we did in the last piece of code now we apply the delays to the motor. The code looks like this:



Yes! also intelligible.

The only thing I can say is that the major drawback when using this method is that because of the use of delays as a temporal measurement we are blocking the code from doing other operations. Since my main goal was to achieve accuracy on steps and not speed this procedure worked very well in my setup.

The result is brilliant. I added some tracking of steps so in an upper function I can tell the code how many steps do I want to move and it will accelerate, keep max speed, decelerate and be right where I want it to be.

hope you enjoyed. And please feel free to ask me if you are interested or just want to know about the project.

Next a post with videos of the new prototype!

2 Responses to “ Stepper motors and Arduino ”

  1. hello can you give me the source i’m intersted

Leave a Reply