LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1)

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.LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1)

Hardware

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.

The Code:

*Note because this uses the GPIO import, you will need to run it in Root Terminal.

[sourcecode language=”python”] import RPi.GPIO as GPIO
import time

def setup_pin():
#GPIO.setmode(GPIO.BCM) # Use Raspberry Pi Board Pin Numbers
GPIO.cleanup()
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

def motor_1_fwd():
GPIO.output(16, GPIO.LOW)
time.sleep(0.01)
GPIO.output(15, GPIO.HIGH)
time.sleep(0.01)

def motor_1_rev():
GPIO.output(15, GPIO.LOW)
time.sleep(0.01)
GPIO.output(16, GPIO.HIGH)
time.sleep(0.01)

def motor_2_fwd():
GPIO.output(23, GPIO.LOW)
time.sleep(0.01)
GPIO.output(26, GPIO.HIGH)
time.sleep(0.01)LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1) Schematic

def motor_2_rev():
GPIO.output(26, GPIO.LOW)
time.sleep(0.01)
GPIO.output(23, GPIO.HIGH)
time.sleep(0.01)

def motor_1_stop():
GPIO.output(15, GPIO.LOW)
time.sleep(0.01)
GPIO.output(16, GPIO.LOW)
time.sleep(0.01)

def motor_2_stop():
GPIO.output(26, GPIO.LOW)
time.sleep(0.01)
GPIO.output(23, GPIO.LOW)
time.sleep(0.01)

def keyboard_input():
try:
mode=int(raw_input(‘Input:’))
except ValueError:
print “Use the Keyboard!”
return mode

def main():
setup_pin()
time.sleep(1) # Sleep 1 second

motor_1_stop()
motor_2_stop()

while True:
input_variable = input(‘Direction?’)

 

For more detail: LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1)

Scroll to Top
Scroll to Top
Read previous post:
Raspberry Pi Home CCTV System

Hello, In this instructable, I will be showing you how to make a small CCTV system using the raspberry pi....

Close