ArduinoNunchuk – Wii Nunchuk library for Arduino

Introduction

I’m working on a project that uses a Wii Nunchuk and an Arduino micro-controller and I needed a way to get the data from the Wii Nunchuk using my Arduino. There are a few libraries that already can do that. However, none of them suited me. Some were too complex, some were outdated and most did not work with Arduino 1.0. Therefore, I decided to create my own library, called ArduinoNunchuk. While writing it, I had two things in mind:

  • The library must be simple and easy to implement in other projects.
  • The library should use the best way to access the information from the Wii Nunchuk.

Most libraries initialize the controller by writing 0×00 to 0×40. When this is done, the data bytes are encrypted and only Nintendo’s controllers will work. However, there is another way to initialize the Wii Nunchuk that will make the data unencrypted and will allow some 3rd party controllers to work too. You should write 0×55 to 0xF0 and then 0×00 to 0xFB instead. This new, improved method was used in my library.

Another change was in the precision of the accelerometer’s data. The Wii Nunchuk sends data in 6 bytes and bytes 2, 3 and 4 contain the values from the accelerometer. However, there are still some digits in bits 2-7 from byte 5. These values are normally ignored in other libraries. Although they do not cause a major change in the data, they increase the precision of the accelerometer’s value. Therefore, I decided to implement this too.

 

Instructions

The library is very simple to be used. You create an instance using:

ArduinoNunchuk nunchuk = ArduinoNunchuk();

In you setup() method, you initialize it:

nunchuk.init();

And in your loop() method, you update the values:

nunchuk.update();

The data from the Wii Nunchuk will be available in the following format:

nunchuk.analogX
nunchuk.analogY
nunchuk.accelX
nunchuk.accelY
nunchuk.accelZ
nunchuk.zButton
nunchuk.cButton

 

Download

You can download the library ArduinoNunchuk on GitHub. Feel free to use it in any project of yours. The only thing I ask is that you credit me if you adapt/reuse/distribute the library. Also, please share any projects that you create or ask any question on the comments section below.

 

FAQ

How do I use this library?

Download all the files from the link above and place the ArduinoNunchuk folder them into your libraries folder, which can be found at:

Windows: My Documents\Arduino\libraries\
OSX: ~/Documents/Arduino/libraries/

If the libraries folder does not exist, you should create one. You must restart the Arduino IDE after moving the folder. You can now import the library to an existing project in Sketch > Import Library... > ArduinoNunchuk. There is also an example file for reference, which can be found at File > Examples > ArduinoNunchuk > ArduinoNunchukDemo. This example file prints all the data from the Wii Nunchuk to the computer’s serial port.

 

References & Further Resources

I based my library on the following projects/websites:

