Dexter Industries LEGO Robot WIth Raspberry Pi

LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1)

Dexter Industries LEGO Robot WIth Raspberry Pi

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.

Hardware

Dexter Industries LEGO Robot WIth Raspberry PiBecause 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.

Circuit Diagram of connecting the Raspberry Pi and LEGO MOTORSWe 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.

Wiring the Raspberry PI and LEGO MINDSTORMS Motors

We used a Pi case from Element14 to cover up the Raspberry Pi and built the chassis out of LEGOs.

Dexter Industries LEGO Robot WIth Raspberry PiFinally, 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.

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)

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?')

		if input_variable == 8:
			motor_1_fwd()
			motor_2_fwd()
		elif input_variable == 2:		#Backwards
			motor_1_rev()
			motor_2_rev()
		elif input_variable == 4:		#Left
			motor_1_fwd()
			motor_2_rev()
		elif input_variable == 6:		#Right
			motor_1_rev()
			motor_2_fwd()
		elif input_variable == 5:		#Stop
			motor_1_stop()
			motor_2_stop()

		elif input_variable == 7:		#Bear Left
			motor_1_fwd()
			motor_2_stop()

		elif input_variable == 9:		#Bear Right
			motor_1_stop()
			motor_2_fwd()

		else:
			motor_1_stop()
			motor_2_stop()

		time.sleep(0.5)
		motor_1_stop()
		motor_2_stop()

#if __name__=='__main__':

main()

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.

Video

And we made a quick video recapping the LEGO robot with Raspberry Pi Brains.

7 thoughts on “LEGO MINDSTORMS Motors with Raspberry Pi (BrickPi 0.1)”

  1. hey guys! guess what? you guys can find a working video here! we used a little different method for us because the SN75441o didnt work for us! visit nerdherdempire.weebly.com

  2. Thank You so much! I finally was able to power my NXT motors!
    I didn’t notice my setup had the wrong wires connected.
    I had the Blue and Yellow as opposed to the Black and White ones.

  3. Hi!!

    First of all great work you have done here. I’m trying to “expand” your work here to support feedback from encoders and add support to LEGO sensors too…and I’m willing to design my own board with this software you used here (eagle), but when i downloaded your board design I realised that u make your own libraries for the NXT motors, this libraries will be really useful to me…so is there any way u can send it to me it will be really appreciated… When the my project is finished i will send you a github or something (it doesn’t exist by the moment) if you are interested in…
    Well great work again, this post was really helpful to me and thanks in advance. (I’m sure you can see my mail address, if it isn’t the case please make me know).

    Thanks again!

  4. Hey!!!

    After all I been able to find the connector you used for your design…please ignore my last post..and sorry for the inconvenience…

    P.D: I’ll send you the github when finished anyway…

    Sorry again… :-S

  5. Hi!
    Why in the diagram do you have the NXT motors connected to GND and 5V? Shouldn’t be enought with just connecting the black/white to the H-Bridge?

    BTW, awesome post and congrats on the final BrickPi! I may get one in the future :)

Comments are closed.

Translate »