One of my recent projects requires a joystick and two buttons as interface. To build it by my own wouldn't be the problem, but the moment i heard that the Wii Nunchuk works with I2C I couldn't help - I must use it!
Here I show how to get the data from the controller via I2C. Those data then will be shown on a display and via 6 LEDs.
You're going to need the following parts:
|1||Arduino||Uno or Nano|
Besides the breadboard the shematic plan is given, because it got a little bit thight due to the LEDs.
In real the setup should look like this:
Attention! The operating voltage of the Nunchuk is 3.3V. Do not supply it with 5V, it will harm your controller! But: The I2C lines are provided with a logic level shifter. This means you can use the I2C pins without any additional hardware.
Most I2C displays with that size work with 5V. For more information about the display and its use, click here. If you just want the necesarry library, click here. Just copy the library into the directory for libraries of the Arduino IDE (../Arduino/libraries/).
As always: GitHub.
In the main directory on GitHub you'll find 3 *.ino files. If you use the standard Arduino IDE, just open the main file – Wii_Nunchuk.ino. The other two files will be opened automatically in separeted tabs. If all files are open, I will describe my functions in the code:
- setup. Set pins, initialize controller, calibrate controller (more later), initialize display.
- loop. Request the data from the nunchuk, bring them to a "neat" output format. Print the data to the display and set the LEDs.
- printFormNum. To display the data right-bound on the display the program adds blank spaces to the string, if they are needed.
- sendDataToNunchuk. Here data is sent via the Wire.h library. For more info about the I2C communication: http://playground.arduino.cc/Learning/I2C.
- initNunchuk. At the beginning the Nunchuk must be initialized. Here the registers are set.
- getNunchukData. Here happens the magic. At first you send a request to the controller for the first 6 registers (0x00, 0x01, ...). Behind those registers there are the position data of the joystick and the buttons. The moment the controller received the request it sends the data back. The map function is for the calibration.
Is the code on the Arduino you can read the data which the Nunchuk sends to it from the display. Besides that the brightness of the LEDs changes depending on the input you do on the controller.
What's now the calibration? The data the Arduino receives from it are raw, so it is very likely, that the maximum positions of the joystick don't correlate with the maximum values (0 or 255). Example: You push the joystick to the far left, the number value on the display should be zero, but it isn't. At this you have to calibrate it. First: Search the following lines in the code and change the number values to:
nunchuk.MinX = 0; nunchuk.MaxX = 255; nunchuk.MinY = 0; nunchuk.MaxY = 255;
Upload the code and note the number values of each maximum joystick position:
- MinX is the value on the display when the joystick is pushed to the far left (eg: 29).
- MaxX: Value when the stick is pushed to the far right (eg 228).
- MinY: Value when the stick is pushed to the bottom (eg 33).
- MaxY: Value for the top most position (eg 222).
Now add those values to the calibration:
nunchuk.MinX = 29; nunchuk.MaxX = 228; nunchuk.MinY = 33; nunchuk.MaxY = 222;
Upload the code again - now it is calibrated for this one controller.
That's it. For questions or comments: deloarts.wordpress.com