Since we first got the Raspberry Pi, we have wanted to make a robot out of it. There aren’t many kits out there yet that let you do that (yet). This HowTo Raspberry Pi Project shows you how we hacked together a motor controller from the Raspberry Pi and used it to control LEGO MINDSTORMS Motors.
The LEGO MINDSTORMS system comes with 3 motors that are run at 9V. The LEGO MINDSTORMS motors run by applying 9V power to the White or Black wires in the Mindstorms jack. If you apply 9V to the black wire, the motor moves in one direction. If you apply 9V to the white wire, the motor moves in the opposite direction. And thus you’re able to get the motor to turn, depending on the voltage put across either of the the two wires.
Because the Raspberry Pi can’t control 9V, and is not designed to control the kind of amperage the motors can consume (up to 1 Amp per motor), we needed a switching device that could do that for us. Enter the SN754410NE from Texas Instruments (datasheet). This quadruple H-bridge can control up to 1 amp. Furthermore, because it’s an H-bridge rated for 9V, we can use the Raspberry Pi to control the logic.
We used a 9V battery and the SN754410NE and a rats-nest of jumpers to get this thing working. Below are some pictures, and to the right you can click on the full circuit diagram. You can also download an Eagle File here.
We used a Pi case from Element14 to cover up the Raspberry Pi and built the chassis out of LEGOs.
Finally, we wrote some simple Python code for controlling the motors. As the Raspberry Pi gets a motor command, it rolls forward, backward, turns, and veers. Because we tested this on a desk, and we didn’t have a very long cable to power up the Pi with, we kept the motions short: when a command is sent, the wheels spin a few turns, and then stop.
*Note because this uses the GPIO import, you will need to run it in Root Terminal.[sourcecode language=”python”] import RPi.GPIO as GPIO
#GPIO.setmode(GPIO.BCM) # Use Raspberry Pi Board Pin Numbers
GPIO.setmode(GPIO.BOARD) # Use Raspberry Pi Board Pin Numbers
GPIO.setup(16, GPIO.OUT) # Setup Motor 2.
GPIO.setup(15, GPIO.OUT) # Setup Motor 2.
GPIO.setup(23, GPIO.OUT) # Setup Motor 1.
GPIO.setup(26, GPIO.OUT) # Setup Motor 1.
# GPIO 0 is also connected to ANA on the NXT Breadboard Adapter
# GPIOS Available: 22, 21, 11, 7,
# GPIOS Unavailable: 0,1,4,17, 10, 9, 8
print “Use the Keyboard!”
time.sleep(1) # Sleep 1 second
input_variable = input(‘Direction?’)
if input_variable == 8:
elif input_variable == 2: #Backwards
elif input_variable == 4: #Left
elif input_variable == 6: #Right
elif input_variable == 5: #Stop
elif input_variable == 7: #Bear Left
elif input_variable == 9: #Bear Right
Room for Improvement:
The most obvious room for imporvement here is to get some Pulse Width Modulation on the pins. Right now our robot simply sets the motor logic to high for the direction it wants to go in. By using PWM, we could vary the speed of the motors a bit.
We need a battery pack for this. We could clearly use a battery pack: our model is tethered to the computer and really can’t go very far.
We could use a bluetooth keyboard: for the same reason. Our model can’t wander further than a keyboard wire’s length from us.
If you liked this tutorial, consider purchasing the Breadboard Adapter for LEGO Mindstorms here.