I2C Class Library - problems with write() and read() primitives

27 Dec 2012

Hi, I'm testing the mbed with a temperature/humidity I2C sensor (Sensirion SHT21) connected on port p27/p28.

I'm experiencing the below problems using the I2C class library.

Using the functions write(int address, char *data, int length, bool repeated) and read(int address, char *data, int length, bool repeated) everything works fine, the sensor answer to commands sent via I2C.

Now, I would like to have a function that checks if the SHT21 is connected on the I2C bus, so I write this small piece of code:

#define SHT21_ADDRESS 128

int SHT21_Check(void) {
    int ack;
    i2c.start();
    ack = i2c.write(SHT21_ADDRESS);
    i2c.stop();
    return ack;
}

The problem is that the i2c.write() returns always 0 (and reading the documentation 0 means NoACK, while 1 means ACK), but using an oscilloscope I can see that the sensor acks its address correctly. I've tried to change the SHT21_ADDRESS to another value, on the oscilloscope I see that the SHT21 sends NoACK but the i2c.write() returns always 0.

This is an image of the oscilloscope when the SHT21_ADDRESS is acked (but the i2c.write() returns 0): /media/uploads/samueleforconi/27122012148.jpg

I can't understand where I'm wrong with this code. Thanks for any help/suggestions.

Samuele.

27 Dec 2012

Writing address is a special case internally in the LPC1768 regarding status it returns, so maybe they didnt check for the address ack in the mbed library (so a bug in that case).

What I use for this purpose is the 'normal' write function:

i2c.write(SHT21_ADDRESS, NULL, 0);

Don't forget this one returns 0 on success and something else on failure.