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.

Next
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.

Next
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.

Next
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.

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!

47 Responses to Wii Nunchuk Mouse

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

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

  3. I mean arduinonunchuck does not name a type

    • 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. 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

    • 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 on said:

    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?

    • 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 on said:

    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

  7. 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.

  8. 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!

    • 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

      • piratebrahm on said:

        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. 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

  10. 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

  11. 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?

  12. 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

    • 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

      • 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

      • *arduino

        • 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

          • i get (in blue)

            Please set up the Arduino port.

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

          • 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

          • 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

    • 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.

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

      • 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

        • 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′

          • 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 on said:

    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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

%d bloggers like this: