You are on page 1of 4

Raspberry Pi Lego Wii controlled Robot

This is a great little project for KS3/KS4 – Depends on how much time
you want to give students and how far you want to take the project.

1. First of all you need a Raspberry Pi any of the models will work fine for this.
http://uk.rs-online.com/web/p/processor-microcontroller-development-kits/832-6274/

2. You’ll need a micro SD card with Raspian pre-installed


http://www.amazon.co.uk/Micro-preloaded-Raspbian-Raspberry-Model/dp/B00LUXISZA/ref=sr_1_1?ie=UTF8&qid=1436282008&sr=8-1&keywords=micro+sd+raspbian

Or a cheap SD card and download the image and transfer yourself.


http://www.amazon.co.uk/Kingston-8GB-Micro-SD-HC/dp/B001CQT0X4/ref=sr_1_12?ie=UTF8&qid=1436281943&sr=8-12&keywords=micro+sd

3. You’ll need a Bluetooth dongle – Some work better than others.


http://www.amazon.co.uk/Plugable-Bluetooth-Adapter-Raspberry-Compatible/dp/B009ZIILLI/ref=sr_1_1?ie=UTF8&qid=1436280996&sr=8-1&keywords=bluetooth+dongle+raspberry+pi

You will be able to get one from the Pound store if you want a cheap one.

4. You’ll need access to the basics of micro USB power, keyboard and mouse and the Pi attached
to a monitor through HDMI or a converter to DVI or VGA.

5. You’ll need a genuine Wii remote, not an import or fake version as the Bluetooth system won’t
work.

6. You’ll need an Explorer hat pro


http://shop.pimoroni.com/products/explorer-hat

7. You’ll need some Lego – Ebay have bundles quite cheap.

8. You’ll need a few 3-5v motors with wheels – Not easy to find wheels to fit separate 3v motors
http://www.amazon.co.uk/Yellow-Black-Wheel-Single-Geared/dp/B00BG7PFVA/ref=sr_1_11?ie=UTF8&qid=1436281581&sr=8-11&keywords=3v+motors

9. Some wire to connect the motor to the Explorer hat.


http://www.amazon.co.uk/Accmart-Flexible-Breadboard-Adapter-Colorful/dp/B00MQEKXNM/ref=sr_1_5?ie=UTF8&qid=1436281491&sr=8-5&keywords=breadboard+wire

10. You need access to the Internet via Ethernet cable or you’ll need a Wi-Fi dongle to connect to
your Wi-Fi router.

Now you’ve got everything, you can make your robot out of Lego – The motors will need to be secure
and there needs to be enough room to place your Raspberry Pi there and either to move it off or wire
it up when needed.

You need to cut back the wires and solder them to the motor for plus and negative points and then
plug in the other ends to the Motor points in the Explorer hat.
You now need to install Bluetooth to the Raspberry Pi and download the Wii module for Python.

Open a terminal and type in:

sudo apt-get update

Wait until it’s finished

sudo apt-get upgrade

Wait until it’s finished

sudo apt-get autoremove

Wait until it’s finished

sudo apt-get install bluetooth bluez-utils blueman

Wait until it’s finished

sudo service bluetooth status

This will check that Bluetooth is working correctly.

We now need to install the Wii module to connect the remote to the Raspberry Pi using python
programming

Still in the terminal type:

sudo apt-get install python-cwiid

Wait until it’s finished.

You now need the python code to get the Wii to control the motors. You will need to open Python
and add the code to a new window and save it. When complete run the code from a terminal window
by typing:

sudo python wii.py

The code will then run and you will be asked to connect with the Wii remote – You should then be
able to move the robot with the remote. See code on next page.

The code could be developed further and your Lego robot could be developed to include various
extras that could then be programmed to work with the Wii remote.

My students have loved this as a project so hope you enjoy.


The following code was developed by E.Lane at Tavistock College.

import cwiid
import time
import explorerhat

button_delay = 0.1

#forward
def forward():
explorerhat.motor.one.forward(100)
explorerhat.motor.two.forward(100)
time.sleep(1)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()

#backward
def backward():
explorerhat.motor.one.backward(100)
explorerhat.motor.two.backward(100)
time.sleep(1)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()
#left
def left():
explorerhat.motor.one.forward(100)
explorerhat.motor.two.backward(100)
time.sleep(.25)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()
#right
def right():
explorerhat.motor.one.backward(100)
explorerhat.motor.two.forward(100)
time.sleep(.25)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()

def spin():
explorerhat.motor.one.backward(100)
explorerhat.motor.two.forward(100)
time.sleep(5)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()
explorerhat.motor.two.backward(100)
explorerhat.motor.one.forward(100)
time.sleep(5)
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()

print ("Press 1 and 2 on your WiiMote now ....")


time.sleep(1)
try:
wii=cwiid.Wiimote()
except RuntimeError:
print("Error")
quit()

print ("Connected")
print ("Press something")
print ("Press Plus and Minus to disconnect")

wii.rpt_mode = cwiid.RPT_BTN

while 'True':
buttons = wii.state['buttons']

if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):


print ("/nClosing connection ... ")
wii.rumble=1
time.sleep(.5)
wii.rumble = 0
exit(wii)
elif (buttons & cwiid.BTN_UP):
print("UP")
forward()

elif (buttons & cwiid.BTN_DOWN):


print("DOWN")
backward()

elif (buttons & cwiid.BTN_LEFT):


print("LEFT")
left()

elif (buttons & cwiid.BTN_RIGHT):


print("RIGHT")
right()

elif (buttons & cwiid.BTN_A):


print("SPIN")
spin()

You might also like