Nunchuck WII Interface I2C

julianE

Senior Member
I've been trying to interface a Nintendo nunchuck to a PICAXE 18X using I2C with no success. It has been done with other platforms and I would think the Picaxe should be able to as well. Perhaps I should be using a 28X.

I've been using this tutorial as a guide,

http://[URL="http://www.windmeadow.com/node/42"]

any suggestions?

thanks.
 

BeanieBots

Moderator
Should be possible to do with an 18X assuming that article ever worked.
It's just a question of changing his code into PICAXE code.

BE WARNED.
The nunchuck is 3.3v. DO NOT do what the author claims to have done (might explain his data errors) and run at 5v. It won't reduce life-span. It will destroy it. Run the PICAXE at 3.3v as well and all will be fine.
 

Technical

Technical Support
Staff member
Something along the lines of

Code:
i2slave $A4, i2cfast, i2cbyte
 
i2cwrite ($40,$00)
pause 10
 
lp:
writei2c (0)
pause 10
readi2c (b1,b2,b3,b4,b5,b6)
pause 500
debug
goto lp
should work. A 28X1 does do i2c faster than an 18X, so may also be worth a try.
 

julianE

Senior Member
i too was weary of running it at 5 v so I used 2 AA batteries for my tests, should be near the 3.3V.

my assumption is to set the slave address as

i2cslave $A4, i2cfast, i2cbyte

i'm not sure that 18x can handle the i2cfast communication but would think the nunchuck would adjust.

i'm very new at this and i'm not all that familiar with different variants of basic, not sure how to translate it to picaxe,

in the article, he does this to read from nunchuck using picbasic Pro

XX: 'read nunchuk

i2cwrite sda,scl,$A4,$40,[$00]
pause 10
i2cwrite sda,scl,$A4,[0]
pause 10
i2cread sda,scl,$A5,[a1,a2,a3,a4,a5,a6]


Best i can tell it would translate to PicAxe,

i2cslave $A4, i2cfast, i2cbyte
writei2c $40,(0)
pause 10
writei2c 0,(0)
pause 10
readi2c 0,(b1,b2,b3,b4,b5,b6)


unfortunately when i tried using this method all i would get is 255 in all 6 registers.

my experience with I2C is limited to interfacing with EEPROMS, most likely i'm doing something wrong.
 

julianE

Senior Member
Just a follow up. the 28x1 came in today and i tried it with the nunchuck. works really well, much better then the 18X chip, which hesitated displaying acceleration numbers. thank so much for all the help.
 

picaxester

Senior Member
Something along the lines of

Code:
i2slave $A4, i2cfast, i2cbyte
 
i2cwrite ($40,$00)
pause 10
 
lp:
writei2c (0)
pause 10
readi2c (b1,b2,b3,b4,b5,b6)
pause 500
debug
goto lp
should work. A 28X1 does do i2c faster than an 18X, so may also be worth a try.
This code doesn't work for me :(
 

julianE

Senior Member
i2slave $A4, i2cslow, i2cbyte

try it with i2cslow. i had to set it for i2cslow even on the 28x, probably should be using hi2csetup with the 28x.

the nunchuck works perfect, i also tried it with the clock set for 8mHz and it worked well.
 

picaxester

Senior Member
Thanks :)

This is the best I can do for now.
The out putted accelerometer data doesn't make a whole lot of sense.
But the button and joy-stick seam to but it looks like the X axis of the joy-stick is overflowing.

Code:
i2cslave $A4, i2cfast, i2cbyte
 
i2cwrite ($40,$00)
pause 10
 
lp:
writei2c (0)
pause 10

readi2c (b1,b2,b3,b4,b5,b0)

b13 = b0
b0 = b13 Xor 0x17 + 0x17

'X joy
b13 = b1
b1 = b13 Xor 0x17 + 0x17
poke $50,  b1

'Y joy
b13 = b2
b2 = b13 Xor 0x17 + 0x17
poke $51,  b2

'X acc
b12 = bit2 * 2 + bit3
b13 = b3
w5 = b13 Xor 0x17 + 0x17 * 2 + b12
poke $52, word w5

'Y acc
b12 = bit4 * 2 + bit5
b13 = b4
w5 = b13 Xor 0x17 + 0x17 * 2 + b12
poke $54, word w5

'Z acc
b12 = bit6 * 2 + bit7
b13 = b5
w5 = b13 Xor 0x17 + 0x17 * 2 + b12
poke $56, word w5

'buttons
poke $58, bit0
poke $59, bit1

'X joy
peek $50, b0
'Y joy
peek $51, b1
'X acc
peek $52, w1
'Y acc
peek $54, w2
'Z acc
peek $56, w3
'buttons
peek $58, b12
peek $59, b13

pause 100
debug
goto lp
 
Last edited:

julianE

Senior Member
decoding is pretty simple. i even wonder if there is a real need to bother with the 2 least significant bits for the accelerometer, would make for a very compact software without. i did decode for the full 10 bit readout for experimenting but i doubt i'll bother in the future.
 

picaxester

Senior Member
Yeah, it's not really needed.

Heres a test program if anyone what to try it:
Attach LED/resistors to output pins: 0, 2, 3, 5.
Code:
i2cslave $A4, i2cfast, i2cbyte

 

i2cwrite ($40,$00)

pause 10

 

lp:

writei2c (0)

pause 10



readi2c (b1,b2,b3,b4,b5,b0)



b13 = b0

b0 = b13 Xor 0x17 + 0x17



'X joy

b13 = b1

b1 = b13 Xor 0x17 + 0x17

