Sunday, April 5, 2009

Sensors in the LEGO NXT MINDSTORM are classified into three categories

1) Passive Sensors like touch etc.
2) Active sensors like LightSensors.
3) Digital sensors like Sonar.

These Digital sensors have a brain of their own i.e. a microcontroller in them. So they can make
directly communicate with the ARM7 processor in the NXT brick using I2C protocol.

I have spent almost a week learning the I2C protocol and then trying to understand how the communication takes place in the NXT. Initially I was baffled and thought LEGO has made the
communication complex to prevent and third parties from understanding it, but i guess I was
wrong . It is very simple and elegent... well almost.

An understanding of the I2C protocol is mandatory to continue reading. Also have the LEGO
HARDWARE Developers kit ready at hand.

The Hardware Developers Kit says that the sensors are treated as memory devices and each
transaction is treated as a read or write into a memory location.
So each digital sensor has two types of addresses:
1) An I2C address unique to each digital sensor
2) Internal Addresses in the sensor to wirte to or read from.

Taking the UltraSonic sensor as an example, its I2C address is 1(7 bit address) and has a
number of internal addresses as given in Appendix 7 of the Hardwares Developers Kit.

The NXT can only act as a master and the sensor is always the slave.

Its best to proceed with an example. To read the version of the Ultrasonic sensor, Appendix 7
says that the NXT, which is the master, must send the byte sequence as follows
1) 0x02 (which is the 7bit sensor address 0x01 and the 8th bit is a zero which indicates
that Master is Transmitted and Slave is receiver)
2) 0x00 (The internal address to read from. So now the slave i.e. the sensor knows that the
master is interested in the address location 0x00 but still does not know if its a read
or write transaction)
3) R + 0x03 (This was the most baffling part! What is R?? What is 0x03?? i guess a seasoned
I2C protocol expert will takes seconds to decode it. It took me well 2 hours and lot
of goggling :( . )

The R indicates "Repeated Start" which is a well know state in I2C protocol. The master does
not relinquish the bus but starts a new message. 0x03 is the 7bit device address (0x01 and the
8th bit is a 1 indicating that Master is Receiver and Slave is Transmitter). So now the slave
knows that in this transaction the NXT brick wants to read some data from a memory location.

If the third byte is not preceeded by a repeated Start, the NXT indicates that it is a write
transaction.

Understanding till here, I could use an ATMega8 controller and get the NXT brick to read a few memory locations.

Since I am feeling a bit lazy now and have written almost everything I wanted to shere.. I will
stop for now. I still need to figure out some tiny but important details like how the master knows
exactly how many bytes to read etc etc... will keep blogging once I get it cleared!

4 comments:

  1. i cant wait for ur next post! im completely baffled by the whole i2c thing and its such a pity.. i demand a tutorial! lol..

    ReplyDelete
  2. Hi, am confused, you know how is the protocol of i2c? i tried comunicated the ultrasonic sensor with a microcontroller but i can't,

    the protocol or structure is:

    Start-1010-Adress device-R/W-Adress High-Adress low-Data-Stop? or is more simple?

    ReplyDelete
  3. I wanna know how is the structure for send and received data.. If you know please help me.

    ReplyDelete
  4. WHat about the servo? is it an I2c device or not?

    ReplyDelete