This will demonstrate exactly how to control Standard Stepper Motors and DC Motors with a Raspberry Pi Single-Board Computer. To do this as fast and seamlessly as possible we will utilise an Adafruit HAT and the Python Programming Language.


Hey gang, Tim here at Core Electronics . Today, we are controlling DC and stepper motors with a Raspberry Pi single board computer. That's all thanks to the Adafruit DC and Stepper Motor Hat. We will build here a fully customizable method of converting electrical energy into angular kinetic energy. Depending on your project needs, either a DC motor or a stepper motor is going to be preferable.

With a single Adafruit DC and Stepper Motor Hat installed onto a Raspberry Pi single board computer, you're going to be able to control and power two stepper motors or four DC motors from your Raspberry Pi. Keep in mind though that this motor HAT is stackable. This means that we could independently control up to 128 DC motors or 64 stepper motors all through a single Raspberry Pi.

On the table for me is everything that you need. Here, I am using a Raspberry Pi 4 Model B 2 Gigabyte. You're also going to need everything to run this Raspberry Pi as a desktop computer, but any Raspberry Pi board can work with this Adafruit Hat. Naturally, you're going to need this Adafruit DC and Stepper Motor Hat. The screw down terminals and headers come unsoldered, which we will need to do if you're going to stack multiple HAT boards on top of each other. Make sure that you solder on stackable headers. These are different than the ones that come with the Adafruit board and this is what they look like.

We want a standard 5 volt DC motor and a standard stepper motor for the motor selection here. We will also need a 5 volt power supply. This will power the HAT and the motors. You may want to use a 12 volt power supply depending on the motors that you have selected. Check the specification data sheet found at the bottom of every single product page. Know that it's okay to run stepper motors at lower than rated voltages. The stepper motor here.

Running a 12 volt motor at five volts means it will carry less current and deliver less torque. However, most tiny 1.5 to 3 volt DC motors will not work in a 5 volt system, and if they do, they will slowly become damaged over time.

We will need a DC barrel jack adapter and some jumper wires to connect all the components together. My setup utilizes two power supplies: one to power the Raspberry Pi and the other to power the motors and HAT. Motors put a lot of noise onto a power supply and can cause power draw stability problems, which would be no good for a power hungry Raspberry Pi single board computer. For high load situations, I recommend placing little heat sinks onto the top of the motor driver's ICs.

In regards to tools, we will need a Phillips head screwdriver to connect the wires to the screw down terminals, and we will be soldering, so we will need everything to do that as well. The very first step is to assemble the screw down terminals and header onto the PCB of the Adafruit HAT. Every single through-hole pin of this hardware will be soldered to enable the electrical connectivity. Note that the screw down terminals slide together, and you can use Bluetack to hold components together when soldering them. Always use flux, as it makes soldering easier and more consistent. Solder the top and bottom corners of the header to confirm correct seating before methodically soldering each connection. This header is relied on as an electrical connection and as a mechanical connection, so take your time with the HAT.

Now that the HAT is completely assembled, we can turn our attention to attaching it to the Raspberry Pi. Raspberry Pi to a power source

To mount the Adafruit Motor HAT on top of the Raspberry Pi, line up the header of the HAT with the GPIO pins of the Raspberry Pi and firmly push the two boards together. Take care that all the pins are correctly lined up and that the HAT is fully seated. You can also use nylon standoffs and screws to secure the two boards even more.

Connect the DC motor to the M1 section, as written on the PCB of the Adafruit HAT. In code, we will refer to and identify the target motors by these port numbers. Now plug your red wire from your DC motor to the furthest left screw down terminal. Then connect the black wire to the screw down terminal directly next to the first.

Let's turn attention to connecting the stepper motor to the HAT. If you have a bipolar stepper motor like I do here, you will only have four wires to connect. We will connect its wires to the screw down terminal blocks labelled M3 and M4. I have identified the two coil pairs. You can identify a coil pair by checking the ends of the two wires for continuity with a multimeter. I'm going to start by connecting this first coil pair (the red and the blue wire) to the M3. Now I will connect the other two wires that make up the other coil pair to the M4 screw down terminals.

Next, we're going to connect a female DC power jack to the positive and negative power input on the bottom right of this HAT. Take note of the positive and negative symbols on the jack and the PCB. The red wire goes from the positive symbol on the jack to the positive symbol on the PCB. The black wire will go from the negative symbol on the jack to the negative symbol on the PCB. There is reverse polarity protection on the HAT, but it's best practice not to put that to the test.

