Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: vigilant on October 16, 2008, 01:57:25 AM

Title: Interfacing Lego NXT ultrasonic sensor with avrlib
Post by: vigilant on October 16, 2008, 01:57:25 AM
Hi!

I'm trying to interface the Lego NXT ultrasonic sensor with an atmega168 microcontroller, using avrlib. I can't get it to work properly though. The sensor is attached to the microcontroller this way:

pin1 -> ~8V
pin2 -> GND
pin3 -> GND
pin4 -> 5V
pin5 -> pin28 of atmega168 (SCL)
pin6 -> pin27 of atmega168 (SDA)

The code used to initialize I2C:

Code: [Select]
i2cInit();
i2cSetBitrate(10);

The following code is used to get the sensor type & read measurements:

Code: [Select]
void get_sensor_type(void)
{
u08 data[8] = {0};
data[0] = 0x10;

i2cMasterSendNI(0x02, 1, data);
i2cMasterReceiveNI(0x02, 8, data);

        ...
}

u08 nxt_ultrasonic_scan(void)
{
        u08 data[8] = {0};
 
        data[0] = 0x42;
        i2cMasterSendNI(0x02, 1, data);
        i2cMasterReceiveNI(0x02, 1, data);

        return data[0];
}

I modified the avrlib functions a bit, to see whether I'm getting ACKs properly. It seems i2cMasterSendNI() function always succeeds, I get an ACK for all the commands. However, i2cMasterReceiveNI() always fails at the point where I'm sending SLA+R. Depending on the configuration, one of the following things happen:

* The i2cWaitForComplete() function after sending SLA+R never returns
* The i2cWaitForComplete() function does return, but with TW_BUS_ERROR error
* The i2cWaitForComplete() function does return, but with TW_MR_SLA_NACK error

Additionally, I've tried:
* Adding delays (from 200us to 500ms) after each read/write.
* Adding external pull-ups to the board (82K)
* To generate REPEATED START, I've tried sending all combinations of START, STOP and STOP/START. None of them work. STOP/START always generates TW_MR_SLA_NACK error as described above. Plan STOP / START always generate a TW_BUS_ERROR or a never returning i2cWaitForComplete() after sending SLA+R.

So basically, it seems I'm able to send stuff to the device, but unable to read anything from it. Has somebody here been able to interface the NXT Ultrasonic sensor with an external microcontroller? Help needed here. :)

Title: Re: Interfacing Lego NXT ultrasonic sensor with avrlib
Post by: Qubix on October 16, 2008, 07:01:18 AM
This guy has made a NXT library for the arduino (which uses a atmega168)

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1221664564/

Not sure if its finished yet, but the NXT library can be found here, might give you some ideas

http://sourceforge.net/projects/a2l/

Hope its of some help.

David
Title: Re: Interfacing Lego NXT ultrasonic sensor with avrlib
Post by: vigilant on October 16, 2008, 10:33:16 AM
Unfortunately ultrasonic sensor related stuff is not implemented in the library yet... interesting project nevertheless. :)