[I2C] Wii Nunchuk Sensor

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: 99jonathan, roger, imaqine

Postby underfarad » Tue Apr 28, 2009 12:56 pm

Hi,

I made a quick summary:

1) remove the pull-ups resistor in the Nunchuk :
Image
Image
(source: http://www.mindstormsforum.de/viewtopic.php?t=3828)

2) Connect new pull-ups and power up the Nunchuk (3.3v) :
You can directly connect the 4.5v of the NXT (Green wire) on the Nunchuk, but it will damage your Nunchuk.
Image

3)Program your NXT :
-You need to use the Gloomyandy leJOS upgrade (look above : snapshot2).
-The sensor source code :

Code: Select all
package v2;
import lejos.nxt.*;

/**
 * I2C mastering for the Nunchuk
 *
 * @version  v0.1
 * @project  Nunchuk leJOS
 * @author  Si-Mohamed Lamraoui
 */
public class NunchukSensor extends I2CSensor
{

   public final static int  NUNCHUCK_ADDR = 0x52;
   private I2CPort port ;
   private byte[] data = new byte[6];
   private byte[] init = new byte[2];
   
   /**
   * Create a new Nunchuk sensor plug on the NXT port
   * @param sensorPort NXT sensor port
   */
   public NunchukSensor(I2CPort sensorPort)
   {
      super(sensorPort);
      this.port = sensorPort;
      this.setAddress(NUNCHUCK_ADDR);
       this.port.setType(SensorConstants.TYPE_LOWSPEED);
       this.port.setMode(SensorConstants.MODE_RAW);
   }

   /**
    * Initializes the Nunchuk to communicate in I2C
    */
   public void initialize()
   {
      this.init[0] = 0x00;
      this.sendData(0x40, init,1);
   }
   
   /**
   * Method for retrieving the data from the Nunchuk
   * @return value of 8-bit I2C bus port
   */
   public byte[] getNunchukData()
   {   
      this.getData(0x00, this.data, 6);
      return data;
   }
   
   /**
    * Ask 6 Bytes of sensor value
    */
   public void AskNunchukData()
   {
      this.sendData(0x00, (byte) 0x00);
   }
   
   /**
   * Method for decrypt the data from the Nunchuk
   * @return value decrypted
   */
   public byte decryptData(byte value){
      return (byte) ((value ^ 0x17) + 0x17);
   }
   
}


The test source code :
Code: Select all
package v2;
import javax.microedition.lcdui.Graphics;
import lejos.nxt.*;


/**
 * Test the Nunchuk with the LCD screen
 *
 * @version  v0.2
 * @project  Nunchuk leJOS
 * @author  Si-Mohamed Lamraoui
 */
public class TestLCD
{

   /**
    * Main
    */
   public static void main()
   {
      
      NunchukSensor nunchuk = new NunchukSensor(SensorPort.S4);
      Graphics graph = new Graphics();
      byte[] data = new byte[6];
      
      graph.drawString(" NUNCHUK NXT v0.1",1,1);
      graph.clear();
      
      while(!Button.ESCAPE.isPressed())
      {
         graph.drawString(" === NUNCHUK === ",0,0);
         nunchuk.initialize();
           nunchuk.AskNunchukData();

         data = nunchuk.getNunchukData();
         
         //Z-Button
         if((nunchuk.decryptData(data[5]) & 0x01) > 0)
         {   
            graph.drawString("Z : RELEAZED",0,9);
         }
         else
         {
            graph.drawString("Z : PRESSED",0,9);
         }
         //C-Button
         if((nunchuk.decryptData(data[5]) & 0x02) > 0)
         {   
            graph.drawString("C : RELEAZED",0,17);
         }
         else
         {
            graph.drawString("C : PRESSED",0,17);
         }
         
         //Joystick
         graph.drawString("Joystick X:",1,24);
         LCD.drawInt(nunchuk.decryptData(data[0]),0,12,3);
         graph.drawString("Joystick Y:",1,32);
         LCD.drawInt(nunchuk.decryptData(data[1]),0,12,4);         
         
         //Accelerometer
         graph.drawString("Accel X:",1,40);
         LCD.drawInt(nunchuk.decryptData(data[2]),0,12,5);   
         graph.drawString("Accel Y:",1,48);
         LCD.drawInt(nunchuk.decryptData(data[3]),0,12,6);   
         graph.drawString("Accel Z:",1,56);
         LCD.drawInt(nunchuk.decryptData(data[4]),0,12,7);   
         
         graph.refresh();
         try
         {
             Thread.sleep(10);
         }
         catch (InterruptedException e) {}
         graph.clear();
      }
   }

}


HAVE FUN !!!
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Tue Apr 28, 2009 1:42 pm

Hi,
One quick question. Did you ever try the extra diode so that you can provide the 3.3V from the NXT rather than having to have an extra 3.3V supply?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4174
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby underfarad » Tue Apr 28, 2009 3:18 pm

gloomyandy wrote:Hi,
One quick question. Did you ever try the extra diode so that you can provide the 3.3V from the NXT rather than having to have an extra 3.3V supply?

Andy

Yes. But I use 0.7v drop Diode, the Nunchuk is powered by 3.8v (same connect of Tom123).
Dangerous, but it works perfectly.
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Re: [I2C] Wii Nunchuk Sensor

Postby trandi » Sun Jul 24, 2011 10:42 am

Here's one of my posts where I don't bother using any diode (simply using the 4.3V from the NXT), I did de-solder the SMD resistors from the nunchuck and I'm simply using 100kOhms ones on the lines directly:

http://trandi.wordpress.com/2010/10/18/nxt-lejos-nunchuck/

It works flawlessly !

Hope this helps,
Trandi
https://trandi.wordpress.com/
trandi
New User
 
Posts: 6
Joined: Mon May 02, 2011 6:37 pm

Previous

Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff