Here is my stepper motor making those precise 180 degree turns. Well, I think you may be surprised to see that the driver is actually this huge breadboard circuit. Some of you are probably thinking that I’m insane. I mean, why not just use this tiny A4988 stepper driver? It’s a whole lot simpler, more efficient, and will save a lot of space. And you are right, but today’s video is an exercise in engineering. Somebody had to design a stepper motor driver from scratch at one point. So, stick around and I’ll show you how I was able to make a stepper motor driver completely from scratch. Let’s dive in.
Here is the complete final schematic for reference so you can follow along, you can also find it in the description as well.
To even start making this driver, we first have to know how a stepper motor works. From the outside, we can see that there four pins, but we can’t learn much more. That’s why I disassembled this stepper motor so we can get a better look inside. Just take the screws out and pry the top off to reveal the inside. Already, we can see eight coils and this circle inside that almost looks like a big gear because of the teeth. This thing is called the rotor. The pieces that are attached to the coils are called the stator and they have teeth as well. You may have noticed that my screw driver was getting stuck on the rotor. This is because the rotor is actually a permanent magnent. The basic idea is that we will pass current through the stator coils to magnetize the stator. Then the rotor will follow.
Now, there are eight coils, but only two pairs of inputs. Why is that? Well, that’s because all of the coils are grouped into two pairs, and each of those coils in each pair is electrically connected. This will make driving a bit easier since we only have to worry about driving just two coils in total. In order find out which pins correspond to a coil, use your multimeter and look for continuity. In my case, each pair was right next to each other. You can also use the resistance function to find out how many ohms the coil has internally. In my case it was just 1.4 ohms. And to create that magnetic attraction, we can pulse the coil with some current, and you will see a tiny bit of movement. This is cool, but it’s not the continuous movement we are looking for. Instead, we need to start building our driver.
Let me explain the most basic form of driver, which is known as full wave driving. There are four steps. First, we are going to energize the first set of coils and form their magnetic fields. This will align the teeth of the rotor and stator together. Then, we will energize the second set of coils and they will pull the rotor closer to their teeth. Then, we will energize the first set of coils again, but in the opposite direction. This will invert the magnetic field and thus, create an opposite magnetic polarity. This will attract the teeth on the other side of the rotor which are also the opposite polarity. We will do the same for the second set of coils. And congrats, we now have continuous movement. Repeat those four steps and you can move the coil as much as you want. And since it takes four movements to move one tooth, and we have 50 teeth. This results in 200 steps. With each step making up 1.8 degrees.
Ok, so now we’ve got the steps down, we just need to make the circuit that will do this all for us. The best place to start is the bi-directional current flow that we will need to supply to each coil. This is where I will introduce the H-Bridge. It gets its name because the circuit forms the shape of an ‘H’. Anyways, if we activate these opposing mosfets, then we can get current flow in one direction through the load. And if we active the other pair of mosfets, then we will get a current flow in the opposite direction, which is exactly what we need to perform our full wave driving. For the P-Channel MOSFETs I used the IRF5305, and for the N-Channel, I used the IRLZ44N. A nice part about these MOSFETs is that they come with diodes built in, so we don’t have to add any to protect against the inductive load. Since I plan on using a five volt microcontroller later on, I added this driving NPN transistors. This will also let me neatly pair the inputs together with each pair requiring just a single HIGH input. If we use this resistor as a test load. You’ll see that I can reverse the direction of current by pushing either button.
When I attached it to first coil of the motor, you can see the the reverse magnetic fields in action. The first press moves the rotor slightly. The second press undoes the first since it simply reverses the magnetic field. Cool, but we still need to address the second coil. So I built a second H-Bridge exactly like the first. Let’s perform the full wave driving manually at first just to make sure that everything is working. The algorithm goes as follows: foward current in the first coil, forward in the second, reverse in the first, and finally reverse in the second. It’s a little choppy, but let’s fix that with some computer control.
I will use the ATmega8A as my controller for the rest of the project. Since the H-Bridge runs off of 9 volts, I needed to add some regulation to get back down to five volts for the microcontroller, and I used an LM317 for that. Anyways, I did the routine PWM test to make sure that the microcontroller was working properly before I make the rest of the program. And you can already see that the internal RC oscillator isn’t the most stable, and there is some serious time variation in the pulses. So, I figured that this 4MHz crystal would fix that. So I attached it and its supporting capacitors. I also had to change the fuses to activate the crystal, so I programmed what I thought were the correct fuses, but accidentally ended up locking myself out. Luckily, I made this fuse resetter in a previous video, so it was really easy to reset the fuses. Anyways, here are the fuses that I ended up using: lfuse:0xef, hfuse:0xd9.
Here is my implementation of the full wave driving method, and yes it works well and moves very accurately. But there is still more that we can do. We can take this a step further and use full step driving. It has the same end result as full wave, but with the added benefit of increased torque. This is because two coils are always driving the rotor at the same time. And here is the result of the full step mode, which basically looks the exact same as full wave. Anyways, we can start getting into more interesting methods of driving. Which are all under the name of microstepping. The first and easiest is the half step mode. This is simply a combination of full wave and full step, and it will go between each mode, essentially doubling the number of steps from 200 all the way to 400. But, this is as far as we can go using our constant voltage. So far, we’ve been supply the coils a single 9 volts, but a constant current would allow us to get more precision.
Let’s aim for the next level, which would be quarter step mode. Quarter step mode would allow us to reach a resolution of 800 steps per rotation. Like I said, we need to create a constant current on the coil. I figured that I would take some inspiration from my buck converter video and create a PWM signal that would allow us to precisely adjust the coil’s current. The first thing that I did was add a current sensing resistor of one ohm. I chose one ohm since it would be easier read the current since it would be a 1:1 correlation between the voltage drop and the current. After that, I followed it up with an op-amp buffer that multiplied the voltage drop by two. I then followed that with another op-amp that was meant to act as a comparator with the other input being a triangle wave. And at this point, the circuit sort of worked, and I could adjust the current by adjusting the triangle wave’s amplitude. But, I noticed, that there wasn’t much PWM happening, so I took the triangle wave out and replaced it with a variable voltage from a potentiometer. And, well, that worked even better. So yes, I didn’t create a switch mode constant current, but rather a linear one, but it works the same either way, so I consider this a success.
This also made it easier to drive with the microcontroller, since I could simply use a DAC to fine tune the output current. I used the MCP4821 for this purpose. The only thing left to do was digitally control which side of the H-Bridge would get the constant current. For that, I used this transistor setup where the microcontroller could pull the input down to ground. When it wanted to activate the input, it would simply stop driving the transistor and then the op-amp could freely drive the H-Bridge. Now, we just have to write the code for our quarter step mode. While I was researching for this video, I read through the datasheet of this stepper motor driver IC: which was the A4988. If you go to page 17, you will find a table of currents needed for step sequencing. So what I did was calculate what DAC values I would need to use to match what the table recommends for current draw. And so, I made an algorithm that both coils would follow to hopefully allow for quarter stepping.
The first attempt wasn’t the smoothest, and there were a lot of oscillations. So, I used my oscilloscope to find out what was happening. It seemed like the problem was that the current values weren’t the greatest for driving the motor. So after some more adjustment, I was able to create a much better sequence of currents. And you can see that the stepper motor’s movement is much smoother. And as a test of precision, I made it step through 800 times, which should be a full 360 degrees. And yes, it made a perfect circle around. These 800 steps are really very precise, but you can take it even further if you want. On the table, you’ll find a guide for both eigth step and sixteenth step modes if you are adventourous enough to take on that challenge. But, quarter step will be good for me.
Either way, I think we can all agree that using a dedicated stepper motor driver is much easier in the end. If you’ve enjoyed the video up to this point, please consider subscribing so that you can see my future videos. Also check out my buymeacoffee page to support this channel so I can keep making videos. Thanks for watching, have a good one!