Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

M33_WiiNnchk_Opener

Today, more and more devices are using motion control. From tablets to cellphones to game consoles, people are getting used to interacting with electronics using gestures. Personal computers, however, have lagged behind a bit. Unable to find a suitable gestural controller for my Windows PC on the consumer market, I set out to build my own.

I based my design on the Wii Nunchuk controller for several reasons. First, it’s a versatile, comfortable, well-designed controller; second, it‘s cheap and easy to find; and third, its native I2C serial protocol is easy to interface with Arduino. As a bonus, the connector will accept standard jumper wires, so there’s no need to cut up the cable or use a dedicated adapter.

The Arduino runs a sketch that reads data from the controller and prints to the computer’s serial port. The computer runs a Python script, which receives serial data and emulates a mouse.

Materials

  • Nintendo Wii Nunchuk controller — $17; third-party controllers may not work.
  • Arduino Uno microcontroller — item #MKSP11 from Maker Shed (makershed.com), $35
  • 140mm male/male jumper wires (4) — item #MKSEEED3 from Maker Shed, $9/65

Tools

  • Windows PC — Mac/Linux users will have to adapt the Python script.

Web Materials

MAKE Volume 33 features our special Software for Makers section covering apps for circuit board design, 3D design and printing, microcontrollers, and programming for kids. Also, meet our new Arduino-powered Rovera robot and get started with Raspberry Pi. As usual, you’ll also find fascinating makers inside, like the maniacs on our cover, the hackers behind the popular Power Racing Series events at Maker Faire.

Try your hand at 22 great DIY projects, like the Optical Tremolo guitar effects box, "Panjolele" cake-pan ukelele, Wii Nunchuk Mouse, CNC joinery tricks, treat-dispensing cat scratching post, laser-cut flexing wooden books, sake brewing, growing incredibly hot “ghost chili” peppers, and much more.

On newsstands now, by subscription, or available in the Maker Shed

Buy now!

Steps

Step #1: Install the software.

PrevNext
Wii Nunchuk Mouse
  • Download the Arduino IDE for Windows at arduino.cc/en/Main/Software. Extract the arduino-1.0 folder to your hard drive. Inside this folder you’ll find arduino.exe.
  • Download the ArduinoNunchuk library at github.com/GabrielBianconi/ArduinoNunchuk. Extract the ArduinoNunchuk folder to your hard drive. Open the Arduino IDE and select File → Preferences. Note the folder under Sketchbook folder and open it in Windows Explorer. Look for a folder named libraries. If there isn’t one, you should create it. Move the ArduinoNunchuk folder into libraries.
  • Download Python 2.7.2 for Windows at python.org/ftp/python/2.7.2/python-2.7.2.msi and run the installer. You can find IDLE, the Python IDE, at Start → Python 2.7 or launch it at C:Python27Libidlelibidle.pyw.
  • NOTE: You should not download Python 3. The script used in this project won’t work with this version.
  • Download the pyserial module at pypi.python.org/pypi/pyserial. Extract the pyserial-2.6 folder to C:. Launch the command prompt window at Start → Accessories → Command Prompt. Type cd /d c:pyserial-2.6 and hit enter. Now type c:Python27python setup.py install and hit enter again.
  • Download pywin32 at makezine.com/go/pywin32 and run the installer.

Step #2: Connect the controller.

PrevNext
Wii Nunchuk MouseWii Nunchuk MouseWii Nunchuk Mouse
  • We’ll use 4 colors of jumper wires to simplify the instructions. Insert the jumpers into the Wii Nun-chuk’s connector as shown.
  • On your Arduino, connect the red wire to +3.3V and the black wire to GND. Connect the green wire to A5 and the yellow wire to A4.

Step #3: Upload the Arduino sketch.

PrevNext
Wii Nunchuk Mouse
  • Connect your Arduino to your computer via USB. If this is the first time, you’ll need to install the required driver. Windows will not be able to install it automatically, so you’ll need to select it in arduino-1.0/drivers.
  • Open the Windows Control Panel and search for the Device Manager. You can find the port in which your Arduino is inserted under Ports (COM & LPT). Open the Arduino IDE and select the correct port under Tools → Serial Port.
  • Relaunch Arduino. Select your Arduino model under Tools → Board.
  • The ArduinoNunchuk library that you’ve installed has an example sketch that prints the data from the Wii Nunchuk to the computer’s serial port. Open the exam-ple file by selecting File → Examples → ArduinoNunchuk → ArduinoNunchukDemo. Upload this sketch to your Arduino.
  • Open the Serial Monitor. At the bottom right, change 9600 baud to 19200 baud and wait a few seconds. You should see 7 columns of values that change if you move the Wii Nunchuk, move the analog stick, or press a button. If it’s working correctly, close the serial monitor.

