With-wii-nunchuck

From Pinguino-Wiki
Jump to: navigation, search

This example shows how to connect a Pinguino board to the Wii Nunchuck.

For this test, we used an original Nunchuck from Nintendo. With compatible model, you need to adapt some value ( delay ).

The nunchuck can be used as a 3 axis accelerometer, an analog stick and 2 buttons.

The code uses the LibI2C module in Pinguino. We adapted the I2C initialisation to work with a clock frequency of 100 kHz. The output sends value to the serial port.

The beta 9-05 version is used to compile the code.


How to connect it ?

A piece of double-sided PCB has been used to make a male connector.

Nunchuck.png


Pinguino is connected to the Nunchuck via the I2C bus.

SCL is PIN 0 and SDA is PIN 1.

The nunchuck is powered from the 5vcc Pinguino power supply.



Pinguino Breadboard

Pinun.png



Two 22k resistors are used as pull-ups on the SDA and SCL lines.

Test is on a 18F2550 Pinguino but it can also be wired on a 18F4550.




The code

// test program for I2C communication with nunchuck
// Jean-Pierre MANDON 2011
 
#include <libI2C.c>
 
byte I2C_address=0x52;						// i2c address for nunchuck
unsigned char I2C_OK;						// acknowledge when calling I2C function
byte I2C_setParameters[2]={0x40,0x00};		                // nunchuck initialisation
byte I2C_readRegister[1]={0x00};			        // read instruction for nunchuck
 
void setup()
{
// Serial is used to monitor data
Serial.begin(9600);
// special initialisation to set I2C to 100khz
TRISBbits.TRISB0=1;
TRISBbits.TRISB1=1;
i2c_open(I2C_MASTER, I2C_SLEW_OFF, 119);
// end of I2C initialisation
}
 
void nunchuck_init()
{
	I2C_OK=I2C_write(I2C_address,I2C_setParameters,2);	// set memory address for future read
	I2C_STOP();
}
 
void send_zero()
{	
	I2C_OK=I2C_write(I2C_address,I2C_readRegister,1);	// set request to read
	I2C_STOP();
}
 
// prepare a new request to memory
void new_request()
{
unsigned char cpt;
	cpt=0;
	I2C_OK=0;
	while ((cpt++<10)&&(!I2C_OK)) nunchuck_init();		
	delay(10);
	cpt=0;
	I2C_OK=0;
	while ((cpt++<10)&&(!I2C_OK)) send_zero();
	delay(10);	
}
 
void loop()
{
unsigned char i;
 
nunchuck_init();						// initialise nunchuck
if (!I2C_OK)
	{
	Serial.printf("Init Error\n\r");
	while(1);
	}
else Serial.printf("Init OK\n\r");
delay(20);
i=0;
I2C_OK=0;
while ((i++<10)&&(!I2C_OK)) send_zero();			// prepare new request
if (!I2C_OK) 
	{
        Serial.printf("Nunchuck Error\n\r");
	while(1);
	}
else Serial.printf("Request OK\n\r");	
while(1)	
	{	
	I2C_OK = I2C_read(I2C_address,6);			// read nunchuck value
	if (I2C_OK)
		{
		for (i=0;i<6;i++) Serial.printf("%d ",i2c_buffer[i]);
		Serial.printf("\n");
		delay(10);
		new_request();					// prepare a new request
		}
	}		
}

6 bytes are read from the nunchuck. Each byte is the value of a sensor.

byte 0 is the X analog value of the analog stick,
byte 1 is the Y analog value of the analog stick,

byte 2 is the X value of the 10 bit accelerometer (bits 2-9),
byte 3 is the Y value of the 10 bit accelerometer (bits 2-9),
byte 4 is the Z value of the 10 bit accelerometer (bits 2-9),

byte 5
bit 7-6 is bit 1-0 of the X accelerometer data,
bit 5-4 is bit 1-0 of the Y accelerometer data,
bit 3-2 is bit 1-0 of the Z accelerometer data,
bit 1 is the state of the C button ( 0=pressed ),
bit 0 is the state of the Z button ( 0=pressed ).


More about the nunchuck

WiiBrew website

Personal tools