230 Responses to ArduinoNunchuk – Wii Nunchuk library for Arduino

  1. Julio Dario Lopez says:

    hello mr gabriel, im trying your code but im getting chars like these

    !…¡¡±Å¬)”R©…
    (…$V
    ©‘
    !…¡¡ M¬”R©•”©•”¥••¥ª Å ”Ä(•ª Å ˆ”Ä(•¥•Å …¬””±‘$¡¡±¥¡‘%¡¡E¥±(‰…Å¡•$V
    ©
    !…¡¡±Å¬)”R©…
    (…$V
    ©‘
    !…¡¡ M¬”R©•”©•”¥••¥ª Å ”Ä(•ª Å ˆ”Ä(•¥•Å …¬””±‘$¡¡±¥¡‘%¡¡E¥± ¥•Å …¬T”´±Ä(•*¥•¬•$¡¡E¥¡‘$¡¡E¥¡‘$¡¡E¥±” ¡¡±¥¡” ¡±E¥±• ¡±±¤•V”©¡” ¡´µ±• ¡±Å¥¡… ¡±Å¥¡…±¡±¡¥1¥…•Å ”T´Dµ¡” ¡±E¥¡• ¡±±¤•V©¡• ¡±±¥¡• ¡±R
    ­
    • ¡±±¥¡• ¡±Å¥¡…±¡±Å¥±…±¡±¡¥¡…±¡±Å¥±…±¡±Å¥! (‰…Å¡$V*
    …Å¡$V
    ­
    !…¡¡±Å¥!”*Ņ€!Ä(”!”(%¤…± °E¤¥¡Å…€)Ä(”$V
    ©
    !…¡¡±(¬”©¡¡±(¬”©•”…©•(¥••*¥•¬‘%¡¡E¥…–
    (Í”Ä,…R
    ¥ ¬” ¡¡E¥¡” ¡±E¥¡”Ä,…R*­
    ” ¡±E¥¡• ¡±Å¥¡• ¡±Å¥¡…±¡4R
    ­(
    … ¡±¡¥±…±¡±Å¥±…±¡±Å¥±…±¡´µ±…±¡´E¥±…¡¡¡Å¥!
    (‰…Å¡$V*©
    !…¡¡±Å¬)”©…”$V
    ©
    !…¡¡±(¬”©•”…¥••¥•… ±(¬¥•”•$¡¡E¥Å–
    (Í”Ä(•…R
    ¥…¬‘$¡¡E¥¡‘%¡¡±¥¡”!±¡‘$¡¡E¥Å–
    ©±‘$¡””¡‘$¡¡E¥¡‘$¡¡E¥±” ¡¡±¥¡” ¡±E¥¡” ¡±±¥¡• ¡±Å¥!¥Ä,…R*­(
    • ¡±¡¥¡…±¡±Å¥±…±¡±¡¥±…±¡±Å¥±…±¡±Å¥! (‰…Å¡…‡
    ¬T
    !…! (‰…Å¡¤V
    ©
    )…¡¡± ¬)”*¥¤…µ)
    ($V
    ‰‘
    !…!”*%¤…µ ©¤E¤¡Å€”Ä(”($V
    ©
    !…¡¡±(¬©•”¥••*¥…¬‘$¡¡±¥¡‘$¡¡E¥±•%¡”´¡” ¡¡E¥¡” ±R
    àÍ”Ä,…E¥±‘$±R
    ¥…¬•$¡¡E¥¡‘%¡¡±¥¡”(4R
    ¥ ¬” ¡¡±¥¡” ¡±E¥±• ¡±Å¥

    what do you think? im just copy and paste your code and installed the libraries, do you think is the nunchuck? its not the original thanks i hope you can give me your opinion

  2. dietrich says:

    hey Gabe, “ArduinoNunchukDemo:15: error: ‘ArduinoNunchuk’ does not name a type
    ArduinoNunchukDemo.ino: In function ‘void setup()’:
    ArduinoNunchukDemo:20: error: ‘nunchuk’ was not declared in this scope
    ArduinoNunchukDemo.ino: In function ‘void loop()’:
    ArduinoNunchukDemo:25: error: ‘nunchuk’ was not declared in this scope

    when i try to run the demo this is what i get… how can i fix this?

    • Hi Dietrich.
      This error occurs when you haven’t installed the library correctly. Did you place the ArduinoNunchuk folder (which contains a .h file, a .cpp file and an ‘examples’ folder) in your Arduino library folder?
      Let me know if the error persists.
      Gabriel

  3. GermanArduino says:

    Hey, I’m from germany and i found your library with google-search. It works very fine and the Lib is easy to use. Very recommend for newb’s like me :-)

    thx 4 your work, you help me a lot!

  4. Sheldon says:

    Thanks for sharing this Gabriel.

    I had a few problems that I managed to solve, so I thought I should post them here in case they help someone.

    Firsty, I was struggling to read the data from my cheap Nunchuck knock-off. Shorting the “detect” pin (the pin between Vcc and SDA) to Vcc solved this problem.

    Secondly, my first line of data wasn’t coming through as a clean array of integers, so I inserted ‘ser.readline()’ at about line 36 of the Python code, just before ‘while ser.isOpen():’. This discards the first line of data before entering the loop. (A dirty hack, I know)

  5. Zsolt says:

    Arduino onu R3 wirelless nunchuck

  6. Zsolt says:

    Hello
    -25082 -13560 1027 2 -4059 1 0
    can somebody help me?

  7. tanorian says:

    Hello Mr. Bianconi – this looks great – so far…. I have the Nunchuk plugged into the Arduino and looking at the serial monitor, it looks like it’s working fine. Moving the joystick changes the first and/or second numbers, pressing the buttons change the last two from 0 to 1…. But when I try to run the Python module I’m getting the following error: Traceback (most recent call last):
    File “C:\Python27\Scripts\NunchukMouse_release.py”, line 2, in
    import math, string, time, serial, win32api, win32con
    ImportError: DLL load failed: The specified module could not be found.

    I looked around the python folder and I can’t find these modules. What did I do wrong when I was setting up Python/pyserial/pywin32?

    • Hi Tanorian,
      It seems that you didn’t install pywin32 or pyserial correctly. Try to run the script with only one module at a time and see which causes the error. Then try to reinstall the module according to the instructions. Good luck!
      Gabriel

  8. zach says:

    hi
    loving the library
    it works well
    im new to the arduino but not to programming(a bit of experience with C++and AS3)
    i have never done controller programming though,especially with accelerometers.
    when i am recieveing the values from the nunchuck
    the x Axis high is about 225
    and low is about 35
    same with the Y axis, im guessing this is normal but im just checking

    and with the accelerometer though
    the X floats at about 500
    the Y floats at about 370
    and the Z floats at about 680
    should they be this high?
    and how would it be proper with detecting the correct changes in those values

    • zach says:

      furthering my post here – i found a problem
      when i used my old wii nunchuck(white, 2006-7ish) the values were read just fine
      when i used my newer wii nunchuck(black 2011-12ish)the values were all maxed out except the buttons
      255 255 1024 1024 1024 0 0
      no idea what could be causing it though

      • zach says:

        sorry to be leaving multiple comments but i fixed the black nunchuck problem
        apparently you have to reset if you plug in a new nunchuck or it wont read the values
        im guessing cuz it sent the initializer buffer to the first nunchuck so the second one never got the signal for processing the values or something along those lines

    • Hi Zach,
      The values are correct. You should map the values to your desired range. There’s a function called ‘map’ for that; you can find more information about it on Arduino’s website. The best method to use the values depend on your project; what are you building?
      Best regards,
      Gabriel

  9. henry89 says:

    hi, first of all thanks for the library it’s really handy! I’ve been running your example code to get calibration values for the joystick to use in my code. The Y axis works fine but the X axis is taking considerable time to change value to and from the limiting values and the zero value. I wanted to use this library to ‘jog’ a CNC machine, by outputting step and direction signals to it’s stepper drivers..Unfortunately this could be hazardous if the joystick reading doesn’t change fast enough. Do you think it could be my Nunchuck? Or is there anything you could recommend, such as modifying the library? (as I don’t need to use the accelerometer) I’ve tried to think my way round it like taking 2 readings, comparing them and whether the values are increasing or decreasing output the relevant direction signal. However with this the machine would just move the opposite direction whilst the reading ascends/descends to the zero value, whilst the joystick would be left in its normal position. Thanks in advance. Henry.

    • Hi Henry,

      I’m glad you liked the library.

      I didn’t see a problem with my x-axis, but it might be a problem of observation. Removing the accelerometer’s readings is not possible since all the data comes together from the controller. This shouldn’t be the problem.

      You could try reducing the delay in “delay(10);”. But be careful; if this value is too low, the controller might freeze. Try 8, 5, 3, … and see if you get a faster performance with enough stability. The value 10 is a safe value, so you should be able to reduce it. Let me know what you get after doing this. If you still have the problem, I’ll try to think of something else.

      Best regards,
      Gabriel Bianconi

      • henry89 says:

        Many thanks Gabriel, unfortunately it didn’t work. I think i should have clarified that i have been testing the speed of transition between values using your example, printing the values to the serial monitor and observing the result. If you haven’t had any issues it must be my Nunchuck I guess. I’m borrowing another one and will check, I’m not doing anything fancy in the program so I can’t really see it being anything else. Cheers and thanks again for the library it’s really cool.

  10. jc says:

    Wow, this works great! Thank you very much!

  11. Robert says:

    I Have tried to run the python program but it keeps saying that there is an error in the syntax and it highlights the 7 in th 2.7.2 at the top of the page, what should i fix

  12. lizard657 says:

    Hi,

    When I try to use this, I my serial monitor, set to 19200, shows this line over and over:

    19208 3072 171 4 4 0 1

    It continues to display these numbers even when I unplug both analog inputs or ground, but stops displaying numbers at all when I unplug the 3.3v wire.

    I’m using an arduino mega, and the nunchuck is attached the way that your tutorial says to.

  13. Rich says:

    Gabriel, many thanks for the library. I’m just getting started with Arduino and have learnt a lot by simply getting this to work. Now it’s up and working the implementation looks like it’s going to be straight forward to integrate into my own projects – great job, again many thanks. Another trick for a robotics club I’m setting up at college!

    FYI: I’ve using the Leonardo R3 and the SCL/SDA connections.

  14. pcgroup786 says:

    Hi,
    Im using the wii nunchuk to control a servo (Hubby servo). But I can’t even get to connect my wii nun chuck to arduino. It’s a project Im doing at university and I really need to do this. Please can you help me. I personally need some help.

  15. George says:

    Hi Gabriel,

    For some reason, your library does not work. I am running on an Arduino R3 with the Arduino IDE v1.0.3. I get the following errors:

    ArduinoNunchukDemo:15: error: ‘ArduinoNunchuk’ does not name a type
    ArduinoNunchukDemo.ino: In function ‘void setup()’:
    ArduinoNunchukDemo:20: error: ‘nunchuk’ was not declared in this scope
    ArduinoNunchukDemo.ino: In function ‘void loop()’:
    ArduinoNunchukDemo:25: error: ‘nunchuk’ was not declared in this scope

    • This errors occurs when the library isn’t imported correctly. The two most common causes are:

      a. You didn’t place the files in the correct folder. Did you place the ArduinoNunchuk folder (not the ArduinoNunchuk-master folder) in the libraries folder?
      b. Did you add #include “ArduinoNunchuk.h” to your code?

      Let me know if the error persists.

      Best regards,
      Gabriel Bianconi

      • John Varga says:

        Hi Gabriel,

        I’m getting the same results George has. Running Arduino UNO with Arduino IDE v1.0.3. All folders are as your responce to George’s comment. I get the following errors:

        ArduinoNunchukDemo:15: error: ‘ArduinoNunchuk’ does not name a type
        ArduinoNunchukDemo.ino: In function ‘void setup()’:
        ArduinoNunchukDemo:20: error: ‘nunchuk’ was not declared in this scope
        ArduinoNunchukDemo.ino: In function ‘void loop()’:
        ArduinoNunchukDemo:25: error: ‘nunchuk’ was not declared in this scope

        • Hi John,
          Can you show me the structure of your libraries folder?
          Also, are you using the demo script via the Examples > ArduinoNunchuk > ArduinoNunchukDemo menu?
          Best regards,
          Gabriel

          • John Varga says:

            Arduino\
            Libraries\
            ArduinoNunchuk\
            examples\
            ArduinoNunchuk.cpp
            ArduinoNunchuk.h
            Keywords.txt
            ArduinoNunchukDemo\
            ArduinoNunchuckDemo.ino

            /*
            * ArduinoNunchukDemo.ino
            *
            * Copyright 2011-2013 Gabriel Bianconi, http://www.gabrielbianconi.com/
            *
            * Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
            *
            */

            #include
            #include
            #include “ArduinoNunchuk.h”

            #define BAUDRATE 19200

          • Hi John,

            The folder structure seems correct. Just make sure that in the preferences windows (Ctrl + comma) this is the folder selected as the workspace folder.

            However, you shouldn’t have those empty #include line. My ArduinoNunchukDemo.ino file looks different from yours. Try redownloading the library.

            Let me know if this fixes the problem.

            Best regards,
            Gabriel Bianconi

          • John Varga says:

            Gabriel,
            When I place your folder in the Parent Library (C:\Program Files (x86) \arduino-1.0.3\Libraries) folder it works. When I place it in the ” C;\User\John\My documents\ arduino \ libraries” folder, it doesn’t compile………… How do I get this website to include words it assumes are misspelled?

  16. Michelle says:

    Hi I am doing a science project and my goal is to use a mindflex headset and convert it to a computer cursor and I can’t seem to find the right code to allow the headset to read the code and move the cursor up, down, right, and left. I was wondering if your library would help convert my headset into a cursor. I am using the arduino uno and i know that the Arduino Uno chip can do a lot of tasks, but I need the right code in order for my arduino to work. I would really appreciate the help, thank you.

    • Hi Michelle,
      This library won’t (probably) work with your headset unless it is adapted. However, I don’t have one and I cannot check how it works. It is probably possible to interface it with the Arduino though.

      Good luck with your project!

  17. raed says:

    R3 I think…

    when it’s plugged in i get no values displayed .. when i unplug it .. I get the line I pasted above ..

  18. Ian says:

    Hey there Gabriel,

    Great job on the library. Do you know (or anyone else reading this) if the NYKO Kama wii mote works with this library?

    It seems like analog X is okay, Y doesn’t update correctly, Yaw works fine, but accel Z doesn’t up date and it looks like C and Z always read 1.

    I’m using a nunchucky breakout board with an ardunio UNO board with the 3.3 V output wired to the 3.3 V on the arduino, the ground into ground, clock into A5 and Data into A4.

    http://blog.makezine.com/2009/06/01/nunchucky-i2c-breakout-board/

    I would like to drive a continuous servo using C and Z to control direction and moving the controller back and forth (or side to side) to control the speed of the servo.

    Right now i can do all that using analog X, But i want to use the Joy stick later on to control two other motors.

    My project is a electronic focus for a telescope (Just like this guys project http://youtu.be/BpAVxB1s2g0). I then want to the joystick as a manual control for my motorized telescope.

    Thanks for taking the time to read my comment.

    • Hi Ian.
      This library was designed for the original nunchuk, but works with some unofficial controllers too. I don’t have a Nyko Kama controller, so I can’t test it myself. However, I think that someone has already mentioned using it in previous comments.

      After a quick search, I saw that your controller has a different I2C pattern. Check this page: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck

      Maybe you could try adapting my library with this information.

      Let me know how it goes! Good luck with your project.

      • ian says:

        Hi again Gabriel,

        Thanks for the link to the wiibrew page. I ordered an official Nintendo Wii Nunchuck from a local comicbook shop, but if they don’t come through i’ll try modifying your library to work with the Nyko one i have.

        I have a quick follow up question for you.

        As i said before, i plan on controlling my motorized telescope using the joystick and the arduino via RS-232 commands that the telescope accepts.

        In order to do this, i’ll have to buy this RS-232 shield.

        http://www.amazon.com/CuteDigi-ARDUINO_RS232-RS232-Shield-Arduino/dp/B003YF1IE0/ref=sr_1_1?ie=UTF8&qid=1357534248&sr=8-1&keywords=CuteDigi

        and using the info from this page to program the arduino.

        http://arduino.cc/en/Tutorial/ArduinoSoftwareRS232

        I was wondering if this shield would conflict with your library or if you have any general advice of issues i might run into.

        Thanks again for your help. I’ll be sure to mention your page once i finish my project and do a write up of it on inscrutables.com.
        -
        Ian

        • Hi Ian,

          As long as the Arduino is still able to communicate with the Wii Nunchuk via the I2C protocol, there shouldn’t be a problem. From the links you gave me, it seems like the RS-232 shield doesn’t use the I2C protocol at all, so I believe that it should be fine.

          Good luck with your project! Be sure to send me the link so I can check it out. Let me know if you need anything.

          Best regards,
          Gabriel Bianconi

          • Ian says:

            Hello again Gabriel,

            So i finally got around to buying that RS 232 shield.

            I have the set the serial baud rate for the shield to 9600.

            It looks like in the nunchuck codes, the serial baud rate needs to be 19200.

            Do i need to use something like SoftwareSerial to set a baud rate for the nunchuck separately from the RS 232 shield?

            Thanks,
            -
            Ian

          • Ian says:

            Actually scratch that.

            I think i just realized that the serial command in the example is just for printing out the readings from the nunchuck.

            Silly me.

          • Hey Ian,

            Yes, as you’ve pointed out, the baud rate shouldn’t be a problem for you.

            When you complete your project, feel free to post it here.

            Best regards,
            Gabriel Bianconi

          • Ian says:

            Hey Gabriel,

            I’ve finally figured out out to send the proper serial codes to my telescope to get it to move using the arduino.

            I’ve tested things out by writing sketches to move each axis, combined motion, etc so i am confident on how all that works.

            But now i have a bit of a general programming problem to figure out.

            Maybe you or other readers of this blog can help out.

            I was repurposing some code i wrote to control a continuous rotation servo with a wii nunchuck based off of this tutorial

            http://arduino.cc/en/Tutorial/Sweep

            What i want to do is if the value, val_Y, of the analog stick is in a certain range, send a move code through serial.

            I then want the arduino to wait until i have moved out of the certain range before sending another command.

            For example. If i move up on the analog stick and hold the stick up, the arduino should send a “up” move command once. When i release the stick, the arduino should send a stop command once.

            If the arduino constantly sends a command while the analog stick is in a given range, the telescope handset freezes up.

            I tried doing this with the following nested while loops, but it only runs once before getting stuck. If i remove the nested while loop, it sends the commands constantly.

            void loop(){
            nunchuk.update();
            cPress = nunchuk.cButton;
            if (cPress==1){
            val_Y = nunchuk.analogY;
            while (val_Y>=0 && val_Y<65){
            cmd[2]=17;
            cmd[3]=37;
            cmd[4]=9;
            for(i=0;i=0 && val_Y<65){
            val_Y = nunchuk.analogY;
            }
            }
            while(val_Y==138 || val_Y==139 || val_Y==140){
            cmd[2]=17;
            cmd[3]=36;
            cmd[4]=0;
            for(i=0;i<8;i++){
            Serial.print(cmd[i]);
            }
            while(val_Y==138 || val_Y==139 || val_Y==140){
            val_Y = nunchuk.analogY;
            }
            }
            }
            }

            (Here i've only included the analog stick down and analog stick at rest nested while loops for brevity. I have more loops for different analog stick ranges.)

            Would it be better to code this as a switch case structure with nested while loops?

            Thanks again for all the help!

          • Hi Ian,

            You could create a variable that would track the state of the joystick, e.g. moving_up = true/false. Here’s some pseudo-code that implements this approach:

            moving_up = false

            void loop() {
            if(analog_y is up and moving_up is false) {
            send_commands
            moving_up = true
            } else if(analog_y is not up) {
            moving_up = false
            }

            Let me know if this works.

            Best regards,
            Gabriel

          • Ian says:

            Hi there Gabriel,

            That did the trick!

            My code is up and running.

            I made a quick demo video that is linked below:

            http://youtu.be/EIsFXXYieJU

            I’ll do a detailed write up after i do some real world testing and after i make an enclosure for the whole thing.

            Thanks again for all your help. It’s amazing that you take the time to read everyone’s comments and help out all those who use your library!

          • Hi Ian,

            I’m glad that it worked for you. Feel free to share you project when it is done!

            Gabriel

  19. read says:

    I always get these values when outputting … can you please tell me what i’m doing wrong ??
    -25082 11297 1024 1024 0 1

    it’s kind of urgent … thx

  20. kEN says:

    Nice clean library. Bet you didn’t realize how much tech support your project was going to get you into. No good deed goes unpunished.

    • Thanks. I wouldn’t call it “punishment.” In fact, I’m glad that people are using my library in their projects. Also, these discussions with other people allow me to improve my project. So, yeah, I’m happy that people are commenting!

  21. gubaguGubagy says:

    Hey Gabriel,
    Just wanted to let you know that your library works great thanks for sharing.

      • Guby says:

        Hey Gabriel,
        I forgot to mention my Nunchuck is wireless one http://www.agknordic.com/index.php?side_id=444. I got everything working. Just have to figure out where to add Nunchuk controll :D
        Lately I am playing with Raspberry Pi, have you seen/heard anyone using your library on the RPI ? C++ is not my strongest skill?

        Take care and keep up the good work.
        Cheerz Guby

        • Hi Guby,
          This library works with some unoriginal nunchuks, but not all. This is why I recommend people to get Nintendo’s. However, I’m glad that it worked for you.

          I don’t have a Raspberry Pi myself. In fact, I was trying to get one, but I couldn’t find it for sale. It always says “Sold Out”. Where did you get yours from? Sorry, but I haven’t looked much into RPI, so I don’t know how to use my library with it. If one day I get one, I can try to port it to C++ (also not my greatest skill, but a challenge is always good!).

          Best regards,
          Gabriel Bianconi

  22. Macoh Musada says:

    I downloaded your library with the sample. when i load the sketch to my board it says “ArduinoNunchuk does not name a type”. How can i fix this?

    and also. will this library work with the wireless nyko kama nunchuk?
    THANKS IN ADVANCE

    • Hi Macoh,
      This happens when the library was not installed correctly, or when you did not import the library into your sketch. Do you have the following on your sketch (at the top)?

      #include “ArduinoNunchuk.h”

      If you do, try reinstalling the library. Make sure you place it in the correct folder.

      Let me know how it goes.

      Best regards,
      Gabriel Bianconi

      • Ross says:

        Hello Gabriel. Very impressive work. Thank you for sharing. I too have the same issue mentioned above, and yes I do have #include “ArduinoNunchuk.h” in my sketch.

        When extracting or moving the library to the folder, i get an error saying the library cannot be used due to special character in the name. I removed the “-master” from the title and imported it. I still get the does not name a type error. I am not sure how to correct this. Thank you for your time…..

  23. BobLynas says:

    Gabriel, I am just about to start using your brilliant library on a project, but I have seen it can only read ONE nunchuck. Is there any way I can read TWO nunchucks ? my project requires a nunchuck in each hand for it to work correctly. Thanks In Advance, Bob

    • Hi Bob,
      I’m glad that the library was useful to you.

      It is possible to do that, but it won’t be so easy. The problem is that both nunchuks will have the same I2C address, so it won’t be possible to differentiate them with the library.

      One solution is to use two Arduino boards, or another external micro-controller, and make them communicate with each other. Each of them would control one nunchuk.

      Another solution is to make a “switch” between them. This would be more complicated. Picture this: you have nunchuks A and B. First, you setup nunchuk A and then read its data. Then, you unload it, setup nunchuk B, and read its data. Then you repeat this. This can be done in the circuit with a transistor.

      If I were to do this, I’d use an Arduino to control a nunchuk and an ATMega328P micro-controller (the same as in your Arduino, but costs ~5 dollars). They can communicate with each other easily (I2C, TX/RX, analog pins, …).

      If you find another approach, let me know.

      Best regards,
      Gabriel Bianconi

  24. Bas Kasteel says:

    Hi Gabriel, Just came across your site.Nice. I have a very nice connector available for the wii and arduino. I have called it wiikend. You can find it http://www.fact4ward.com/blog/wii/wiikend/

  25. John Nank says:

    Hello Gabriel-

    I am still kinda new at this, but so far yours is the only code that has worked for me. I am making an animatronic halloween prop pan/tilt head (see quick vid):

    http://www.youtube.com/watch?v=duqM3DZileQ&feature=plcp

    In the video, I am using a wired parallax thumbstick, but would like to use my wireless Nyko Kama nunchuk instead. Your code works (servos move) with my chuk, but how can I get the servos to self center when I release the stick, and respond a little quicker? (I don’t need the accelerometer, or the velocity of stick- just simple servo rotation). I think I’m a little (a lot) over my head here…

    Anyhow, Thank You for your time and code!
    John

    • Hi John,
      Thanks for commenting. You could record the x and y values for the stick when it is centred. Then, you could use these values as references to see when the servos must return to their initial position. Basing on those values, you could do what you’re looking for without problems. Regarding the response rate, there isn’t much you can do I think. You could try to increase the baudrate, which may help. Also, I don’t know if this unofficial version of the Nunchuk has a slower response. I only have an official nunchuk and the response is fast (for my previous projects).

      Let me know if you still have problems.

      Best regards,
      Gabriel.

  26. Elijah Rosas says:

    Hey Gabriel. Just wanted to say thanks for providing this awesome library. This is a project that I did using your library and editing some of the code… Let me know if you would like me to provide more information on this… Check it Out!

    • Hey Elijah,
      Nice project, thanks for submitting it! Which Nunchuk model are you using? Did this wireless model work with the library without problems?
      Best regards,
      Gabriel.

      • Elijah Rosas says:

        This wireless nunchuk was made by Playtech, which I got from ebay. I did not have to modify the library for this nunchuk. I did have to use the adapter from FunGizmos as you mentioned. I previously tried to connect it with an extension, which didn’t work for me.

        I still have trouble knowing how the library (ArduinoNunchuk.cpp) works. Is there anything I can refer to for this?

  27. John Ingram says:

    It works very nicely. Thank you very much for your efforts and your clean implementation.

  28. Chris says:

    I’m following the Livefyre blog for your nunchuk library, After the last email update I figured I would stop in and catch up on what I missed on the postings. But I can’t find the postings.
    Can you post me a link to the blog?
    Thanks
    Chris

    • Hi Chris,
      Nice to know that you’re following my blog! Yesterday I changed from Livefyre to Disqus. I transfered the old comments to this new system, but the admin panel says it takes up to 24 hours for the changes to take place. This is why you can’t find the posts. If you visit this page again tomorrow or later today, the comments will be here (if no problem occurs).
      Best regards,
      Gabriel Bianconi.

  29. amithgc says:

    Hi GB,
    I try’d ur library.. on my Arduino uno R3
    connections from nunchuck are as
    Red to 3.3V
    White to GND
    Yellow to A5
    Green to A4
     
    and when i upload and run the code..
    the execution hangs at
    ArduinoNunchuk::_sendByte(0×55, 0xF0);
     
    what can be wrong?? :(
     

    •  @amithgc The Arduino Uno R3 has dedicated SCL/SDA ports. Instead of using A4 and A5, use these ports, like in an Arduino Mega. Let me know how it goes.

      • amithgc says:

         @GabrielBianconi I tryied that too… and it didnt work…
        but the same thing works on my Bread Board Arduino and not on my Arduino Uno R3, don’t know whats going wrong… 
        but anyways thanks for the great library…

  30. HansEirikBull says:

    Hi!
    Your library is great! Thanks a lot! :D
     
    But is it possible to change the SCL and SDA ports from A4-A5 to other ports?
     
    Greetings!

    •  @HansEirikBull Hi Hans. It is not possible in Arduino UNO R2 or older, unless you change the microcontroller or Arduino model (these ports are set in the microcontroller that your Arduino UNO uses).  I’m sorry. In the UNO R3 or in the Mega, there are dedicated ports for this. 
       
      Best regards,
      Gabriel.

  31. GaryAylward says:

    Hi Gabriel
    This worked “straight out of the box” for me. :-)
    It is the first step on my way to building a self-balancing robot. Now I need to integrate the Wii Motion Plus gyros and I think your library will be a very good starting point.
    Many thanks for your code.

  32. JaredAnderson says:

    I haven’t been able to update the code to my project since arduino 1.0 came out.  The new IDE just screwed everything up. . . Until now.  I cant give you enough thanks.  This is helping out a ton!  I used to have two separate code setups for both wired and wireless nunchuks.  Now they both work on the same code!!  Pretty awesome.  Anyways, I’m in the process of transferring my old code to your backbone.  Serial Monitor is working great.  In my old code I used Bitread to change the status of relays switched by C and Z buttons.  But know I cant seem to get any status change at the relays.  Is there an example of this being done with your library so I can make sense of what I’m doing wrong? 

  33. JFAVIDAL says:

    Hi Gabriel,
     
    I0m trying the example  ArduinoNunchukDemo, but when I verify I have this message:
     
    ArduinoNunchuk_Demo.cpp.o: In function loop': C:DOCUME~1FREDER~1LOCALS~1Tempbuild1178972498780425002.tmp/ArduinoNunchuk_Demo.cpp:28: undefined reference to ArduinoNunchuk::update()’ ArduinoNunchuk_Demo.cpp.o: In function setup': C:DOCUME~1FREDER~1LOCALS~1Tempbuild1178972498780425002.tmp/ArduinoNunchuk_Demo.cpp:23: undefined reference to ArduinoNunchuk::init()’

    •  @JFAVIDAL Hi,
      Make sure that you’ve followed the setup steps correctly. Have a look at the other comments, they may be able to help you too. Let me know if you manage to fix it.

  34. darkside40 says:

    Hi Gabriel,
    first of all thanks for the Library, helped me a lot controlling my old Asuro robot via an IR Diode and a Nunchuk. Just one other question, do you plan to extend your Library to support the Wii Motion Plus Dongle. That could be a great extension to it.

  35. AdiSoffer says:

    Hey Gabriel, thanks for sharing this. Do you think it’s possible to code the Arduino to store the data and than play it back at the exact same rate again and again? I’m trying to control a camera and record the movement – and than repeat it.

    •  @AdiSoffer I believe it is possible, yet I don’t know exactly how. The memory of your Arduino board depends on the model. Some models (like the Mega) have a lot of memory, so you could try to fit in that. Also, you could use an external memory card. I think that you should start by saving the values in a variable (array). For example, save the position of the camera every second. Then reiterate the array and move the camera again. Let me know if you managed to fix this.

      • AdiSoffer says:

         @GabrielBianconi Thanks GB, since I’m new at all this Arduino stuff I’m still learning the basics. I figured I could store into arrays, but can’t figure out a way to read the data at the same rate it was entered – thus maintaining the movement and the tempo of the movement. Right now I’m trying to introduce this data storage and playback into the Knob sketch as a way to pull focus on a camera and “replay” that focus pulling at the same rate with simple push of a button. 

  36. Soffer says:

    Hey, great project and thanks for sharing. Here’s my question: Is it possible to “record” the Nunchuk data and that play it back at the same rate from the Arduino? What I’m trying to do is control a camera with this device and than make the camera repeat that motion again and again…

  37. Tim says:

    Hi, i have never used a nunchuk with my arduiono before, ho would I wire it to my UNO?

  38. Elijah says:

    I am new to the arduino and wanted to know where to wire the SDA and SCL wire of the nunchuck to the arduino is it analog pins 0 and 1?

    •  @Elijah If you are using an Arduino UNO, you should use Analog In pins 4 and 5. If you are using an Arduino Mega, there are dedicated SCL and SDA pins. 

      • Elijah says:

         @GabrielBianconi Thank you for your response. So I have wired everything, however, I can’t get the system to work with the code. I currently have LEDs hooked up to the outputs and they don’t seem to react to anything. Should I modify the code in anyway? Please let me know.
         
        So eventually I want to build a remote control car with a tracked chassis controlled by wii controller everything is slowly coming  together, just need to figure out this wii controller before I can continue.
         
        Any advice will help.
         
        Elijah

      • Elijah says:

        @GabrielBianconi Thank you for your response. So I have wired everything, however, I can’t get the system to work with the code. I currently have LEDs hooked up to the outputs and they don’t seem to react to anything. Should I modify the code in anyway? Please let me know.
         
        So eventually I want to build a remote control car with a tracked chassis controlled by wii controller. Everything is slowly coming  together, just need to figure out this wii controller before I can continue.
         
        Any advice will help.

        •  @Elijah Are you using an original (made by Nintendo) Wii Nunchuk?

        • Elijah says:

           @GabrielBianconi  I have tried using both an original nunchuck and a generic nunchuck. Does the code have to be modified? Also, I am using an extension cable to connect it to the arduino. I believe I do have it wired correctly. There is an extra brown wire that I believe is a NC.

        •  @Elijah The code doesn’t need to be modified for the original nunchuk. Does something show up in the Serial Monitor when you run the sketch?

        • Elijah says:

           @GabrielBianconi Sorry its been a while. I just found out how to access the serial monitor (I’m still a noob) I do not see anything while I am running the sketch. What should I do?
           
          Thanks!
           

        •  @Elijah Try to complete this check list:
          - Are you using an original Wii Nunchuk?
          - Are you are connecting correctly (note that there is a difference between UNO and MEGA)?
          - Are the cables are well connected and not loose?
          - Did you upload the correct sketch? Did you get any errors?
          - Did you set the correct baud rate in the serial monitor (the same as in the sketch, if you did not change it is 19200).
           
          Good luck!

        • Elijah says:

           @GabrielBianconi THANK YOU SOOO MUCH GABRIEL!!! I went back to the drawing board and bought a different adapter and it works great! I will definitely post my project. :D i was testing this with an LED now i can build my RC car…

        • Elijah says:

           @GabrielBianconi I’ve been trying to control a servo motor with the nunchuck l but I’m not sure how to do this. I’ve tried integrating the servo library with your code but have not got this to work. Do you have any suggestions?

        •  @Elijah I haven’t worked with servos yet (I have bought some, but I did not have enough time to search about them). What I suggest is to get the accelX value and try to map it to the servo function (search for the map() function). Let me know how it goes.

        • Elijah says:

           @GabrielBianconi Actually it sort of works as is… I just forgot to put the servo ground to the arduino ground. It actually rotates now :) . I will research more on why it works. I thought servos worked off of pulse signals not voltage… hmmm… Thanks!!!

      • MikeDX says:

         @GabrielBianconi FINALLY! After 4 days of scouring the internet, i finally find why my mega hates my nunchucks! Thanks Gabriel! :)

  39. Chad says:

    Awesome software!
     
    I’m trying to do something simple (well at least I think so) of just turning on a LED when the analog stick is pressed in a certain direction but I’m not really sure how to go about doing that.  I’ve tried something like :
     
    if(numchuk.analogY > 0) {
    digitalWrite(12,HIGH);
    }
    else {
    digitalWrite(12,LOW);
    }
     
    but that didn’t seem to work.

  40. John de Kuyper says:

    Gabriel this is a great piece of software made simple. Only I have some problems with running lib twi.c which I can’t figure out please help. (arduino romeo 1.0 board, pins 4, 5 and 7 used) errors: In file included from K:arduino-1.0librariesWiretwi.c:38: K:arduino-1.0librariesWire/twi.h:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token K:arduino-1.0librariesWire/twi.h:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘can’ K:arduino-1.0librariesWire/twi.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘version’ K:arduino-1.0librariesWire/twi.h:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘even’ K:arduino-1.0librariesWire/twi.h:17: error: expected identifier or ‘(‘ before ‘if’ K:arduino-1.0librariesWiretwi.c:49: error: ‘TWI_BUFFER_LENGTH’ undeclared here (not in a function) thanks John de Kuyper

    •  @John de Kuyper I don’t know what’s causing this. Can you explain your sketch with more details? What are you trying to do? Which Arduino version are you using? Which board model?

      • John de Kuyper says:

         @GabrielBianconi 
        Found  that the wire.h lib was not the right one it’s working fine now.
        I was trying to run a rover with the nunchuck it’s working fine next is to use the BT wii remote as simple as your nunchuck code!
        Tanks for your good work!

      • John de Kuyper says:

         @GabrielBianconi 
        I am using a Arduino Romeo and a need sketch to let this nunchuck rover work?
        pin 5;  // analog 0-255  speed
        pin 6;  // analog 0-255  speed
        pin 4;  // digital forward/backward
        pin 7;  // digital forward/backward
        if my nunchuck is in neutral analogX 124 and analogY 122 and should work like
        every joystick. I tried the if  .. < 124 then analogWrite but is slow in reaction.
        Anything on the shelf?
         
         
         

  41. Andrew says:

    I love how clean this is… however I cannot get it to compile using 0022 or 0023 to save my life. Keep running into the following error:
     
    Z:ArduinolibrariesArduinoNunchukArduinoNunchuk.cpp: In member function ‘void ArduinoNunchuk::update()’:
    Z:ArduinolibrariesArduinoNunchukArduinoNunchuk.cpp:45: error: ‘class TwoWire’ has no member named ‘read’

  42. christiangoff says:

    Sir, you have made my project amazing. I’ll send you a link to what I have built. Quick question though. How easily adaptable do you think your library would be for the Wii classic controller?

    • @christiangoff I’m glad that you managed to finish your project! I’d be very interested in seeing it.

      You could try to port the library using the information from: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller

      I don’t have a Classic Controller, so I can’t create a library for that. If I had one, I’d try to create it. Maybe in the future I’ll buy one.

      Good luck with your projects!

      • christiangoff says:

         @GabrielBianconi excellent!  I am almost done porting over the code (which of course I’ll give you a tested copy to do with as you see fit).  I am having trouble with the analog values.  I can’t seen to figure out the logic behind your multipliers.  Looking at the data sheet you sent me there are numbers next to the input ( like LX ).  I can’t figure out how that applies to the analog values.  How did you come up with your ” *2 ” multipliers in your library?

      • christiangoff says:

         @GabrielBianconi Disregard that last comment.  I feel stupid.  The Classic controller library is finished and verified if you would like a copy.

  43. Hi Gabriel,

    Thanks for that great job!

    I’m starting a basic servo project using the Wii Nunchuk and I’m facing a problem:

    I use your good library and all works fine if I keep the line “Serial.begin(19200)”, but if I remove this line it seems that the received data is not right (that works for buttons but not for values of accelerometer and stick). :(

    May be I’m wrong, but the I2C has got it’s own baudrate generator and I don’t understand why I have to set the Serial if I don’t need it (I don’t send any data to serial). Is there any relationship between Serial and Wire?

    Many thanks for you help.

    • @EricQuagliozzi My library works without “Serial.begin(19200)” if you do not print anything to the serial port. For example, the following sketch works fine:

      #include

      #include

      ArduinoNunchuk nunchuk = ArduinoNunchuk();

      void setup(){

      nunchuk.init();

      pinMode(13, OUTPUT);

      }

      void loop(){

      nunchuk.update();

      if(nunchuk.accelX > 530) {

      digitalWrite(13, HIGH);

      } else {

      digitalWrite(13, LOW);

      }

      }

      When the Nunchuk is rotated right, the led turns on. When it is rotated left, it turns off.

      You could use this script to debug your sketch. The problem is probably with your sketch.

      Please let me know if you need more help.

      Best regards,

      Gabriel Bianconi.

      PS: @ccweb1 this may interest you. I’m still working on your problem, I’ll answer your comment soon.

    • @EricQuagliozzi

      My library works without “Serial.begin(19200)” if you do not print anything to the serial port. For example, the following sketch works fine:

      http://pastebin.com/z7xJJbLX

      When the Nunchuk is rotated right, the led turns on. When it is rotated left, it turns off.

      You could use this script to debug your sketch. The problem is probably with your sketch.

      Please let me know if you need more help.

      Best regards,

      Gabriel Bianconi.

      PS: @ccweb1 this may interest you. I’m still working on your problem, I’ll answer your comment soon.

  44. ccweb1 says:

    I’m trying to come up with a solution to my problem; should I be able to add a simple blink script to your demo and have the led on pin 13 flash as well as have the nunchuk values print out serially? I would think this would be very possable, but when I try to do this nothing happens. When I put “//” in front of

    nunchuk.init(); (in the setup), the blink function works just fine. So back to my other post, do you think it has something to do with my nunchuk not inishalizing? Or hanging up the sketch?

  45. ccweb1 says:

    Ok, I rounded up all the pieces to the puzzle and my first attempt to make it all come together didn’t go as I would have hoped.

    I’m using an Arduino Uno Purchased recently,

    Arduino 1.0 software,

    a wiichuck adapter,

    a Nintendo Wii Nunchuck,

    When I upload your sketch I see the Tx and Rx leds flash while uploading then they stop.

    After uploading I open the serial monitor and it’s blank, if I unplug the nunchuck the Tx led flashes continually and I get values similar to Mikael and others;

    “20486 -32248 1024 1026 -21466 1 1″ that continually repeat, if I plug the nunchuck back in the serial communication stops. Nothing I do to the nunchuck provides any data on the com port.

    I referenced Mikael’s post and my connections are the same

    - to gnd

    + to 3.3

    d to A4

    c to A5

    I have referenced your responce to earlier posts and I am almost positive I am hooked up correctly, But would be willing to post pics.

    I made sure the nunchuck was connected when i powered up the Arduino Uno Board…

    Are you supposed to see Rx and Tx communication on the leds while the program is running? (I am not.)

    Any suggestions would be a big help,

    Thanks

  46. SebastianZenobio says:

    This library, run with Nunchucks wireless?

  47. SebastianZenobio says:

    ok perfect :D , tnks!!!!!!. One more question, in the library where configure A4 and A5 ??

  48. SebastianZenobio says:

    Tnks for your response, I refer to the ports used by nunchuck in library

  49. SebastianZenobio says:

    Where Sets the ports to be used??

  50. MikaelPratama says:

    Thank you for your library… It seems will help me a lot.

    I have a problem like several person below.. It keeps send this value to serial 20486 -32248 1024 1026 -21466 1 1

    I am using Original Nintendo Wii Nunchuk.

    I newest Arduino… (just bought it October 2011).

    I connect ‘+’ to 3.3V,

    ‘-’ to GND,

    ‘c’ to A5,

    ‘d’ to A4.

    Would you mind if you help me?

  51. MikaelPratama says:

    Thank you for your library…It seems will help me a lot.

    I have a problem like several person below..It keeps send this value to serial 20486 -32248 1024 1026 -21466 1 1

    I am using Original Nintendo Wii Nunchuk.

    I newest Arduino… (just bought it October 2011).

    I connect ‘+’ to 3.3V,

    ‘-’ to GND,

    ‘c’ to A5,

    ‘d’ to A4.

  52. ccweb1 says:

    Wow, this library is great. Just what I have been looking for. I have tried other libraries and there examples, but they wont compile. I’m thinking it is because I’m using Arduino 1.0.

    I am new to Arduino but feel I’m picking it up very quickly, but know I have lots to learn in the code writing department. I am working on a project and would like to integrate a wii nunchuck. I intend to make my own nunchuck adapter, but for now I don’t have the time so I ordered one. Should be here soon. I would like to get started on my code before it gets here.

    So my question(s) is/are; what are the input values/ranges for the nunchuck axises? I’m assuming z button and c button are 1 or 0(is this correct?), but what are the accel and joy ranges? I see Adrian (in his post) listed a rang of 400-650, and Gabriel mentions 0-1024.

    If using servos, is the best method to use the map function or write a task function similar to that of todbot?

    Example : http://todbot.com/blog/bionicarduino/

    downloaded nunchuck sketch

    float tiltx = nunchuk.accelX(); // x-axis, in this case ranges from ~70 – ~185

    tiltx = (tiltx – 70) * 1.57; // convert to angle in degrees, roughly

    accelpulseWidthLR = (tiltx * 5.6) + minPulse; // convert angle to microseconds

    Also do the ranges change if your using a third party nunchuck vs. a Nintendo nunchuck?

    Thanks

    ccweb1

    • @ccweb1 I’m glad you liked the library :D

      Theoretically, the values for the accelerometer would be in the 0-1024 range. However, the values have a smaller range, like Adrian said. I believe that these values change from controller to controller, so I think that the best solution would be to test them using your own controller. Regarding the buttons, 0 and 1 are the only possible values.

      I also plan to work with servos and an Arduino Nunchuk. I’ve ordered some servos, but they haven’t arrived yet, so I can’t really help you with this right now. When I get them, I’ll test some scripts and I’ll let you know what works best for me.

      When you complete your project, post it here. I’d like to see it! Thank you for visiting!

    • Adrian Martinez says:

      @ccweb1 The ranges I used were the ranges that were printed to serial port using the example GabrielBianconi provides. Regarding the map function, I’m still pretty new to Arduino and map() was the only way I know how to use the accel/analog ranges to control the servo. I haven’t figured out the twitch issue I’m having, I’m curious to see if you have the same problem. Keep us posted.

  53. darkside40 says:

    First of all thanks for your work.

    But i got a problem with your library. I use an Arduino Uno R3, a Wiichchuck Adapter (wired to PIns A2-3-4-5) and an origina Nintendo Nunchuck.

    Yesterday i just tried your demo program, but the funny thing is as long as the Nunchuck is connected the Serial Monitor stays blank. When i disconnect the Nunchuck many rows filled with 20486 -32248 1024 1026 -21466 1 1, when i reconnect the Nunchuck the Serial Monitor stops.

    To ensure that my Nunchuck works correct i tried the Demo made by todbot, and there i get Values when the Nunchuck is connected and the change when the Nunchuck is moved.

    • @darkside40 The difference between my library and todblog’s library is that mine does not use Analog In pins 2/3. You have to wire + to +3.3V and – to GND. Todblog’s library uses +5V. Although this works, it will reduce the life span of your Wii Nunchuk, which should only receive 3.3V. This is why I decided to use the other pins. It should work if you change this. Please let me know if that fixes your problem.

      • darkside40 says:

        @GabrielBianconi Thanks for the Hint, that did the trick. I just wired it up like it was mentioned on the todblog site. Maybe you should mention that tip in your instruction.

        Thanks for your work at this library.

  54. tgif says:

    I am using arduino 1.0 and an arduino Duemilanove board.

    I just installed your library and tried to compile your example program and got the following error messages:

    class TwoWire has no member named ‘read’

    class TwoWire has no member named ‘write’

    No modifications have been done to the standard Wire library.

    • This is wierd, since Wire.read and Wire.write were created in Arduino 1.0. Are you sure that you are using this version? Have you configured something to use an older Wire library or something like that? Many people, including I, are using Arduino 1.0 without problems.

    • If you manage to fix this, please comment here so that others with the same problem can see it. Also, if someone knows the answer for this, please let us know.

    • If you manage to fix this, please comment here so that others with the same problem can see it. Also, if someone knows the answer for this, please let us know.

    • christiangoff says:

      I am having the same issue. all I did was add the libraries and copy the demo code. Here is my error log.

      sketch_jan14a.cpp: In function ‘void nunchuck_init()’:

      sketch_jan14a:31: error: call of overloaded ‘write(int)’ is ambiguous

      C:Documents and SettingscgoffDesktoparduino-1.0librariesWire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)

      C:Documents and SettingscgoffDesktoparduino-1.0hardwarearduinocoresarduino/Print.h:49: note: size_t Print::write(const char*) sketch_jan14a.cpp: In function ‘void send_zero()’:

      sketch_jan14a:39: error: call of overloaded ‘write(int)’ is ambiguous

      C:Documents and SettingscgoffDesktoparduino-1.0librariesWire/Wire.h:55: note: candidates are: virtual size_t TwoWire::write(uint8_t)

      C:Documents and SettingscgoffDesktoparduino-1.0hardwarearduinocoresarduino/Print.h:49: note: size_t Print::write(const char*) sketch_jan14a.cpp: In function ‘void loop()’:

      sketch_jan14a:49: error: ‘class TwoWire’ has no member named ‘receive’

      As of Arduino 1.0, the Wire.receive() function was renamed to Wire.read() for consistency with other libraries.

      sketch_jan14a.cpp: In function ‘char nunchuk_decode_byte(char)’:

      any help would be appreciated christian.goff at gmail.com

      • @christiangoff It seems that you are using both my library and todblog’s library (since you have an error in nunchuck_init() function, which my library doesn’t have). You should only use mine (or his). Create a sketch and add only my files to it. I think that this is the problem. Let me know if this fixes it.

      • @christiangoff It seems that you are using both my library and todbot’s library (since you have an error in nunchuck_init() function, which my library doesn’t have). You should only use mine (or his). Create a sketch and add only my files to it. I think that this is the problem. Let me know if this fixes it.

        • christiangoff says:

          @GabrielBianconi

          Turns out you were right. in my haste to make it work, I was using two different libraries. Just now used only your files with success. Through my stumbling, however, I figured out the read/write issue that was bothering me as well. the new wires library requires wire.read instead of wire.receive and instead of wire.send (0×00) it needs wire.send (byte (0×00)).

          Thanks for your prompt reply. You have been a huge help in my project.

        • @christiangoff Hey Christian! I’m glad that it worked. Regarding the Wire library, I added support to Arduino 1.0 and tried to add to older versions too, but there is one problem which needs to be fixed (I’m working on this). But it should work with the latest version. When you complete your project, please send me a link to it. I’d be interested in knowing what you built.

      • @christiangoff It seems that you are using both my library and todbot’s library (since you have an error in nunchuck_init() function, which my library doesn’t have). You should only use mine (or his). Create a sketch and add only my files to it. I think that this is the problem. Let me know if this fixes it.

  55. Adrian Martinez says:

    Thank you for this library, I love it. I used the it to make a pan and tilt unit! I only have one problem. The servos(HITEC HS-322HD) twitch like mad. Check out the YouTube video I linked. I’m pretty sure its not the servos because when I upload the sweep example(Arduino1.0 on an ArduinoUno r2) they work smoothly. I’m not using an aftermarket nunchuk and as you can see I am holding it very still. It pans and tilts but very twitchy. What do you think is the problem?

    This is my loop

    nunchuk.update();

    val = nunchuk.accelX;

    val = map(val, 400, 650, 0, 170);

    myservo.write(val);

    Serial.print(nunchuk.accelX);

    delay(15);

    val2 = nunchuk.accelY;

    val2 = map(val2, 400, 650, 0, 179);

    myservo2.write(val2);

    Serial.print(nunchuk.accelY);

    delay(15);

    servoTwitch

    Thanks again

    • @Adrian Martinez I have an idea. The precision of the accelerometer is very high (0-1024), so a very small movement would change the value. So, you could try to divide its value by ten and then round the value. By doing this, the precision would be much smaller (0-124). Try this and let me know if it works!

      • Adrian Martinez says:

        @GabrielBianconi Thanks for the suggestion. Unfortunately I am still very new to programming and don’t know exactly where to reduce these values. I’m going to keep learning as much as I can(Google Search University). I’ll come back and post when I get it to work.

        • @Adrian Martinez You can do it like this.

          Instead of:

          val = nunchuk.accelX;

          Write:

          val = nunchuk.accelX/10;

          And instead of:

          val = map(val, 400, 650, 0, 170);

          Write:

          val = map(val, 40, 65, 0, 170);

          Do the same for val2. Let me know if this works for you.

        • Adrian Martinez says:

          @GabrielBianconi Still not working smoothly. I have tried many different configurations with the numbers. Now when I program the servos to analogX and Y it works perfectly. I’m still trying to find a solution to the accel issue.

        • @Adrian Martinez I bought some servos some days ago, but they haven’t arrived yet. I’m going to build something using my Wii Nunchuk and them, so if I manage to find a solution, I’ll post it for you. I don’t know how long it’ll take for them to arrive, since I’m importing them. I hope that they arrive in 1-2 weeks. In the meantime, if you find a solution, please post it here. I’m sorry, but I can’t help you more until they arrive.

      • christiangoff says:

        @GabrielBianconi@Adrian Martinez

        Your delay is set really low. it is sending a new position to the servo every 15 milliseconds. Combine that with the extremely sensitive accelerometer and it’ll twitch. change delay(15); to delay(100); and see if that helps. that will give 10 positions per second instead of 67.

  56. rhbgames says:

    Ignore previous comment. Our problem was that we connected to digital pins 4 and 5 rather than analog pins. Once we fixed that, we observed the expected behavior. Thanks again!!!

  57. rhbgames says:

    Thanks so much for providing this library! It seems pretty easy to use, but we have not yet been successful. We have an Arduino Uno and a standard Wii Nunchuck.

    When uploading the program and opening the serial monitor (set to 19.2) we see many rows of:

    20486 -32248 1024 1026 -21466 1 1

    Since it is scrolling by so quickly, it is hard to tell if anything is changing – but doing something simple like pressing/releasing the C and/or Z buttons never seems to change the final two values to anything other than “1 1″. I sense that we might be close, but just missing some key piece.

    I was assuming that if I pushed on the Nunchuck joystick I would see the x or y values change in a somewhat smooth manner – but perhaps I was mistaken.

  58. tomydb says:

    Hi Gabriel,

    I buy a nunchuk on DealExtreme. In the comments of the item, a user claims to have been able to control and explain how ( http://club.dealextreme.com/forums/Forums.dx/threadid.952046 ), but I could not set that in your code. I hope you can help!.

    Regards

    • @tomydb Hey Tomy,

      According to that website, the I2C address is different. Instead of using 0×52, you should use 0xA4 to initialize and 0xA5 to get values. I modified the ArduinoNunchuk.cpp file for you. You can find this new version here:

      https://gist.github.com/1562252

      Replace the file in my library with this one. Note that this version only works with the DealExtreme Wii Nunchuk. Let me know if this worked.

      Best regards,

      Gabriel.

      • tomydb says:

        @GabrielBianconi@tomydb Hello again,Thanks for the quick response and work to modify the code to me!Probe the code after update the declaration of _sendByte in the header file, since not compiled.Unfortunately, the only result I get is similar to the mike, the nunchuck is constantly responding: 20998 -31736 1024 2 -19418 1 0Is it possible that the nunchuk be broken?

        • @tomydb Oh, I forgot that I needed to modify something in ArduinoNunchuk.h too. I added the new file to that same link. I’m sorry for the problem, please try again and let me know if it worked.

        • tomydb says:

          @GabrielBianconi I try, but the same thing happens to me a mike, the results do not change.

          I’ts:

          20998 -31736 1024 2 -19418 1 0

          constantly. sorry for my english!

        • @tomydb Let me ask a few questions:

          - Which Arduino model are you using?

          - Are you using the two files from the link I gave to you?

          - Are you getting any errors, or just not getting the values?

          - Anything else that you think that is uncommon?

          - Do you have a Wii so that you can test whether the controller is working?

          - Do you have Skype or MSN? It could be faster to solve if we chat.

        • @tomydb Also, how did you wire the Wii Nunchuk to your Arduino?

        • tomydb says:

          @GabrielBianconi – Arduino UNO is the version of my Arduino

          - Yes, i’m using the new files.

          - Data coming out of the demo program is as follows and does not change: 20998 -31736 1024 2 -19418 1 0

          - I haven’t a wii to test :(

          - Yes, MSN: tomydb@hotmail.com, i’m online!

          - I opened the nunchuck, where are the wires on the board say which is which.thanks again

        • @tomydb I added you to MSN, but you are not online to me. This is probably a problem with MSN, sometimes it takes a while for invatations to appear to others. So, can you send me a photo of how you connect the Wii Nunchuk to your Arduino? Maybe the connection is wrong. I don’t see another reason if you are using the new files.

  59. Hi Mike,

    Are you using an official Wii Nunchuk or a 3rd party controller? Make sure the wiring is correct. How did you wire them? What is your Arduino model? Make sure that the wiring is not loose. Sometimes, when my wires are not well connected, I also get values like that. Are you using an adapter? Did you cut the cord? Are you inserting in the connector?

    Please let me know if you managed to fix it. If you couldn’t, answer those questions so that I can help you more.

    Cheers,

    Gabriel Bianconi

    • Mike says:

      @GabrielBianconi Still could not get it to work. Its a NYCO 3rd party controller.I am using a Arduino SMD R2. I cut the cord on the controller and connected the green wire to Analog Pin A4, Yellow wire to A5, Red wire to 3.3V Pwr, and White wire to GND. Thanks again for all your help.

      • Mike,

        It seems that the controller you are using does not work with this method. I don’t have one, so I can’t really test another method. In the following page, you can find some information about your controller:

        http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck#Nyko_Kama

        You should try to follow the instructions there.

        I recommend that you buy an original controller. You can find cheap ones on ebay. I hope that link can help you. If you have any success, please comment here so that other with similar problems can get more information.

        Good luck,

        Gabriel Bianconi.

  60. Mike says:

    Great Work! but I am having a problem with the data that is coming out of the nun chuck. None of the data is changing when I move the controller around. Don’t know what the problem might be do you have any suggestions? Data coming out of the DEMO Program is as follows and does not change 20486 -32248 1024 1026 -21466 1 1

  61. Gratz says:

    Hi! Very great work, man! But i have a problem: i’ve cut the cable (i haven’t a WiiChuck adapter) and i found 5 (FIVE!) wires… red, white, yellow, green, black. So i tried to connect red to 3.3V, white to GND, green to analogic pin 4, yellow to pin 5 but (obviously) it doesn’t work… have you got some ideas?thanks!

    • Hey Gratz,

      It should work like you did. However, if it is not working, I would need more information. What is your Arduino model (UNO, MEGA, …)? Are you sure you’ve set up the library correctly? Are you getting any errors? What do you get when you try to print the data?

      Cheers,

      Gabriel Bianconi.

      • I updated my library today, try the new version and follow the installation instructions above. The last version should have worked too, but maybe you weren’t using it correctly. If it still does not work, please answer the questions from the last comment.

  62. isaac says:

    was wondering if you could help me with some errors, im getting arduinochuk does not name a type,in function void setup(),nunchuk was not declared,in function void loop,xjoystick was not declared in this scope. oh and im new to all this so bear with me THANKS

    • Hey Isaac, I’m emailing you. Thank you for visiting.

      • corygrunk says:

        @GabrielBianconi Any solution to this? I’m having the same issue with ‘ArduinoNunchuk’ does not have a type.

        • @corygrunk Did you import the library correctly? If you place the library in the folder described in the FAQ section and restart the IDE, you should see a working example in File > Examples > ArduinoNunchuk > ArduinoNunchukDemo. Compare this example to your file and look for what is causing the problem. If this example file is not appearing, you haven’t installed the library correctly. Let me know if you managed to fix this. If this doesn’t fix your problem, let me know and I’ll think about something else.

        • corygrunk says:

          @GabrielBianconi@corygrunk I do see ArduinoNunchukDemo in the Examples dropdown menu. The only thing I did differently from the FAQ was to put the library in a different folder because my sketches are not saved in the ~/user/Documents/Arduino. I see the libraries and examples in the dropdown menus though, so I’m assuming everything is installed properly. Here’s a screenshot of my error: http://dl.dropbox.com/u/156187/public-images/wii-nunchuk-error.png

        • @corygrunk I see that you have the two other files opened. You shouldn’t open them. Try this: close the IDE, open it again and open the example file (don’t open any other file). It should only open the example file and it should work. Let me know if this was the problem.

        • corygrunk says:

          @GabrielBianconi@corygrunk Problem solved. I feel dumb, but the issue was that I copied the wrong ArduinoNunchuk folder to my library. The root Github folder was also called ArduinoNunchuk and I copied that initially instead of the folder inside it. Thanks for your help.

        • @corygrunk I’m glad you managed to fix it. :)

  63. Alex Benson says:

    Absolutely awesome library! Super easy to use!! Only thing I did differently than you is hooked pwr and gnd of the nunchuck to +3.3V and GND instead of analog pins. My nunchuck doesn’t work on the analog pins… maybe because it’s getting 5v? Great work!
    ~Alex

    • Hi Alex,

      Thanks for commenting! Actually, I hooked up like you did. This picture was only for illustrative purposes, but now I see that it may cause some confusion. I’m going to remove it and take a picture of my Arduino. I recommend the following:

      - (gnd) to GND

      + (pwr/vcc) to +3.3V

      d (I2C data) to pin 4

      c (I2C clock) to pin 5

      Note that if you are using an Arduino Mega, like I am, this should be done differently, like I explained in the following post:

      http://www.gabrielbianconi.com/blog/how-to-hook-up-wii-nunchuk-arduino-mega/

      Thank you for commenting and sorry for the confusion.

      Best regards, Gabriel.

      • Stu says:

        Thanks for the library. Works well, but for some reason the x axis is reversed, ( move nunchuck stick left, x axis, servo moves right)is there a way to reverse the x axis in the library? Thanks for your time.

        Here’s the sketch,
        #include
        #include
        #include

        #define BAUDRATE 19200

        #define DEADBAND 5

        #define X_MIN 29
        #define X_CTR 130
        #define X_MAX 227

        #define Y_MIN 41
        #define Y_CTR 120
        #define Y_MAX 228

        ArduinoNunchuk nunchuk = ArduinoNunchuk();
        Servo servoX;
        Servo servoY;
        Servo servoF;

        int dbVal = DEADBAND;
        boolean bFire = false;
        int xOld=0,yOld=0;
        int xVel=0,yVel=0;
        int servoXpos=90;
        int servoYpos=90;
        int ledPin = 13; // led to show trigger pulled
        int Elfire = 8; //For laser

        void setup()
        {
        Serial.begin(BAUDRATE);
        nunchuk.init();
        pinMode(Elfire,OUTPUT);
        pinMode(ledPin,OUTPUT);
        servoX.attach(10); // DIO pin 10
        servoX.write(servoXpos); // set it to centre
        servoY.attach(11); // DIO pin 11
        servoY.write(servoYpos); // set it to centre
        servoF.attach(9); // DIO pin 9
        }

        void loop()
        {
        nunchuk.update();

        // check SLO-MO button
        if (nunchuk.cButton ==1)
        dbVal=DEADBAND * 10;
        else
        dbVal= DEADBAND;

        //deadband the fire button for 100ms once pressed
        if (nunchuk.zButton==1) {
        if (!bFire) {
        bFire = true;
        Serial.println(“FIRE BUTTON PRESSED”);
        digitalWrite(ledPin, HIGH);
        digitalWrite(Elfire, HIGH);
        }
        } else {
        if (bFire) {
        bFire=false;
        Serial.println(“RELEASED FIRING BUTTON”);
        digitalWrite(ledPin, LOW);
        digitalWrite(Elfire, LOW);
        }
        }

        // update current X & Y velocity values
        // – not co-ords but relative ‘change’ value for each axis
        // – a ‘rate of change’ to feed to the servo – so the servo can move slowly or quickly
        xVel = getRelativeSpeed(nunchuk.analogX, X_CTR, dbVal);
        yVel = getRelativeSpeed(nunchuk.analogY, Y_CTR, dbVal);

        if (xOld != xVel) {
        xOld = xVel;
        Serial.print(“X change =”);
        Serial.println(xVel,DEC);
        }

        if (yOld != yVel) {
        yOld = yVel;
        Serial.print(“Y change =”);
        Serial.println(yVel,DEC);
        }

        // move servo angle relative to stick input
        servoXpos -=xVel;
        if (servoXpos180) servoXpos=180;
        servoYpos -=yVel;
        if (servoYpos180) servoYpos=180;

        //update servo position
        servoX.write(servoXpos);
        servoY.write(servoYpos);
        }

        int getRelativeSpeed(int absVal, int axisCentre, int deadbandValue) {
        // return a centred-relative positional value with optional deadband/resolution
        return ((absVal – axisCentre)/deadbandValue);
        }

        • Hi Stu,

          I guess that changing
          servoXpos -=xVel;
          to
          servoXpos +=xVel;
          would fix your problem, since this would invert the direction of motion.

          If this doesn’t work, let me know and I’ll look deeper into it.

          Best regards,
          Gabriel Bianconi

          • Stu says:

            Sorry, I have been away, thanks for taking the time to have a look at it. It works as intended now. It goes to show simple things can cause big headaches, still fairly new to the programming side of things. Thanks again, and thanks for the library.

            Stu

Leave a Reply