Monday, August 1, 2011

WiiChuck Library

     One of my favorite libraries for Arduino is the WiiChuck library. WiiChuck allows the Arduino to interface to a Wii Nunchuck. What is cool about the Nunchuck is that it has a three-axis accelerometer, a two-axis joystick, and two buttons. And it's I2C, so it's easy to work with. However, you will need one of these things to actually use it. You'll also need to solder four header pins to little PCB so you can plug it into the Arduino.

There are five contacts in the WiiChuck socket. Three on the right, two on the left.

Four header pins, nicely soldered.
Make sure the NC is lined up with the groove in the middle.


      Here is the link to the WiiChuck library. There are a few out there, but this is the I use the most. Put in the library folder (it's quite simple so I won't write it here) and make sure it shows up in the Sketch/Import Library menu in Arduino. Once you have the library installed and the adaptor ready, plug it into the Arduino board ("-" in ANALOG 2, "c" in ANALOG 5) and upload. the following demo program:


#include <WiiChuck.h>


#include <Wire.h>


WiiChuck wii = WiiChuck();        // sda, scl
 // tx, rx


void setup(){
  wii.initWithPower();
  Serial.begin(9600);
}


void loop(){
  if(wii.read()){
    Serial.print(wii.getJoyAxisX(), DEC);
    Serial.print(",");
    Serial.print(wii.getJoyAxisY(), DEC);
    Serial.print(",");
    Serial.print(wii.getAccelAxisX(), DEC);
    Serial.print(",");
    Serial.print(wii.getAccelAxisY(), DEC);
    Serial.print(",");
    Serial.print(wii.getAccelAxisZ(), DEC);
    Serial.print(",");
    Serial.print(wii.getButtonZ(), DEC);
    Serial.print(",");
    Serial.print(wii.getButtonC(), DEC);
    Serial.print(13, BYTE);
    Serial.print(10, BYTE);
  }
  delay(10);
}
Analog pins 2, 3, 4, 5 with 

The whole hardware setup.
Once it is loaded, click on the serial monitor. You should get something a bit like this:
Serial output

     You can easily figure out which value belongs to which sensor, or you can just look in the code! I hope it worked out for you as well as it did for me. If you have any questions, leave them in the comments.


2 comments:

  1. Do you have an updated version of this, that works with Arduino 1.0.5. I am getting errors that BYTE is no longer supported and this output when I check the sketch after commenting the BYTE lines out:

    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp: In member function 'void WiiChuck::init()':
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp:13: error: 'class TwoWire' has no member named 'send'
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp:14: error: 'class TwoWire' has no member named 'send'
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp: In member function 'boolean WiiChuck::read()':
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp:37: error: 'class TwoWire' has no member named 'receive'
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp: In member function 'void WiiChuck::sendNullRequest()':
    C:\arduino-1.0.3\libraries\WiiChuck\WiiChuck.cpp:135: error: 'class TwoWire' has no member named 'send'

    Thank you.

    ReplyDelete
  2. project dead

    Arduino: 1.5.7 (Windows 7), Board: "Arduino Uno"

    In file included from sketch_jul31a.ino:1:0:
    H:\ARDUINO\arduino\libraries\WiiChuck/WiiChuck.h:10:22: fatal error: WProgram.h: No such file or directory
    #include
    ^
    compilation terminated.

    ReplyDelete