9 months ago

Use Wii Nunchucks with Raspberry Pi

In this tutorial we’ll show you how to use two Wii Nunchuks and code your own unique fast-paced game called The Secret Labyrinth.

We love mazes here at the Bakery, but steering through one on a computer has its problems. Basically they are either too easy or too difficult.

This tutorial was written by Mike Cook and can be found in The MagPi issue 67.

The Secret Labyrinth is hard but intriguing. It can be played as either a two-player or single-player game. The players begin at opposite corners of a maze and have to find the centre by using a series of horizontal and vertical moves controlled by a Wii Nunchuk.

But the twist is that you can’t see the walls in the maze: if you walk into a wall then you are instantly whisked back to your starting point. There are no player turns – just move as quickly as you can – and the maze is constructed so that the path from the bottom to the centre is the mirror image of the path from the top to the centre, so both players face an identical task.

You will need

  • 2 × Wii Nunchuks
  • 2 × Nunchucky adapters
  • Stripboard: 24 strips of 13 holes
  • 74HTC4052 analogue multiplexer
  • 16-pin IC socket
  • Bottom-entry twin-row header sockets
  • 4 × 68 kΩ resistors
  • 0.1 μF ceramic capacitor

The Nintendo Wii Nunchuks

The Wii Nunchuk is a cheap and remarkable interface. While in this project we will just be using the X-Y joystick, the Nunchuk also contains two push-buttons and a 10-bit three-axis accelerometer. They are interfaced through the I2C bus to the Raspberry Pi, but there is a small problem: each device on an I2C bus needs to have a unique address and all Nunchucks have the same address, fixed in the hardware, that can’t be changed. So to get round this problem we have to split the bus by steering the I2C signal lines to one Nunchuk or the other by using an analogue multiplexer chip. The multiplexer needs to be analogue because signals can go from the Raspberry Pi to the Nunchuk, or from the Nunchuk back into the Raspberry Pi. However, all we need to do is to set the multiplexer’s select pins and talk to the Nunchuk normally, and the selected Nunchuk replies.

The schematic for this board is shown in Figure 1 and full construction is shown in the step-by-step section. The Nunchuk itself when read, sends back a block of six bytes; each byte holds different data and a map of this data is shown in Figure 2. Note how the least significant two bits of the accelerator readings are in the last byte. This allows a simple access to an 8-bit reading as often the last two bits are noise. The two push-buttons are in this last byte as well.


Figure 1 Schematic of the I2C multiplex board


Figure 2 Registers returned by the Nunchuk

Making the Nunchucks interface board


Step-01: Preparing the board

Take a piece of stripboard 24 strips wide by 13 holes high, and break the tracks shown by the dark areas with a scalpel or spot face cutter. The dotted lines show the positions of the IC socket and the four bottom-entry sockets. The board will hang over the side of the Pi, not over the printed circuit board.


Step-02: Underside of the board

The board will be plugged into the Raspberry Pi’s GPIO header. Only six connections are needed electrically, but we have used a second four-section of socket for mechanical balance. The two four-length sections of socket are for the Nunchuk adapters to plug into. Solder these sockets up before commencing the wiring.


Step-03: Wiring up

Use stranded wire and make the links between the components, as shown in the diagram. The straight wires to adjacent holes can be made from tinned copper taken from the cut-off legs of the resistors. You can use different colour wires if you have them, but the electricity doesn’t really care what colour wire it flows through.

Step-04: Solder the header pins

Solder the header pins onto the Nunchuk adaptors and plug the leads into them. Then insert them in the bottom-entry sockets on the board. Note that the Nunchuks are inserted in what we would consider to be upside-down. Plug the board into the Pi when it is unpowered, as with anything you plug into the GPIO header.


The Secret Labyrinth maze game

The game is set in a 9×9 grid with players starting at the upper and lower corners. Due to the symmetry of the maze, it is in fact a 9×5 grid that contains the maze, but there is no restriction on a player going into the other half. Each square has four possible exits and these are marked as either blocked or clear in the list that holds the definition of the maze.

So by checking the exit side of a square you want to move from, this will either allow free passage or send the player back to the start square. The maze is generated by starting at the centre and then moving in a random direction until a corner is reached. That corner becomes the start position for the player.

On each random move, the side of the square just gone through is marked as clear and the side of the destination square is also marked as clear. This allows two-way movement between these two squares during the playing phase. The sides are marked as clear by setting the appropriate bit in the maze list element for that square. This is summarised in Figure 3.


Figure 3 Sides of a playing square and its attributes

The side of each square is labelled clockwise from the top with the letters A to D. So if the maze-generating program moves from the current square through side D, then bit 3 of the maze variable is set and the square it moves to (the one immediately to the left) has bit 1 for side B also set. This then becomes the current square and another random movement is made. There are constraints to stop the random movements occurring off the edge of the playing board, so that the whole board is ringed by blocked moves; this simplifies the playing logic. When any side of a square is cleared, the mirror image of that square and side are also cleared.

The game resources

Each square of the board has a tile background chosen at random from a number of tiling strategies. Here we have implemented random tiles, diagonal rows of the same tile, displaced repeating, and all the same tile. Note how diagonal rows are simply displaced repeating with the displacement equal to one. We found that the tiling scheme as well as adding variety to the game also affected the way you play. We chose to use two versions of Tux, the Linux penguin, as the playing pieces. All tiles and playing pieces should be scaled to 72×72 pixels. We again raided the Scratch media files to get the sounds for when a player crosses a blocked boundary and is sent back to the start. A screen dump of a game in progress is shown in Figure 4.

Fig-4 Wii Nunchucks

Figure 4 A game in action; all walls are hidden

Get the game code

The software for the project, labyrinth.py, uses our favourite Pygame frame work. It uses the smbus extension to handle the I2C interface – make sure it is enabled in the Interfaces tab of the Raspberry Pi Configuration tool. The main function is quite simple and consists of an infinite loop repeatedly playing different games. The inner while loop just plays one game by checking for an event or move and, if necessary, redrawing the screen. At the end of the game, the maze’s hidden walls are revealed for a few seconds before generating a new maze and starting the play again. The drawscreen function takes in a Boolean value that determines whether the real walls are to be shown or if every square seems to have a wall. For very small people, you might want to play the game with the walls shown – simply change False to True in the drawscreen function call to do this.

The mazeGen function is perhaps the most complex in this code. It starts off by selecting a tile strategy at random and generating a tilePlan list which stores the tile number associated with each square. In the same loop, the old maze is erased. Then the centre square is set so that it has no barriers around it and the random walk clearing the walls is begun from the centre until either top corner is visited. That completes the maze and ensures at least one path through it has been generated. We have a version of the program in our GitHub repository that uses only keyboard keys to move. Finally, the G key will give a glimpse of the walls for a second or two – only to be used if you are really stuck.

Taking it further

You could add a timer and keep a high score of the speed of a run. Or you could change the game into a turn-based affair. As the maze is symmetrical, you could – if you are clever – avoid some walls by remembering what your opponent has done. You could also change the ending point of the maze generation when it reaches the top or bottom row instead of a corner. For the more advanced, you could generate the mazes by hand, or another program, and store several of them in a file and pick the one to use at random. This would ensure a much greater variety of mazes, and allow you to make quite complex ones, but beware of making them too complex.

You can download the code from Mike’s GitHub page.