With that complete, we're going to insert a microSD card flash with Raspberry Pi OS and hook up the Raspberry Pi to a power source.

Now with your system rebooted and connected to the internet, open up a new terminal window. Type and enter the following lines directly into the terminal. If prompted, type and enter 'y' to continue installations. You can also copy and paste these terminal commands directly from the forwarding up article links to it down below. This should take less than five minutes and once completed, we have fully set up our Raspberry Pi computer to work with the Adafruit DC Stepper Motor Hat.

From here, we're going to open up a Python interface like Sony IDE. Thonny IDE is a Python interpreter software and you can use whichever is your preference. Then we can copy and paste the following code to run a DC motor from the full written up article. Now let's save the script by pressing that save icon and name it as anything we'd like. I'm going to name it 'DC Motor Control'.

Now keep your hands away from any potentially spinning machinery and let's run the code by pressing the big green button inside Thonny IDE. Wow! Very good both ways. I installed a little fan onto my DC motor just so you can see it spinning much more easily at home. I also 3D printed a little mount for it which you can find at the full written up article.

Now allow me to explain the code. These first three lines here are there just to set up all the necessary information that the script needs by importing certain functionality. It then initializes a variable named 'kit' that we can use to quickly identify and call the I2C connected.

The very next line is going to make the Raspberry Pi power the DC motor with the maximum amount of current available. This means it's going to be running at top speed in the clockwise direction. The Raspberry Pi will then wait four seconds. The motor is going to continue to spin during this period. This process is then repeated with different speeds and directions being utilized. Right here, this point five will spin this DC motor at half of its top speed.

The next line of note is this one right here. This is going to spin our DC motor a third of its maximum speed, but importantly it's going to do it in the opposite direction. That means it's going to spin anti-clockwise and that's based on this minus value right here. The very last line in this script is `kit.motor1.throttle = none`. You can see it right here. This will stop all the current going through the DC motor and leave it free spinning. This is different from the setting above when the throttle is set to zero, as by doing that we still have energy in the system and it's not going to be free spinning.

The code here you can alter to make whatever speed or direction or timing that you desire. Stepper motors are different to DC motors, therefore the script needed to run them correctly is going to be different too. We're going to start a new script in Thonny IDE and copy and paste from the full Retina article the code that you can find right here. With that done, you can save your file as anything you'd like. I'm going to save it as 'Stepper Motor Control'. Then, making sure your hands are away from anything spinning, we can then press the big green run button. I have placed this maker badge on top of the stepper motor so you can see it rotate easier at home. tracks and follows a person or object using a Raspberry Pi and a stepper motor

This script shares a lot of similarities with our previous script, particularly in regards to the first couple of lines. We can see functionality being imported and a kit variable being initialized. Then, some differences: a for loop is created that loops for 500 cycles. Inside this repeating loop, the first line is a command that will rotate the shaft one step. Next is a small time pause, and this then repeats. To our human eyes, this looks like a smooth movement, but really it's just a whole bunch of small steps repeated.

Once it's done that, it's going to start another loop. This next loop will cause the stepper motor to rotate in the opposite direction, as decided by this argument backward right here, using double step movements. There are different ways to step through rotations with a stepper motor; double step movement rotates twice as fast as single step. Once it loops 500 times in this loop, it then jumps to the next for loop.

This third loop will make the stepper motor rotate in the same direction very slowly, as it's now utilizing micro steps. Micro stepping is a way to break down a full step into even smaller segments, allowing even more control over the exact angular position of the stepper motor. You can see the very last line is a release line, and this will de-energize the stepper motor so we can freely move it.

There is so much you can do now with the power to control DC and stepper motors under your belt. Take a look at any consumer 3D printer or CNC machine and you're going to very quickly identify stepper motors. Take a look at any lathe, mill, or motor vehicle and you're going to very quickly find DC motors. An amazing project worth checking out that utilizes stepper motors in their project is the Auto Tracking Pi Camera System by Nigel. This is a platform that automatically tracks and follows a person or object using a Raspberry Pi and a stepper motor.

We are full-time makers and always happy to help, so if you have any questions come pop me a message and we'll figure it out. We have an amazing project where we use a Raspberry Pi high quality camera to take long exposure images of our solar system. The links are down in the description. That's really all for today. So with my murders buzzing, until next time, stay coz.



Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.