[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

[I2C] Wii Nunchuk Sensor

Postby underfarad » Wed Apr 22, 2009 1:25 pm

Hi,

I work on the Wii Nunchuk and I would connect this one with the NXT.
But i have some problem, the nunchuk does not responding at the I2C request.

-I use 33k pull-up (connected on 3.3v)
-The nunchuk is powered by 3.3v
-The of the NXT is connected on the Nunchuk
-The nunchuk I2C speed is 100khz (we can up to 400khz)
-The clock and the data wires is correctly connected

Nunchuk sensor :
Code: Select all
import lejos.nxt.*;

/**
 * I2C mastering for the Nunchuk
 *
 * @version  v0.1
 * @project  Nunchuk RS232
 * @author  Si-Mohamed Lamraoui
 */
public class I2CNunchuk extends I2CSensor
{
   //leJOS expects the unshifted 7 bit address and the read/write direction is specified separately (it is the last parameter in the Start method above).
   // div by 2 -> convert to 7 bits by shifting the address right by 1.
   public final static int  NUNCHUCK_I2C_WRITE_ADDR = 0x52;//0xA4 / 2; //address (0x52) + write bit (0) = 0xA4
   public final static byte NUNCHUCK_I2C_READ_ADDR  = 0x52;// 0xA5 / 2; //address (0x52) + read bit (1) = 0xA5
   private I2CPort port ;
   private byte[] data = new byte[6];
   private byte[] init = new byte[2];
   
   /**
   * Constructor
   * @param sensorPort NXT sensor port
   */
   public I2CNunchuk(I2CPort sensorPort)
   {
      super(sensorPort);
      this.port = sensorPort;
      this.setAddress(NUNCHUCK_I2C_READ_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] = 0x40;
      this.init[1] = 0x00;
      this.sendData(0x00, init,2);
      /*try
       {
          Thread.sleep(10);
       }
       catch (InterruptedException e)
       {}*/
   }
   
   /**
   * Method for retrieving the data from the Nunchuk
   * @return value of 8-bit parallel bus port
   */
   public byte[] getNunchukData()
   {   
      this.getData(0x00, this.data, 6);
      return data;
   }
   
   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);
   }
   
}


Main :
Code: Select all
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;

/**
 * I2C Testing
 *
 * @version  v0.1
 * @project  Nunchuk RS232
 * @author  Si-Mohamed Lamraoui
 */
public class TestLCD
{
   
   public static void main(String [] options)
   {
      
      I2CNunchuk nunchuk = new I2CNunchuk(SensorPort.S4);
      Graphics g = new Graphics();
      byte[] data_n = new byte[6];
      int result;
      nunchuk.initialize();
       g.drawString(" NUNCHUK - NXT v0.1",1,1);
       g.clear();
      
      while(!Button.ESCAPE.isPressed())
      {
         g.clear();
         g.drawString(" NUNCHUK values : ",1,1);

         data_n = nunchuk.getNunchukData();

         LCD.drawInt(data_n[0],1,1,3);
         LCD.drawInt(data_n[1],1,1,4);
         LCD.drawInt(data_n[2],1,1,5);
         LCD.drawInt(data_n[3],1,1,6);
         LCD.drawInt(data_n[4],1,1,7);

           g.refresh();
           nunchuk.AskNunchukData();
         try
          {
             Thread.sleep(100);
          }
          catch (InterruptedException e) {}
      }
   }

}


I find a man who have connected the Nunchuk in C language:
http://www.mindstormsforum.de/viewtopic.php?t=3828

Thanks
underfarad
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Wed Apr 22, 2009 2:38 pm

Hi,
Did you remove the built in pull up resistors from the Nanchuk?

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

Postby underfarad » Wed Apr 22, 2009 2:56 pm

No, I didn't touch the circuit.
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Wed Apr 22, 2009 3:10 pm

I think you will have to remove the default pull ups if you dont then it is not possible for the nxt to pull the level down to 0V....

