Project tutorial

Control Servos using Wii Nunchuk

In this project we will connect a Wii nunchuk to an Arduino and control pan/tilt servos.

  • 22,417 views
  • 11 comments
  • 78 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
Pan/Tilt Kit
×1
Wii Nunchuk
×1
Micro Servo
Adafruit also has a fully assembled kit with servos if you want to use that.
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1
Wii Nunchuk
×1

About this project

Intro:

I came across some old Will nunchuks in a closet and thought, "perfect gadget to connect to an Arduino".  After some time with a hackster.io users best friend, Google, it looked like the nunchuk uses I2C.  Perfect for an Arduino.  It also looks like a lot of people have done exactly that.  So I did some digging at found some code from todbot.com to utilize the Will nunchuk then added my own code to control the servos!

This is a very simple project and I am sure you can find plenty of applications for this.  Like teasing your cat with a laser pointer attached.  :)

Step 1 - Wii Nunchuk

There are a couple ways to connect this to the Arduino.  You can buy a Wii nunchuk adapter or do what I did and just cut the wires since I didn't have a Wii to connect it to any longer.

I soldered some solid core wires to the nunchuk wires so I could make breadboard connections easier.  I matched the color to the exposed nunchuk wires.


Connect Wii Nunchuk to Arduino UNO as follows:

White wire (GND) --> GND

Red wire (+5V) --> 5V

Green Wire (SDA) --> Analog 4 or dedicated SDA pin

Yellow (SCL) --> Analog 5 or dedicated SCL pin

Step 2 - Connect Servos to Arduino

Connect the three wires to the Arduino as follows:

Servo 1 (x-axis)

Brown (GND) --> GND

Red (5V) --> 5V

Yellow (data/Signal) --> Pin 10 on the UNO

Servo 2 (y-axis)

Brown (GND) --> GND

Red (5V) --> 5V

Yellow (data/Signal) --> Pin 9 on the UNO

Step 3 - Bringing it all together

Here's what it looks like all connected.  Kinda messy sorry.

Step 4 - Upload the code

Upload the code using the Arduino IDE. There are 2 standard libraries that you need.

  • Wire.h
  • Servo.h

The code will do the following:

  • Initialize the nunchuk using I2C
  • Initialize the servos
  • Read the nunchuk data
  • Move the servos based on the nunchuk readings


*** Thanks to todbot.com for the Will nunchuk code.


See it in action!!


I hope this is useful and please provide me with any feedback as I am always willing to learn. Enjoy!!!

Code

nunchuk_codeArduino
Code used to read Wii nunchuk and position servos
/*
 * NunchuckPrint
 *
 * 2007 Tod E. Kurt, http://todbot.com/blog/
 *   
 * Change log:
 * 
 *    Mark Tashiro -    Changed Wire.read to Wire.write
 *                      Changed Wire.receive to Wire.read
 *                      Added code for servos
 *
 */
 
#include <Wire.h>
#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

void setup()
{
  Serial.begin(19200);
  
  servoLeft.attach(10);  // Set left servo to digital pin 10
  servoRight.attach(9);  // Set right servo to digital pin 9
  
  nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  nunchuck_init(); // send the initilization handshake
  Serial.print ("Finished setup\n");
}

void loop()
{
  nunchuck_get_data();

// map nunchuk data to a servo data point
  int x_axis = map(nunchuck_buf[0], 23, 222, 180, 0);
  int y_axis = map(nunchuck_buf[1], 32, 231, 0, 180);

//move servo to desired position based on Wii nunchuk reading
  servoLeft.write(x_axis);
  servoRight.write(y_axis);
    
// un-comment next line to print data to serial monitor  
//  nunchuck_print_data();          

}


//
// Nunchuck functions
//

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize        
}

// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{ 
  Wire.begin();                      // join i2c bus as master
  Wire.beginTransmission(0x52);     // transmit to device 0x52
  Wire.write(0x40);            // sends memory address
  Wire.write(0x00);            // sends sent a zero.  
  Wire.endTransmission();     // stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request()
{
  Wire.beginTransmission(0x52);     // transmit to device 0x52
  Wire.write(0x00);            // sends one byte
  Wire.endTransmission();     // stop transmitting
}

// Receive data back from the nunchuck, 
int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6);     // request data from nunchuck
    while (Wire.available ()) {
      // receive byte as an integer
      nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
      cnt++;
    }
    nunchuck_send_request();  // send request for next data payload
    // If we recieved the 6 bytes, then go print them
    if (cnt >= 5) {
     return 1;   // success
    }
    return 0; //failure
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{ 
  static int i=0;
  int joy_x_axis = nunchuck_buf[0];
  int joy_y_axis = nunchuck_buf[1];

  int accel_x_axis = nunchuck_buf[2]; // * 2 * 2; 
  int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
  int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;


  int z_button = 0;
  int c_button = 0;

  // byte nunchuck_buf[5] contains bits for z and c buttons
  // it also contains the least significant bits for the accelerometer data
  // so we have to check each bit of byte outbuf[5]
  if ((nunchuck_buf[5] >> 0) & 1) 
    z_button = 1;
  if ((nunchuck_buf[5] >> 1) & 1)
    c_button = 1;

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

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

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

  Serial.print(i,DEC);
  Serial.print("\t");
  
  Serial.print("joy:");
  Serial.print(joy_x_axis,DEC);
  Serial.print(",");
  Serial.print(joy_y_axis, DEC);
  Serial.print("  \t");

  Serial.print("acc:");
  Serial.print(accel_x_axis, DEC);
  Serial.print(",");
  Serial.print(accel_y_axis, DEC);
  Serial.print(",");
  Serial.print(accel_z_axis, DEC);
  Serial.print("\t");

  Serial.print("but:");
  Serial.print(z_button, DEC);
  Serial.print(",");
  Serial.print(c_button, DEC);

  Serial.print("\r\n");  // newline
  i++;
}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}

Comments

Similar projects you might like

Using a Wii Nunchuk with Arduino

Project tutorial by infusion

  • 19,466 views
  • 49 comments
  • 32 respects

Arduino Dynamic Web Control

Project tutorial by phpoc_man

  • 34,757 views
  • 15 comments
  • 117 respects

Using Buttons to Control Servos with Annikken Andee

Project tutorial by Arduino_Scuola

  • 1,226 views
  • 1 comment
  • 2 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 49,066 views
  • 63 comments
  • 145 respects

Take Control Over Lego Power Functions

Project tutorial by Patrick Müller

  • 43,672 views
  • 3 comments
  • 60 respects
Add projectSign up / Login