Motor driver

Having made the pi wireless and headless, that is not tethered to power socket or keyboard and monitor, what it needs to be mobile is the ability to drive a motor. I had an L293N which allows control of 4 drives at 1Amp at 9v quite easily, that’s enough to power four motors in one direction, or two motors in forwards and back, or one bipolar stepper motor.

The circuit is straightforward, I use a 9v battery to power the motors and (via a voltage regulator and a couple of capacitors to deal with spikes) the L293 — I prefer this to taking power from the pi’s 5v pin, which seemed to be too much for the battery pack the pi was running off. The resistors act to pull the GPIO outputs to ground when they are not set high.

Circuit schematic for two-way control of four motors
Circuit schematic for two-way control of four motors
Physical layout of two-way control of two motors

[Aside: these images were created with Fritzing, which I just discovered looking for a something to draw breadboard prototypes. Getting the schematic was a bit of a faff, but once I realised it was more that a schematic drawing tool I began to think it could be really useful]

I’ve installed WiringPi which gives a nice little command-line utility to set the mode and state of the pi’s GPIO pins, and so is useful for testing.so I knew what pin does what to the motors.

In python I used the RPi.GPIO module to write scripts to run motors as shown above, to test out pwm, and to drive a stepper motor salvaged from an old epson printer.

Example: pulse width modulation for single motor, one-way.

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
gpio1 = 12
GPIO.setup(gpio1, GPIO.OUT)

p = GPIO.PWM(12,50)
p.start(0)
number = 0
old_number = 999

print "Enter number 0..100 to control speed"
print "Enter anything else to stop"

while True:    
    try:
        userInput = int(input())
    except:
        p.stop()
        GPIO.cleanup()
        break
    else:
        number = userInput
        if (number < 101):
            p.ChangeDutyCycle(number)
        else:
            p.stop()
            GPIO.cleanup()
            break

Example 2: stepper motor controller

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
from sys import argv
from sys import exit

try:
    script, n, p = argv
    nsteps = int(n)
    pausetime = float(p)
except:
    print("usage: stepper2 nsteps pausetimentnsteps (integer) number of motor step to make;ntpausetime (float) time to hold current on each step.nte.g stepper2 10 0.1")
    exit()
    

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
gpio0 = 11
gpio1 = 12
gpio2 = 13
gpio3 = 15
gpio4 = 16
GPIO.setup(gpio0, GPIO.OUT)
GPIO.setup(gpio1, GPIO.IN)
GPIO.setup(gpio2, GPIO.OUT)
GPIO.setup(gpio3, GPIO.OUT)
GPIO.setup(gpio4, GPIO.OUT)

a1 = gpio0
a2 = gpio2
b1 = gpio3
b2 = gpio4


def forwardstep(t):
    GPIO.output(a1, 1)
    GPIO.output(b1, 1)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 1)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 1)
    GPIO.output(a2, 1)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 1)
    GPIO.output(b2, 0)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 1)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 1)
    sleep(t)
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(t)

def stop():
    GPIO.output(a1, 0)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)

def init():
    GPIO.output(a1, 1)
    GPIO.output(b1, 0)
    GPIO.output(a2, 0)
    GPIO.output(b2, 0)
    sleep(1)
    stop()

for i in range(1,nsteps):
    forwardstep(pausetime)
stop()

1 thought on “Motor driver

  1. I like this post, enjoyed this one thankyou for posting . eedkbedcbckfgbdd

Comments are closed.