poke $50,  b1



'Y joy

b13 = b2

b2 = b13 Xor 0x17 + 0x17

poke $51,  b2



'X acc


b13 = b3

b3 = b13 Xor 0x17 + 0x17 

poke $52, b3



'Y acc


b13 = b4

b4 = b13 Xor 0x17 + 0x17

poke $54, b4



'Z acc


b13 = b5

b5 = b13 Xor 0x17 + 0x17 

poke $56, b5



'buttons

poke $58, bit0

poke $59, bit1



'X joy

peek $50, b0

'Y joy

peek $51, b1

'X acc

peek $52, b2

'Y acc

peek $54, b3

'Z acc

peek $56, b4

'buttons

peek $58, b5

peek $59, b6



if b2 < 100 then

	high 0

else

	low 0

endif



if b2 > 140 then

	high 2

else

	low 2

endif



if b3 < 100 then

	high 3

else

	low 3

endif



if b3 > 140 then

	high 5

else

	low 5

endif



pause 10

'debug

goto lp

eric
 

MurrayJ

Senior Member
I would like to try out a Nunchuck with my picaxe system, but both my breadboard and application I want to try it out on will be 5V. Is there any simple way to connect a nunchuck to 5V?
 

picaxester

Senior Member
I used a 3.3V regulator.

But if your PICAXE circuit has to run on 5V then you could do something like this :)



Running only the nunchuk on 3.3V.

Wait... that might not work, oops.
Sorry, I just woke up, I'm still out of it.

But you could try something kinda like that.
 
Last edited:

hippy

Technical Support
Staff member
In theory, as all interfaces to the I2C bus should be open-collector, it should be possible to use that circuit without the diodes - but that's done at your own risk and with no guarantees, any damage done is your own responsibility.
 

MurrayJ

Senior Member
Thanks Picaxester and Hippy, the resistors on the I2c lines is what I was thinking, had'nt thought about diodes. What about powering the Nunchuck from 5V as well? Would a diode or LED on the nunchuck power line work ok?
 
Last edited:

Technical

Technical Support
Staff member
Three silicon diodes (1N4001 etc) from 5V to the nunchuck power line will indeed give you about 2V drop, so giving 5V at PICAXE and 3V at nunchuck.
 

slimplynth

Senior Member
from the link ..."Bread board friendly! Can be used with normal serial, I2C, SPI, and any other digital signal. Does not work with an analog signal."
 

MurrayJ

Senior Member
For I2C connection I believe you connect the two I2C wires to the two TX lines and leave the RX lines unconnected.
 

tye_scott

New Member
This is my first post so here goes...
The following code extracts the Wii Nunchuck Accelerometer data, including the LSB for x,y,and z acceleration. All information was found at http://www.wiili.org/index.php/Wiimote/Extension_Controllers/Nunchuk
and using the accelerometer datasheet, I notated that values are +-2G. Please Evaluate code, I'm new to programming and this is my first real program. Thanks!
Code:
Symbol Analog_x = b10
Symbol Analog_y = b1
Symbol Accel_x = W1
Symbol Accel_y = w2
Symbol Accel_z = w3

Init:
i2cslave $A4, i2cslow, i2cbyte

writei2c ($40,$00)

Pause 10

Retrieve:

writei2c (0)

Pause 10

Readi2c (analog_x,analog_y,Accel_x,Accel_Y,Accel_z,b0)

b0 =b0^0x17 + 0x17			'decrypts button state
Poke $58, Bit0				'Bit 0: "Z"-Button (0 = pressed, 1 = released)
Poke $59, Bit1				'Bit 1: "C" button (0 = pressed, 1 = released)
						'Bits 2-3: X acceleration LSB
						'Bits 4-5: Y acceleration LSB
						'Bits 6-7: Z acceleration LSB

analog_x= analog_x^0x17 + 0x17 	'Decrypts x-axis Value of analog stick 
						'Min(Full Left):0x1E
						'Medium(Center):0x7E
						'Max(Full Right):0xE1
			

analog_y=analog_y^0x17 + 0x17 	'decrypts y-axis value of analog stick
						'Min(Full Down):0x1D
						'Medium(Center):0x7B
						'Max(Full Right):0xDF
						
				
Accel_x=Accel_x^0x17 + 0x17	'decrypts x-axis acceleration value
accel_x=accel_x << 2			'Min(at -2G):0x48
b0=b0 >> 2 & 0x03	 		'Medium(at 0G):0x7D
accel_x=Accel_x | b0			'Max(at 2G):0xB0


Accel_Y=Accel_y^0x17 + 0x17	'decrypts y-axis acceleration value
accel_y=accel_y << 2			'Min(at -2G):0x46
b0=b0 >> 2 & 0x03			'Medium(at 0G):0x7A
accel_y=Accel_y | b0			'Max(at 2G):0xAF

					
Accel_z=Accel_z^0x17 + 0x17	'decrypts z-axis acceleration value
accel_z=accel_z << 2			'Min(at -2G):0x4A
b0=b0 >> 2 & 0x03			'Medium(at 0G):0x7E
accel_z=Accel_z | b0			'Max(at 2G):0xB1
Peek $58, Bit0
Peek $59, Bit1
				
pause 10
debug
goto retrieve
This is on a 28X1 running at 4MHz
 

Madtom

Member
If you have an aftermarket nunchuck then write $40,(0) doesn't work you will have to put

Writei2c 0xf0,(Ox55)
Pause 10
Writei2c Oxfb,(0x00)
Pause 10

Then carry on with writei2c 0,(0)
 
Top