go_away

Author Topic: Interfacing Lego NXT ultrasonic sensor with avrlib  (Read 5011 times)

0 Members and 1 Guest are viewing this topic.

Offline vigilantTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Interfacing Lego NXT ultrasonic sensor with avrlib
« 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. :)


Offline Qubix

  • Full Member
  • ***
  • Posts: 55
  • Helpful? 1
Re: Interfacing Lego NXT ultrasonic sensor with avrlib
« Reply #1 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
http://robots.ie  - Ireland's robot site

Offline vigilantTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: Interfacing Lego NXT ultrasonic sensor with avrlib
« Reply #2 on: October 16, 2008, 10:33:16 AM »
Unfortunately ultrasonic sensor related stuff is not implemented in the library yet... interesting project nevertheless. :)

 


Get Your Ad Here

data_list