Step #4: Set up the script.

PrevNext
Wii Nunchuk Mouse
  • Download the Python script at makezine.com/go/nunchuk.
  • Open it with IDLE (Right-click → Edit with IDLE). Look for the line which says port = ‘arduino_port’ and write the correct port (leave the quotes). It should look like port = ‘COM10’ (use the same port as set up in the Arduino IDE). Save the script (File → Save or Ctrl+S).
  • Press F5 to run the file and wait a few seconds. Enjoy using the Wii Nunchuk as a computer mouse!

Gabriel Bianconi

Gabriel Bianconi is a high school student in São Paulo, Brazil. He is interested in technology, especially programming, and wants to study computer science in college.


Comments

  1. Is there more to this? I’d like to see the how-to and the programming code for the arduino.

    1. Hi Neil,
      This is Gabriel, the author of the article. The article that is being published in the print edition contains all the instructions needed to complete the project. I’m also the author of this Arduino library that interfaces the Wii Nunchuk. If you want to learn more about it, please check: http://www.gabrielbianconi.com/projects/arduinonunchuk/

      If you’re interested in the project, I recommend that you get the print edition.

      Best regards,
      Gabriel Bianconi

  2. Tristan says:

    Whenever I Try It Says arduinonunchuck not found Help me Obyone conoby your my only hope

  3. Tristan says:

    I mean arduinonunchuck does not name a type

    1. Hi Tristan,

      I’m the author of the article. 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

  4. Kevin K says:

    i have the print magazine and im on the last step of your article (4c) and it says that the syntax is invalid. the program (python) then highlights the 7 in the first line which you can not edit. i tried restarting everything like the article suggested but it still wont work. what am i doing wrong?
    -Kevin K

    1. Hi Kevin,
      Are you using the Python script that can be found at: makezine.com/go/nunchuk ?
      If yes, please send me a picture of the error. You can host the screenshot at imgur.com or any other image hosting website.

      Best regards,
      Gabriel Bianconi

  5. Matthew Teta says:

    I’m having the same problem with the “ArduinoNunchuck does not name a type”. I was wondering if I was supposed to leave the command prompt and idle open. I made sure it was in the folder and stuff. One other thing that might be the problem is when i opened up Arduino, It said ArduinoNunchuckMaster was unusable because of the dash(-), Could this have messed anything up?

    1. Hi Matthew,

      You should place the “ArduinoNunchuk” folder, not the “ArduinoNunchuk-master” folder, in “libraries”. Note that the “ArduinoNunchuk” is the folder inside “ArduinoNunchuk-master”.

      Best regards,
      Gabriel Bianconi

  6. Aaron Mitchell says:

    Hi Gabriel,

    I enjoyed this project quite a bit, thank you. Did you author the python script used in the project? I have ported it to Linux and would like to make it available.

    Thanks,
    Aaron Mitchell

    1. Hi Aaron,
      I’m glad that you’ve enjoyed completing the project. I authored the Python script too. When you publish the code, let me know. I’d be interested in having a look!

      Thanks,
      Gabriel Bianconi

      1. Aaron Mitchell says:

        I have uploaded the script to https://github.com/ajmitc/nunchukmouse. If you like it, feel free to borrow from it and post a link to it from your project blog. Suggestions are always welcome.

        Thanks,
        Aaron

  7. Gavin says:

    I am having trouble downloading the pyserial module in step 1d. It only leads to a pyserial-2.6.tar.gz file, not a pyserial-2.6 folder. Is this correct? The command prompt reply tells me “path not found”. Thank you.

    1. Aaron Mitchell says:

      Hi Gavin,

      I’m not typically a Windows user, but I believe you can extract the tar.gz file using WinRAR (I did this a number of years ago). Google ‘winrar’ to find some sites that offer it for download.

      Alternatively, you can go to http://sourceforge.net/projects/pyserial/files/pyserial/2.5/ and find Windows binaries for version 2.5. I haven’t tested this project with pyserial-2.5, but it may still work.

      Hope that helps,
      Aaron

      1. Gavin says:

        thanks!

  8. piratebrahm says:

    Hey Gabriel,

    I’m up to step 4b on the project, but when I go to the makezine site, I can’t seem to right click-open with IDLE. Instead, I opened IDLE on my (Win 7) PC, file->new window, copied and pasted the text and replaced with the COM port. When I save and hit F5, I get a new window with two lines of >>> then no response from the nunchuk. I confirmed that the serial reader in Arduiono IDE is picking up the nunchuk, so I’m guessing there’s something I missing with Python? Any thoughts would be appreciated!

    1. Hey piratebrahm,

      You should save the file on Make’s website as a .py file (e.g. “nunchukmouse.py”) on your computer. You should right-click this file. Let me know if you have any other problem!

      Best regards,
      Gabriel Bianconi

      1. piratebrahm says:

        Thanks, it turned out that I couldn’t download the .py file because I was using Internet Explorer. I tried it with Firefox and was able to get the save prompt to show up.

  9. Gavin says:

    Hi It turns out that i also have an issue with step 3e, where it sayes ‘Open the Serial Monitor.’ I have never used the serial monitor before and i don’t know where to find it. i’ve tried useing windows search but that turned up nothing relivent. please help! ;P

    1. Gavin says:

      Never mine i got it

    2. For others that may face the same problem:

      You can open the serial monitor by clicking the following button in the Arduino IDE: http://arduino.cc/en/uploads/Guide/serial_monitor.png

      1. Gavin says:

        the link above doesn’t work. in the arduino ide, press Ctrl+Shift+M or select it on the tools tab.

        1. Hi Gavin,
          The link is working for me. It is however a small icon; look at the top left of the link. Thanks for the shortcut!
          Best regards,
          Gabriel Bianconi

  10. Gavin says:

    on 4c ‘press F5 to run the file and wait a few seconds,’ i get this message in red, and no response from the nunchuck:
    Traceback (most recent call last):
    File “C:/Python27/ArduinoNunchuck”, line 31, in
    ser = serial.Serial(port, baudrate, timeout = 1)
    File “C:\Python27\lib\site-packages\serial\serialwin32.py”, line 31, in __init__
    SerialBase.__init__(self, *args, **kwargs)
    File “C:\Python27\lib\site-packages\serial\serialutil.py”, line 261, in __init__
    self.open()
    File “C:\Python27\lib\site-packages\serial\serialwin32.py”, line 59, in open
    raise SerialException(“could not open port %s: %s” % (self.portstr, ctypes.WinError()))
    SerialException: could not open port COM4: [Error 5] Access is denied.

    Note the last line; How to allow access?
    polease help! ;P

    1. Hi Gavin,

      Try restarting your computer; this problem is common and rebooting usually solves it. If the problem persists, make sure that your Arduino is connected on port COM4 (as described in the article). Let me know if you still get the error after trying these steps.

      Best regards,
      Gabriel Bianconi

      1. Gavin says:

        thanks!

    2. Matt King says:

      Also make sure that you read the step to close the serial monitor. I left mine open and received the same error.

      1. Hi Matt,

        Thanks for pointing this out.

        In the article I mentioned that the serial monitor should be closed, but didn’t emphasize this as an important step. Maybe I should have mentioned this explicitly.

        Thanks again!

        Best regards,
        Gabriel Bianconi

  11. austin says:

    I got the serial port to read the nunchuk outputs but the Python is reading a syntax error over with the 7 in 2.7.2. Any suggestions?

    1. Hi Austin,

      Could you please post a screenshot of this error and tell how it occurred?

      Best regards,
      Gabriel Bianconi

  12. Gavin says:

    hi yet again! after restarting, and i reopened the arduinonunckuck file, hit F5, and got this error message in red print:

    Traceback (most recent call last):
    File “C:\Python27\wiimouse”, line 49, in
    analogX = int(line[0])
    ValueError: invalid literal for int() with base 10: ”

    please help! ;P

    1. Hi Gavin,

      This message shows that an error occurs while the Python script tries to read the nunchuk’s data. One possibility is that the values are not being reported to your computer’s serial port. When using the Arduino IDE’s serial monitor, do you see changing values? Are the Arduino board and the controller set up correctly?

      Another possibility is that the data is being sent to the computer, but the Python script is not reading it correctly. Did you edit the file and add the correct port? You should recheck that you are trying to read from the right port, because it may have changed after you’ve restarted the computer.

      Let me know if the error persists.

      Best regards,
      Gabriel Bianconi

      1. Gavin says:

        yes, when i use the Serial Monitor, i see the values change. i rechecked that the arduino is connected to the right port, and i edited the python script with the same changes. i have the wii nunchuck properly connected to the arduoni, and i still get the error message!

        thank you! ;P

      2. Gavin says:

        *arduino

        1. Hey Gavin,
          I have to see what is being written to your serial port in order to solve your problem. The following script shows this information:

          http://pastebin.com/raw.php?i=gKM0Jqtt

          Can you run it (as you would with the regular script) and tell me what you see? Please paste a few lines here.

          Best regards,
          Gabriel Bianconi

          1. Gavin says:

            i get (in blue)

            Please set up the Arduino port.

          2. Gavin says:

            what does that mean? i have the port working. (i can upload to the arduino)

          3. Hey Gavin,

            You should edit the Arduino port in this script too, like you did in the previous. This script shows what is being written to your computer’s serial port.

            Best regards,
            Gabriel Bianconi

          4. Gavin says:

            Ok here’s what the second python script. this is about 3 seconds; it put about 5 lines per second

            Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
            Type “copyright”, “credits” or “license()” for more information.
            >>> ================================ RESTART ================================
            >>>

            20123 124 322 544 562 0 0

            ### 20123 124 322 544 562 0 0 ### ['20123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 322 544 562 0 0

            ### 123 124 322 544 562 0 0 ### ['123', '124', '322', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            ### 123 124 321 544 562 0 0 ### ['123', '124', '321', '544', '562', '0', '0']
            123 124 321 544 562 0 0

            sorry if that’s a lot of lines ; it went about 5 lines per second.
            i hope this helps! ;P

    2. Seth says:

      Gavin,
      I was getting the exact same problem. I believe it occurs because the first few times the serial port is read in, it outputs some strange digits (I’m using Windows 8). After about the second time it reads in the values from the serial port, the output starts looking good. I set up a counter to wait for the third array of input values before it starts converting them into integers for the cursor movements. I put the code I used at http://www.kwalla.com/NunchuckMouse_release.txt . There is probably a more eloquent solution, but it’s my first time using python. Hope it helps some people. It’s pretty fun once you get it up and running.

      1. Hi Seth,
        Thanks for the contribution! Your insight was very clever and could fix Gavin’s problem.
        Thanks again!
        Best regards,
        Gabriel Bianconi

      2. Gavin says:

        Thanks Seth, but when i copyed the script, changed the port to my arduion’s port, and rad the script in python, it poped up a window saying:
        There’s An Error In Your Program:
        Invalid Syntax.
        and then in the script it highlights the 7 in ‘python 2.7.2′:
        Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
        Type “copyright”, “credits” or “license()” for more information.

        i tryed to delete the top section, but the comp. made one of those crazy erroe blips. Please Help! ;P

        1. Fernando says:

          i have the same error, when i copy the script and run it i get There’s An Error In Your Program:
          Invalid Syntax.and then in the script it highlights the 7 in ‘python 2.7.2′ any ideas how i an fix it ?
          i change the port to port = ‘COM11′

          1. Hi Fernando,
            I believe that you are pasting the script in IDLE’s shell, but you should paste it in a new file instead. Save the script with Ctrl+S (or File > Save) from MAKE’s website with .py extension (no IDLE yet) and then right-click > Edit with IDLE. This will open a text-editor-like window and not the IDLE shell. Edit the port, save it again, and press F5 to run.
            Let me know if this solves the problem!
            Gabriel

  13. Carter Hamblin says:

    ImportError: No module named serial
    Can someone please give me a step by step for this, because evidently I am having some trouble.

    Thanks in advance!

    1. Carter Hamblin says:

      Problem fixed!
      One more thing though…
      Does anyone know what the mac version for the python script would be? I have everything but that (and pywin32) working fine.

      Thanks!

      1. Hey Carter,

        You can use https://github.com/msanders/autopy instead of pywin32 to emulate the mouse on the mac. If you have some Python knowledge, it shouldn’t be hard to implement it.

        Best regards,
        Gabriel Bianconi

      2. Joey says:

        How did you fix it? I’m having the same issue.
        Thanks.

      3. Nzy says:

        “No module named serial” hasn’t been addressed yet. I couldn’t get my set up to work.
        How did you solve your problem?

  14. David says:

    Hi Gabriel, I’m having the same problem as Austin; I can’t get the python code to run when I press F5, as it highlights the 7 in Python 2.7.2 at the beginning of the script, saying there’s a syntax error. I was hoping you could give me some advice.

    1. David says:

      Nevermind. I got it.

  15. Padz says:

    Hi Gabriel,

    I’m using the wireless nunchuck which I’ve successfully used on another Arduino project instead of the wired nunchuck. I wanted to know how compatible your python code is because I have different variable names to yours so is it a case of changing the variable names in your python code?

    Thanks

    1. Hi Padz,
      As long as the values are printed to the serial port correctly, you should have no problem (note that the order must be right). Perhaps you’ll have to calibrate the values, but as long as you can interface the controller, it should work.
      Best regards,
      Gabriel Bianconi

  16. Grant says:

    Gabriel,
    During step one, I downloaded the pyserial-2.6 file and used win.RAR to extract it to my C: drive; however, when I type in cd /d c:pyserial-2.6 the system cannot find the path specified.
    Grant

    1. Hi Grant,
      The online version of this article is missing a backslash. The command should be:
      cd /d c:\pyserial-2.6
      Gabriel

      1. Grant says:

        Gabriel,
        Thank you for clearing that up. I’m also experiencing problems typing in the second command c:Python27python setup.py install I also tried c:/Python27python setup.py install I appreciate your patiene as I am just learning how to do most of these things
        Grant

        1. Grant,
          It should be:
          c:\Python27\python setup.py install
          Gabriel

          1. Peter says:

            hello i am Peter. i downloaded the pyserial 2.6. but i cant extract it. the extention is pyserial-2.6.tar.gz

  17. Zarek says:

    HI Gabriel, i have a problem in python shell, it says:
    import error: No module named win32api.

    Thanks.

  18. Mich says:

    It seems that the nunchuk mouse goes off of the gyroscope in the nunchuk and not the analog pad, I have to tilt the remote to move the cursor, but the z and c buttons work. How can I make it go off of the analog stick?

  19. chirs says:

    hello Gabriel! i came across this project while looking for stuff to do with Arduino. I could figure this out if i was on windows but im on mac osx. Could you link me to/ make a tutorial for mac osx? thanks alot

  20. I got the file as pyserial-2.6.tar.gz I was wondering if this was correct for windows 8, mainly because I thought it would come out as a zip folder.
    By the way, I’m 12.

  21. Brandon Thompson says:

    Gabriel
    i am having troubles with step 3d and uploading the sketch to my Arduino Leonardo, i do not know if it has something to do with the board being a Leonardo, or if the issue is that i don’t have the option for a COM4 or some other unknown reason. All that i get when i try uploading the sketch is “Error compiling.” Any advice is apreciated

  22. ofrank says:

    Is there any way to make this work with mac?

  23. John Lutz says:

    Hey Gabriel!

    Great project! I’m still relatively new to programming generally, and Arduino specifically, but I was able to make this work for me with little difficulty. Thanks for the project!

    Using the accelerometers is fun, but I’m looking for an easy project that will allow me to use the joystick as the input for moving the pointer around. I would be happy to ignore the accelerometers completely, although I am happy with the way the c and z buttons function. Can you suggest how I might be able to adjust your code to fit my needs? Are these changes that I would need to make within the library, or can I make them within the sample program?

    Thanks again!

    John Lutz

  24. Ptrip3 says:

    So yeah got to the last step. Spent hours trying to get this to work and this pops up:
    Traceback (most recent call last):
    File “C:\Users\PaulIII_Internet\Desktop\NunchukMouse_Release.py”, line 2, in
    import math, string, time, serial, win32api, win32con
    ImportError: DLL load failed: The specified module could not be found.
    Any ideas as how to fix this? Ive tried pretty much everything. Thanks

    1. Ptrip3 says:

      Also I wasn’t able to get pyserial-2.6 because all they have now is pyserial-2.7.

  25. Jack says:

    when i put the code in command prompt it says “This system can not find the path specified.”
    what is wrong?
    Thanks

  26. RoxRock says:

    How to tell which board do I have? The package it came with did not come with the description. On the chipset it says ATMEL-1312, ATMEGA328P-PU. Thanks.

  27. […] The project was selected for publication in MAKE Magazine (vol. 33, jan. 2013), where I explain how to build it step-by-step. I’m not allowed to publish my article here, but fortunately they’ve published it online. Check the Wii Nunchuk Mouse on MAKE’s website. […]

In the Maker Shed