Build cool stuff with Lego and Pi

Wii Remote Controller

With the Lego Mindstorms NXT, you can make some really interesting robots and rovers. We thought it would be neat to use an old Wii remote as a controller (Figure 4).

Figure 4: NXT rover controlled by a Wii remote.

For this project, we connected the NXT motors on ports B and C. The NXT motor library has a number of different functions; we only needed the run and brake calls here. Listing 4 shows our motor test program.

Listing 4

motor_test.py

01 import time
02 import nxt.locator
03 from nxt.motor import *
04
05 b = nxt.locator.find_one_brick()
06
07 left = Motor(b, PORT_B)
08 right = Motor(b, PORT_C)
09
10 left.run(power = 100,regulated=False)
11 right.run(power = 100,regulated=False)
12
13 time.sleep(1)
14
15 right.brake()
16 left.brake()

The next step is to get the Python library called cwiid that uses Bluetooth to communicate with the Wii remote. This package is installed by entering:

sudo apt-get install python-cwiid

Only one Bluetooth dongle is required for both Wii and NXT communications to the Raspberry Pi. Unlike the NXT, the Wii remote does not need to be paired with the Pi.

To get your program to see the Wii remote, you need to push buttons 1 and 2 at the same time on the Wii remote. Listing 5 shows our Wii remote test program.

Listing 5

wii_test.py

01 import time
02 import cwiid
03
04 print 'Press button 1+2 on your Wiimote now...'
05 wii = cwiid.Wiimote()
06
07 time.sleep(1)
08
09 wii.rpt_mode = cwiid.RPT_BTN
10 print 'WII Remote Connected'
11
12 while True:
13   buttons = wii.state['buttons']
14
15   if (buttons & cwiid.BTN_UP):
16     print 'up button'
17     time.sleep(0.2)
18
19   if (buttons & cwiid.BTN_DOWN):
20     print 'down button'
21     time.sleep(0.2)

For our final program, we used the following buttons:

  • Up – Run both motors forward.
  • Left – Run the left motor, brake the right motor.
  • Right – Run the right motor, brake the left motor.
  • Down – Brake both motors.
  • A – Make a noise.

Listing 6 shows the final program that controls the rover with the Wii remote.

Listing 6

rover_wii.py

01 import cwiid
02 import time
03 import nxt.locator
04 from nxt.motor import *
05
06 FREQ_E = 659
07
08 print 'Looking for a Lego NXT ... may take up to 15 seconds...'
09 b = nxt.locator.find_one_brick()
10 print 'Lego NXT Connected'
11
12 left = Motor(b, PORT_B)
13 right = Motor(b, PORT_C)
14
15 print 'Press 1+2 on your Wiimote now...'
16 wii = cwiid.Wiimote()
17 time.sleep(1)
18 wii.rpt_mode = cwiid.RPT_BTN
19 print 'WII Remote Connected'
20
21 while True:
22   buttons = wii.state['buttons']
23   if (buttons & cwiid.BTN_LEFT):
24     right.brake()
25     left.run(power = 100,regulated=False)
26     time.sleep(0.1)
27
28   if (buttons & cwiid.BTN_RIGHT):
29     left.brake()
30     right.run(power = 100,regulated=False)
31     time.sleep(0.1)
32
33   if (buttons & cwiid.BTN_UP):
34     right.run(power = 100,regulated=False)
35     left.run(power = 100,regulated=False)
36     time.sleep(0.1)
37
38   if (buttons & cwiid.BTN_DOWN):
39     left.brake()
40     right.brake()
41     time.sleep(0.1)
42
43   if (buttons & cwiid.BTN_A):
44     b.play_tone_and_wait(FREQ_E, 500)

Some possible future rover projects include the following:

  • Adding more button functions like speed control.
  • Adding the ultrasonic sensor for collision control.
  • Creating a trailer for the rover to carry the Raspberry Pi with a Rasp Pi camera module attached.
  • Making a robot arm instead of a rover.

We hope these ideas will inspire you to try a project of your own.

The Author

Brooke and Leah Metcalfe are 12-year-old twins who live in Burlington, Canada. When they aren't doing computer projects with their dad, Pete, they like swimming, paddleboarding, and skiing.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content