Motor Control with the Raspberry Pi

Motor control with the raspberry pi is pretty simple. In addition to the pi, you may use a board such as DC+Stepper Motor HAT board from Adafruit and some stepper motors. (Here’s how stepper motors work.)

The motor control board
Some stepper motors

I have some makeshift adapters to attach the motor tubing to check valves. The motor wiring is attached to the board using the screws. Fair warning, I had one fail on me within 2 months of usage, most likely due to the fact there isn’t anything to anchor the motors to for stability. To the far right you can see the wiring connecting the board to its power source.

Motors attached to the board

As you can see, the board screws right into the pi.

Raspberry Pi and motor control board

This board can control four motors. Here I am using the motors to pump liquids. These motors have a minimum speed of about 70-80 units, making their minimum flow rate (mL/min) fairly high for some applications. For example, for “motor 2”, the minimum flow rate is 10 mL/min.


The library  to control the motors is available on github. Since the raspberry pi has python pre-installed, you will just need to load the library in your program.

from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor

You can find an official guide to control of the pumps here. The code to initialize the pumps may be as follows:

class experiment(Adafruit_DCMotor):
     def __init__(self, addr, interval, days=7, pumptime=1.0, speed=255, mh=mh1,                offset = False):
          Adafruit_DCMotor.__init__(self, mh, addr)
          self.pumptime = float(pumptime) #solution injection time in seconds
           self.interval = float(interval)#time between injections in minutes
           self.days = days #time to run each experiment in days
           self.flag = 0 #indicates whether pump is running
           if offset:
                self.offset = self.interval
               self.offset= 0

Default parameters include pump speed, pump time, time to run experiment, and offset. Offsets can be used to alternate between pumps. You may also define your own parameters, as I defined the address. To call the experiment,

def run(experiments):
     totaltime = max([x.days for x in experiments])*24*60*60
     for exp in experiments:
          exp.flag = 0
          exp.counter1 = 1
          exp.counter2 = 1
          exp.counter3 = 0
          while t < totaltime:
               if (exp.days*24*60*60 == t):

The cleanup function is described below. Note that mh* refers to the board, and each board may have up to 4 motors.

def cleanup():

You can prematurely stop the program in the python console by pressing ctrl-c, but make sure you do it while the motors are off, or they will continue running. (I’m sure there’s some way to make them stop in the console, but I’m not currently aware of it).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s