Tuesday, May 5, 2009

I2C on the NXT again! ( no: PCF8574A!)

We went back to the nxt for a moment.
We tried the PCF8574, (actually mindsensors.com sent PCF8574A) I2C chip of mindsensors.com with the arduino, that worked fine on a breadboard with the wire.h class of the ARDUINO example.

But the PCF8574A would not come alive with the NXT. We used the Anderson CPP lib, with bluetooth.
But we discovered again what we already did actually know: you cannot write registers on the brick using bluetooth, so the address is not reached.
(not very understandable at the moment, but we saw this too with the clock sensor....:-(((, it did cost a lot of time to figure this out.....why is the Anderson lib giving this possibility?? Did he succeed here?)

So we tried the program on the brick. We had to reflash it, and we stumbled over my multiple USB connections (nxt not found......but without the hub, USB directly into the laptop, the NXT was at last found.)

Then we had to figure out the i2c lejos class, we had this trouble with the former lib, (before the update) that it had the I2c addresses doubled, and indeed we found out from examples that some used 0x70 as the address.

But the last update (nov 2008 0.7) just uses the (official) address also used for the ARDUINO: 0x38.
(So not the 0x40 mentioned in the mindsensors.com documentation.)

Finally the LED's lighted up on the breedboard and on the mindsensors thing.

The only problem left is this one: sensor.sendData(address, byte); but this means i can steer only 4 pins of the PCF8574A (1111 = F is one byte)....(?).....how to steer all 8? (Solution is simple see rest of blog)

sensor.sendData(address, byte[], len); did not help......

anyway we used this code to steer half the PCF8574A:

import lejos.nxt.*;

public class I2C {
public static void main(String[] args) throws Exception {

I2CSensor sensor = new I2CSensor(SensorPort.S1) ;

int address = 0x38;
byte value = 0x0;
while (!Button.ESCAPE.isPressed())

{
sensor.setAddress(address);
LCD.drawInt(address,0,0);
value = 0x5;
sensor.sendData(address, value);
LCD.drawString("High",3,4);

Thread.sleep(500);

value = 0xA;
sensor.sendData(address, value);
LCD.drawString("low ",3,4);

Thread.sleep(500);
}
}
}

Then we searched for further solution of this last problem on the web and found this interesting application of the I2C sensor:

http://www.extremenxt.com/keyboard.html

What is this byte problem?
Eclipse registered a mistake in: sensor.sendData(address, 0xFF); -- saying this 0xFF is no byte. The solution is simple: sensor.sendData(address, (byte)0xFF); -- and you can steer your eigth pins!

ok the A0, A1, A2 pins can be used to attach rather a lot other PCF8574A chips to the same port. Because these pins make 8 base adresses possible, of chips steering 8 LEDS, makes 64 LEDS from one port!

my idea is to let the ARDUINO and the NXT talk to each other by way of the I2C chips.....then you have a ARDUINO with bluetooth connection...well in principle.....

it is nice to have some different controllers and consoles around, NXT ARDUINO, NDS (serial extension), IPHONE......it is very interesting to compare these systems. In the end the connections and the sensors are the same.....

last tutorial lejos: http://lejos.sourceforge.net/nxt/nxj/tutorial/index.htm

found a good descritpion of the PCF8574: http://www.i2cchip.com/pcf8574.html#PCF8574

at voti the PCF8574 costs 2,62 euro, http://www.voti.nl/winkel/catalog.html

ever wanted to know all about i2c: http://www.i2c-bus.org/

Last problem: when connecting the UltraSound distance sensor to the brick and steering the LEDS on the I2C "sensor" from the distance, refreshing all the time, we got a terrible effect of the LEDS between refresh all lighting up. So between indicating the distance we get a flash of all LEDS. This was still the case on a breadboard (without the mindsensor.com print). Not resolved.

the ARDUINO script can be found here: http://www.contrechoc.com/flash/arduino-i2c.txt
the JAVA script (made in eclipse) can be found here: http://www.contrechoc.com/flash/lejos-i2c.txt

2 comments:

esmetaman said...

I read your post, I liked so much.

Do you have a leJOS example with .PDE example?

cheers

underCover said...

i added the two scripts i used to test the PCF8574A with the NXT and the ARDUINO, the most important thing is to find the base address...