Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 1 month ago.
Read temperature from AD7415 I2C Sensor
How can i read temperature from i2c sensor AD7415? This is the sensor datasheet: http://www.analog.com/static/imported-files/data_sheets/AD7414_7415.pdf .
I try configure the i2c frequency to 1/2.5us = 400000Hz, i have the right address but when i try to read or write to i2c bus the functions returns me a non-0 value. Read and Write function returns non-0 on failure and 0 on succes.
This is the code i tried to use:
And here is the output: Temp = 0.00 Write 1:32 Write 2: 32 Read: 72
3 Answers
11 years ago.
The tmp calculation seems wrong, are you getting the result you expect? the AD7414/15 returns a 10bit result in 2 bytes MSB first:
[MSB,B8,B7,B6,B5,B4,B3,B2] [B1,LSB]
with MSB as sign bit (1= temperature below 0 Celcius).
here is what i did, suggestions for improvements are welcome;
float ad7414::read(void) { _i2c.read(_addr, cmd, 2); // read the two-byte temperature result [MSB,B8,B7,B6,B5,B4,B3,B2] [B1,LSB] bool sign = cmd[0] & 0x80; //read signed bit (neg/pos temperature) int adc = ((cmd[0] & 0x7f)<< 2) + (cmd[1] >> 6); //make one 10 value if(!sign) return (float)adc/4; //positive temp calulation. return ((float)adc-512)/4; //negative temp calculation. }
also, the write operations are not needed as the power up default has it running in continuous mode with register pointer set to 0 (temperature register) so the 2-byte read is enough, in fact, w1 will attempt to write a 0x01 to the temperature register(read only)
I do have a halfbaked library for the AD7414/15 I don't need it anymore but i will finish it upon request.
Thank you! The temperature calculation was wrong, initially I tried to adapt the code found in Handbook. I did something similar after I had the right address. Now my code looks like this:
i2c.read(address, cmd, 2); // Reading Two Bytes of Data from the Temperature Value Register if(cmd[0] & 128) // check if the temperature is negative (MSB is 0) to know what formula to use temperature = float(((cmd[0]<<2) | (cmd[1] >> 6)) - 512) / 4; else temperature = float((cmd[0]<<2) | (cmd[1] >> 6)) / 4;
11 years, 1 month ago.
It is handier to paste your code between <<code>> and <</code>>
, less work for you, easier for us to copy/paste.
These status codes mean that no device acknowledged the address sent on the bus. And it also looks like your address is wrong: you used the 7-bit address, but you need the 8-bit address, which is equal to the 7-bit address multiplied by two: 0x94.
11 years, 1 month ago.
Thank you, the code work just fine now.
#include "mbed.h" int main() { printf("Thanks for the code posting tip!"); }