How To Control The Direction Of a Rotational Servo Motor With a Wiichuck and Arduino Board

by Miguel on July 15, 2011

in Arduino

Arduino, nunchuck and servo motor circuit

Arduino, nunchuck and servo motor

Controlling a servo with your wiichuck and the WiiChuck Adapter is easier than you think, read below to see how.

The Circuit

The circuit is one of the easiest you’ll probably see, the wiichuck adapter is layout so that you just have to plug it in, the servo motor as you might already know has three pins: GND (black), PWR(red) and signal(white). We will be using pin 11 to make the signal for the motor.

wiichuck servo control schematic

wiichuck servo control schematic

The Code

You’ll need the wiichuck library for this, download it and paste in your "libraries" folder in your Arduino directory.

What we want to do is turn the motor left if you move the chuck’s joystick all the way to the left and turn it right if you move it to the right. Please note that the motor am using here is a full rotational model so when I use the servo library the angles get translated to speed and direction, 90 degrees is zero speed, 0 degrees is full speed right, 180 is full speed right.

#include 
#include 
#include "nunchuck_funcs.h"

Servo myservo;
int x=0;
void setup()
{
  myservo.attach(11);
  nunchuck_setpowerpins();
  nunchuck_init();
  myservo.write(90);
  Serial.begin(9600);
}

void loop()
{
  nunchuck_get_data();
  x=nunchuck_joyx();
  if(x > 200)
   myservo.write(0);
  else if(x < 40)
   myservo.write(180);
  else
   myservo.write(90);

  Serial.print("joy X: ");Serial.println(nunchuck_joyx());
  
}

Note that if you remove the Serial.print line, for some reason, the motor will not turn. The values 200 and 40 were found by testing, all I did was move the joystick one way and the other several times to see what values they produced, then I choose values (200 and 40) that will be sure to include these values.

Previous post:

Next post: