Wii nunchuk as input for picaxe

lbenson

Senior Member
Inspired by the article in the March, 2011 issue of Nuts and Volts on hooking a wii nunchuk to a propeller, I decided to try to connect it to a picaxe—a 20X2 in this case. This has been done before - http://www.picaxeforum.co.uk/showthread.php?t=12220 and elsewhere – but that thread didn’t show the hardware, didn’t use the joystick, and in comments, didn’t fully describe the button values (but thanks much for the working program).

The nunchuk has a 3-axis accelerometer, a joystick and two buttons, labeled “C” and “Z”. It connects via i2c, with a 3.3-volt interface. In the Nuts and Volts article, the author cut the nunchuk cable and added connectors, but I didn’t want to do that, so I went searching for an adaptor. I found one at fungizmos.com for $4 with free shipping in the U.S.: http://store.fungizmos.com/index.php?main_page=product_info&cPath=69&products_id=212

I wired up an adaptor to the adaptor to get the signals from my i2c header (0V, clock, 5V, data) to the voltage and position of the fungizmos adaptor (0V, 3.3V, data, clock). I used an MCP1700-3300 voltage regulator to make the nunchuk happy, with 4.7K pull-ups.

I modified the program listing in the picaxe forum post given above to display the values of the joystick x and y and the C and Z triggers only when there is a change. The 6 bytes of data which the nunchuk gives back are “encoded”—you get the “unencoded” value with “val = val ^0x17 + 0x17”. I don’t know what to do with an accelerometer, so I ignored those values. The 6 bytes are joystick-x, joystick-y, accel-x (bits 2-9) , accel-y (bits 2-9) , accel-z (bits 2-9), and the catch-all end byte which contains the two least significant bits of accel x,y,z and the condition of the buttons.

The buttons are a bit counterintuitive. The Z button is pressed when bit 1 is 0 (so 00 or 01), but the C button is press when one but not both bits are on (01 or 10). Both buttons are released when the bits are 11.

For the joystick, I found that the x values ranged from 46 (full left) to 255 (full right) with 172 as the center; y values ranged from 46 (full down) to 255 (full up) with 131 as the center. When moving the joystick, some values below 46 appeared—I didn’t identify the circumstances.

I haven’t a particular use for this interface, so I’ve put it in the Miscellaneous category. Because the name of the device is frequently spelled “nunchuck”, I’ve included that spelling so a search for it will find this thread. I got my nunchuk on ebay for under $7.

Code:
'20Nchk1 reads joystick and buttons
#picaxe 20x2


Symbol joystick_x = b30
Symbol joystick_y = b31
Symbol Accel_X = b32
Symbol Accel_y = b33
Symbol Accel_z = b34
Symbol Accel_buttons = b35 ' lower 2 bits of accel & Z,C button states
Symbol xOld = b36
Symbol yOld = b37

Symbol CZ0old = bit0 ' CZ0 and CZ1 describe button positions:
Symbol CZ1old = bit1 ' 00=Z pressed; 01=both pressed ; 10=C pressed ; 11=neither pressed
Symbol changed = bit2
Symbol CZ0 = bit24 ' CZ0 and CZ1 describe button positions:
Symbol CZ1 = bit25 ' 00=Z pressed; 01=both pressed ; 10=C pressed ; 11=neither pressed

Symbol nunchukaddress = $A4

Init:
  pause 10
  hi2csetup i2cmaster, nunchukaddress, i2cslow, i2cbyte
  hi2cout ($40,$00)
  pause 1

main:
  do
    gosub readNunchuk
    changed = 0
    if CZ0 <> CZ0old then
      changed = 1
      CZ0old = CZ0
    endif
    if CZ1 <> CZ1old then
      changed = 1
      CZ1old = CZ1
    endif
    if joystick_x <> xOld then
      changed = 1
      xOld = joystick_x
    endif
    if joystick_y <> yOld then
      changed = 1
      yOld = joystick_y
    endif
    if changed = 1 then
      sertxd(#joystick_x, " ", #joystick_y, " ", #CZ0, " ", #CZ1,cr,lf)
    endif
'    pause 2000
  loop

readNunchuk:
  hi2cout (0)
'  pause 1
  hi2cin (joystick_x,joystick_y,Accel_X,Accel_Y,Accel_Z,Accel_buttons)
  b3 =Accel_buttons^0x17 + 0x17    'decrypt button state
'                      '"Z" Button pressed = 00 or 01
'                      '"C" button pressed = 01 or 10
                            'Bits 2-3: X acceleration 2 LSBs
                            'Bits 4-5: Y acceleration 2 LSBs
                            'Bits 6-7: Z acceleration 2 LSBs

  joystick_x= joystick_x^0x17 + 0x17    'Decrypts x-axis Value of joystick 
                            'Min(Full Left): 46
                            'Medium(Center): 172
                            'Max(Full Right): 255
            

  joystick_y=joystick_y^0x17 + 0x17      'decrypts y-axis value of joystick
                            'Min(Full Down): 46
                            'Medium(Center): 131
                            'Max(Full Right): 255
  return
 

Attachments

lbenson

Senior Member
To the best of my recollection, the picaxe was running on 5V, the nunchuk at 3.3V, and the pullups were to 3.3V. It worked fine with my limited testing. I haven't used it since.
 

andrefcarvalho

New Member
I was using but follow with a 18m2 3.3v worked well connecting some leds, but never gave in debug to read, so that the plan had to 5v and 3.3v after use.
 
Top