Arduino Nunchuck project

Mar 8, 2013  

I have cobbled together a couple of circuits to combine output to an OLED with input from a Nunchuck, through a WiiChuck adapter.  I have avoided using the analogue pin (by plugging the adapter directly into the arduino, over analogue pins 2-5) and instead used the 3.3v output because apparently the Wii Nunchuck should be powered by 3.3v and not 5v.  I have attached a diagram, but for clarity, there is the wiring explained here too:

Nunchuck circuit diagram

WiiChuck:

  • + to 3.3v
  • - to GND
  • d to analogue 4
  • c to analogue 5

OLED 128x32 SSD1306:

  • CS to digital 12
  • Reset (RST) to digital 13
  • D/C to digital 11
  • Clock (CLK) to digital 10
  • Data to digital 9
  • Vin to 5v
  • GND to Ground
Nunchuck image

I also merged together the AdaFruit sample code for the OLED SSD1306 with a Nunchuck library found here:

http://www.timteatro.net/2012/02/10/a-library-for-using-the-wii-nunchuk-in-arduino-sketches/

The snippet of code I used was simple, just to get it working, and has the functions from both libraries mentioned earier:


void setup()
{
 Serial.begin(19200);
 nunchuk_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
 nunchuk_init(); // send the initilization handshake
 Serial.print ("Finished setupn");

 setup_oled();
}

void loop()
{
 nunchuk_get_data();
 // nunchuk_print_data();
 nunchuk_display_data();
 delay(100);
}

void nunchuk_display_data() {
 static int i=0;
 int joy_x_axis = nunchuk_buf[0];
 int joy_y_axis = nunchuk_buf[1];
 int accel_x_axis = nunchuk_buf[2]; // * 2 * 2;
 int accel_y_axis = nunchuk_buf[3]; // * 2 * 2;
 int accel_z_axis = nunchuk_buf[4]; // * 2 * 2;

int z_button = 0;
 int c_button = 0;

 z_button = nunchuk_zbutton();
 c_button = nunchuk_cbutton();

if ((nunchuk_buf[5] >> 2) & 1)
 accel_x_axis += 2;
 if ((nunchuk_buf[5] >> 3) & 1)
 accel_x_axis += 1;

if ((nunchuk_buf[5] >> 4) & 1)
 accel_y_axis += 2;
 if ((nunchuk_buf[5] >> 5) & 1)
 accel_y_axis += 1;

if ((nunchuk_buf[5] >> 6) & 1)
 accel_z_axis += 2;
 if ((nunchuk_buf[5] >> 7) & 1)
 accel_z_axis += 1;

 i++;

display.clearDisplay();

// text display tests
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);

 if (joy_x_axis < 100) {
 display.print("Move Left - ");
 display.println(joy_x_axis, DEC);
 } else if (joy_x_axis > 150) {
 display.print("Move Right - ");
 display.println(joy_x_axis, DEC);
 }

if (joy_y_axis > 150) {
 display.print("Move Up - ");
 display.println(joy_y_axis, DEC);
 } else if (joy_y_axis < 110) {
 display.print("Move Down - ");
 display.println(joy_y_axis, DEC);
 }

if (accel_x_axis < 110) {
 display.print("Tilt Left - " );
 display.println(accel_x_axis, DEC);
 } else if (accel_x_axis > 140) {
 display.print("Tilt Right - ");
 display.println(accel_x_axis, DEC);
 }

if (accel_y_axis < 100) {
 display.print("Tilt Backwards - ");
 display.println(accel_y_axis, DEC);
 } else if (accel_y_axis > 155) {
 display.print("Tilt Fowards - ");
 display.println(accel_y_axis, DEC);
 }

if (accel_z_axis < 100) {
 display.print("Upside Down - ");
 display.println(accel_z_axis, DEC);
 } else if (accel_z_axis > 140) {
 display.print("Upright - ");
 display.println(accel_z_axis, DEC);
 }

if (c_button && z_button) {
 display.println("Both buttons pressed");
 } else if (c_button) {
 display.println("C button pressed");
 } else if (z_button) {
 display.println("Z button pressed");
 }

 //display.setTextColor(BLACK, WHITE); // 'inverted' text
 //display.setTextSize(2);
 //display.setTextColor(WHITE);
 //display.print("0x"); display.println(0xDEADBEEF, HEX);
 display.display();
}

void setup_oled() {
 display.begin(SSD1306_SWITCHCAPVCC);
 display.clearDisplay(); // clears the screen and buffer
}