See the last post on this page...
http://forums.nxtasy.org/index.php?showtopic=3043&st=0

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

Postby underfarad » Thu Apr 23, 2009 7:02 am

I see !

So, I remove the Nunchuk pull-ups (R1 and R2) and I use 82k pull-ups.

It's ok?

(thanks for your help :wink: )
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Thu Apr 23, 2009 7:13 am

Hi,
I have no direct experience of doing this, but the following post seems to make sense to me...
http://forums.nxtasy.org/index.php?showtopic=3075
I would follow that. It seems to require 33K resistors plus a diode...

Having said that you may still run into problems with the leJOS i2c driver, but I think you need to gt the hardware sorted out first....

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

Postby underfarad » Thu Apr 23, 2009 7:21 am

Yes, i will do that.
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby underfarad » Thu Apr 23, 2009 1:41 pm

I have remove the nunchuk pull-ups (R1&R2) and I have put 33k pull-ups.

I have check with the scope, the signal was good (400mv for low-level and 3v for high-level).
In the same time I have measured the frequency, I find 10khz, but some people say:
Direct I2C
Data can be read directly from the nunchuck cable using the I2C protocol at 400kHz, when powered at 5V, or 100kHz when powered at the recommended 3.3V. A microcontroller such as an Atmel Atmega168 can be used to read the data. The device address is "0x52" ("0xA4" in 8-bit addressing). Two bytes "0x40,0x00" must be sent to initialize the nunchuck. Then one byte "0x00" must be sent before each data request. Data is requested in 6 byte chunks. Sample code can be found at [1].
http://www.wiili.org/index.php/Wiimote/ ... rs/Nunchuk


Finaly, the NXT does not respond :(
Any ideas?
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Thu Apr 23, 2009 2:47 pm

Have you got the diode in the circuit to drop the supply voltage to 3.6v rather than 5v?

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

Postby gloomyandy » Thu Apr 23, 2009 2:52 pm

Also it looks like you need to write 0x00 to register 0x40 to start things off....

so your code
Code: Select all
      this.init[0] = 0x40;
      this.init[1] = 0x00;
      this.sendData(0x00, init,2);


May need to be....
Code: Select all
      this.init[0] = 0x00;

      this.sendData(0x40, init,1);
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4241
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby underfarad » Thu Apr 23, 2009 3:31 pm

I didn't use a diode, I uses an external power (3.3).
I try to connect the pull-ups one 3.3v and one 4.5(NXT).

I also try the write in register 0x40, but it still does not work :(


I think the problem comes from the frequency (too low). I think Tom123 succeeded because he uses Not eXactly C (other firmware), and leJOS I2C's frequency is lower than this firmware.
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Thu Apr 23, 2009 4:49 pm

No the leJOS i2c frequency is the same as the standard firmware...

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

Postby gloomyandy » Thu Apr 23, 2009 5:02 pm

You could try using the updated i2c support that is in the development version. Instructions for switching to this are here....
http://lejos.sourceforge.net/forum/viewtopic.php?t=1374

Note however that the i2c interface has been changed and you may have to make changes to your code to use this new version...

The source of the latest i2c class is here...

http://lejos.svn.sourceforge.net/viewvc/lejos/trunk/classes/lejos/nxt/I2CSensor.java?revision=2333&view=markup

and here...
http://lejos.svn.sourceforge.net/viewvc/lejos/trunk/classes/lejos/nxt/SensorPort.java?revision=2269&view=markup

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

Postby underfarad » Fri Apr 24, 2009 12:12 pm

It's works !!!

THANK YOU VERY MUCH Gloomyandy :mrgreen:

I will clean my code, and i will post here.
underfarad
New User
 
Posts: 9
Joined: Wed Apr 22, 2009 12:40 pm

Postby gloomyandy » Fri Apr 24, 2009 1:50 pm

Hey that's great news! Let us know what you had to do to get this working... I'm sure there will be a lot of people that will want to try this...

All the best

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

Next

Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff