Connecting a Wii nunchuck to Arduino

May 6, 2013

After getting started with the Arduino Uno, in order to get 3-D accelerometer data from the Wii nunchuck and onto the Arduino, you will need the following:
  • a Wii nunchuck: there are some generic non-Nintendo brand nunchucks available for sale for around $6-7 (like this one) but the reviews on those seemed a bit mixed on their longevity. The official one by Nintendo goes for about $18 on Amazon currently, so I ended up buying a used Nintendo brand nunchuck off eBay to try to get the benefits of both.
  • Nunchucky breakout adapter ($3, adafruit) though there are other kinds of nunchuck adapters as well. This is so that you don't have to cut up into the wires of the nunchuck and can just have something to plug it into instead.
The Solarbotics Nunchucky breakout board adapter comes from adafruit in two pieces, like so:



That piece on the right, mine actually came with 6 pins instead of just 4, but you can just snap off 2 of them. GND = ground and 3.3V = power source, while Data = the data coming in off the nunchuck and CLK = a clock for the serial connection. The nunchuck has standard I2C interface, which allows you to read input from the accelerometer inside (x/y/z force), the two buttons, and the joystick (2 axis). Basically even though you hold it in your hand as one device, the nunchuck is actually all these different devices bundled together into one, and the serial connection allows you to read and distinguish the input from different parts of the nunchuck.

Anyway, the pins need to be soldered to the board so that it ends up looking like this:



My Hackbright instructor Christian brought his soldering iron to do this with some lead-free solder that I just bought for $5 at Radioshack. The soldered part lets you plug the 4 pins into the Arduino board with some stability and conductivity for the data to pass through, from A2 to A5 of the Analog pins:



The Wii nunchuck plugs in to the adapter at the bit with the gold bars. The nunchuck's plug comes with  bits on the sides that you can squeeze to release the locking mechanism for plugging/unplugging it:



VERY IMPORTANT NOTE: plug in the nunchuck with the Nintendo logo facing up! There's a plastic bit on the bottom. The adapter will fit in the nunchuck just fine upside down, but this won't match up with the Arduino code because the pins would be all backwards then. I lost a whole afternoon to this, don't repeat my mistake!

From here, you can get the nunchuck library written by Tod E. Kurt. Of the files in the Github repository, you really only need the ones in the WiichuckDemo folder. WiichuckDemo.ino is the file with the example code that you'd want to focus on, while nunchuck_funcs.h is the library of functions you can call from your .ino file to get data off the nunchuck, like whether a particular button has been pressed down.

When you run these files from the Arduino IDE with your nunchuck all plugged in to the Arduino and the Arduino plugged into your computer, this is what you should end up seeing:



Watching that data come in as you wave the nunchuck around is seriously magical! Tod E. Kurt has some extra info in these slides (PDF file) he used for a class (the relevant slides start about halfway through) but what's in this post should be enough to get you set up. The x, y, and z acceleration data should report values from 1-255 with these directions:



Even when you just leave the nunchuck sitting on the table, it will show some values because it's reporting acceleration, not absolute position in space, so even while in a still position, there will be the downward force of gravity acting on it.

For my project, I basically modified the example Wiichuck code so that it would collect data while you held down the big z-button and stop once you let it go. Pressing down the z-button prints out "start", then while you keep that button pressed down, it collects the xyz acceleration vectors, until you let it go, when it prints out "stop". I also added in a timer library to more reliably initiate collecting data every millisecond (Tod Kurt uses a counter variable in the Arduino's loop), though in practice this actually gives readings about every 100ms due to the nunchuck having a sampling rate of about 100Hz.