AM2315 I2C Temperature and Humidity Sensor. Tested on Nucleo STM32 F103RB with 3.3V supply and no pullup resistors and I2C level converter with 5V supply and pullup resistors as well.
Revision 0:770879aaecd5, committed 2014-09-25
- Comitter:
- pici
- Date:
- Thu Sep 25 18:39:45 2014 +0000
- Commit message:
- First Revision AM2315 I2C Temperature and Humidity Sensor.; Tested on Nucleo STM32 F103RB.
Changed in this revision
AM2315.cpp | Show annotated file Show diff for this revision Revisions of this file |
AM2315.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 770879aaecd5 AM2315.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AM2315.cpp Thu Sep 25 18:39:45 2014 +0000 @@ -0,0 +1,60 @@ +#include "AM2315.h" +#include "mbed.h" + + +AM2315::AM2315(PinName SDA = I2C_SDA , PinName SCL = I2C_SCL ):i2c(SDA, SCL) +{ +} + +// I2C Temperature and Humidity Sensor AM2315 +// +bool AM2315::read( ) +{ + + char data_write[5]; + char data_read[10]; + int i =0; + for(i=0; i<8; i++) + data_read[i]=0; + + // Wake up the sensor + // write single byte twice to wake up + // single write is not enough + data_write[0] = 0x00; + i2c.write(AM2315_ADDR,data_write,1,0); + i2c.write(AM2315_ADDR,data_write,1,0); + + // Read temperature and humidity register + // send request to AM2315 + data_write[0] = AM2315_REG_READ; + data_write[1] = 0x00; // read from adr 0x00 + data_write[2] = 0x04; // read 4 bytes + i2c.write(AM2315_ADDR, data_write, 3, 0); // with stop + + // wait 2ms before we start to read reg + wait_ms(2); + + i2c.read(AM2315_ADDR, data_read, 8, 1); + + if (data_read[0] != AM2315_REG_READ) + return false; + // check numbers of bytes read + if (data_read[1] != 4) + return false; + + humidity = data_read[2]; + humidity *= 256; + humidity += data_read[3]; + humidity /= 10; + + celsius = data_read[4] & 0x7F; + celsius *= 256; + celsius += data_read[5]; + celsius /= 10; + + if (data_read[4] >> 7) + celsius = -(celsius); + + return true; +} +
diff -r 000000000000 -r 770879aaecd5 AM2315.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AM2315.h Thu Sep 25 18:39:45 2014 +0000 @@ -0,0 +1,22 @@ +#ifndef MBED_AM2315_H +#define MBED_AM2315_H + +#include "mbed.h" + +#define AM2315_ADDR 0xB8 +#define AM2315_REG_READ 0x03 + + +class AM2315 +{ + public: + AM2315(PinName SDA , PinName SCL ); + bool read(); + + float celsius; + float humidity; + private: + I2C i2c; +}; + +#endif \ No newline at end of file