mbed NXP-LPC1768 using LiquidWare Temperature and Humidity Sensor with LibHumidity.h

21 Jul 2012

Hey fellas,

Please reference http://www.liquidware.com/system/0000/3180/Humidity_Sensor_Cheatsheet.pdf.

Board: NXP-LPC1768 Firmware: 21164 Sensor: SHT21 Humidity and Temperature Sensor

I once had the simple project listed in the .pdf file above working, but that was on an Arduino board. I would like to make this sensor work for an mbed board.

The code I have is as follows, and I believe I have it nearly completely ported over from the Arduino style function calls, to the mbed style function calls, although, please pay special attention to lines 138 and 140 of LibHumidity.cpp, where I am attempting to cast a char * to uint8_t.


Question 1: Is my casting correct? Does it look as if something is incorrect?

Question 2: Why would I be getting incorrect results on the serial output, and further, why are the outputs not changing when I change the humidity or the temperature of the room??

Lastly, I am aware that Arduino uses 5V, and mbed uses 3V3, although, I am providing 5V to the board itself from pin 39, USB 5.0V.

Any help would be greatly appreciated.



21 Jul 2012

I am not 100% sure if the mbed will handle the hold on the SCL line correctly. Did you already check if you get an ACK back during communication?

Anyway this is the code you wondered about:

    char *data;
    i2c.read(eSHT21Address, data, 3, false);
    result = ((uint8_t)data << 8);
    i2c.read(eSHT21Address, data, 3, false);
    result += (uint8_t)data;

The casting shouldn't be a problem (no reason that I know of to add the (uint8_t)), but you read 3 bytes from the sensor, and you only have room to store one. It would again be useful to check if you get an ACK, but you don't need the second read. Define data as char array with length 3, then do the same with the first two elements of the array to create the result. And you can also have a look at the checksum in the third byte, if it is what you expect.

Also useful to know, which incorrect results are you getting? In principle that error should only corrupt the LSBs, which leaves the MSBs correct, so you would expect your result to be generally correct. But the function now will write data in memory locations it really shouldnt be writing, so who knows what it is doing.

21 Jul 2012

Erik is correct about the fact that you need an array to store the three received bytes. The SCL hold should work. That is standard I2C hardware functionality. Depending on the hardware module for the sensor you may need to look into the signal voltage levels since it was originally intended for a 5V arduino and verify if you need pull up resistors on the i2c lines. Also note that the I2C slave address should be defined as 0x80 for the mbed i2c lib. The lib uses the 8 bit convention rather than the 7bit convention used by the arduino lib.