├── AM2320.pdf ├── AM2320_pins.jpg ├── keywords.txt ├── README.md ├── library.properties ├── src ├── AM2320.h └── AM2320.cpp └── examples └── AM2320_test └── AM2320_test.ino /AM2320.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EngDial/AM2320/HEAD/AM2320.pdf -------------------------------------------------------------------------------- /AM2320_pins.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EngDial/AM2320/HEAD/AM2320_pins.jpg -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | AM2320 KEYWORD1 2 | 3 | cTemp KEYWORD2 4 | Humidity KEYWORD2 5 | State KEYWORD2 6 | Read KEYWORD2 7 | startConvert KEYWORD2 8 | getData KEYWORD2 9 | AM2320_address LITERAL1 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino - AM2320 2 | ================= 3 | Library for working with the temperature and humidity sensor AM2320. 4 | 5 | The AM2320 sensor is connected via the I2C bus (Wire). 6 | 7 | ![AM2320_pins](https://github.com/EngDial/AM2320/blob/master/AM2320_pins.jpg) 8 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=AM2320 2 | version=1.0.2 3 | author=EngDial 4 | maintainer= 5 | sentence=Library for working with the temperature and humidity sensor AM2320. 6 | paragraph=The AM2320 sensor is connected via the I2C bus (Wire). 7 | category=Sensors 8 | url=https://github.com/EngDial/AM2320 9 | architectures=* 10 | includes=AM2320.h 11 | -------------------------------------------------------------------------------- /src/AM2320.h: -------------------------------------------------------------------------------- 1 | #ifndef AM2320_H 2 | #define AM2320_H 3 | 4 | #include 5 | #include 6 | 7 | #define AM2320_address (0xB8 >> 1) 8 | 9 | class AM2320 10 | { 11 | private: 12 | TwoWire* communication; 13 | public: 14 | AM2320(TwoWire* com_wire); 15 | float cTemp; 16 | float Humidity; 17 | uint8_t State; 18 | uint8_t Read(void); 19 | uint8_t startConvert(void); 20 | uint8_t getData(void); 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples/AM2320_test/AM2320_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | AM2320 th(&Wire); 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | Wire.begin(); 8 | } 9 | 10 | void loop() { 11 | Serial.println(F("Chip = AM2320")); 12 | switch(th.Read()) { 13 | case 2: 14 | Serial.println(F(" CRC failed")); 15 | break; 16 | case 1: 17 | Serial.println(F(" Sensor offline")); 18 | break; 19 | case 0: 20 | Serial.print(F(" Humidity = ")); 21 | Serial.print(th.Humidity); 22 | Serial.println(F("%")); 23 | Serial.print(F(" Temperature = ")); 24 | Serial.print(th.cTemp); 25 | Serial.println(F("*C")); 26 | Serial.println(); 27 | break; 28 | } 29 | delay(2000); 30 | } 31 | -------------------------------------------------------------------------------- /src/AM2320.cpp: -------------------------------------------------------------------------------- 1 | #include "AM2320.h" 2 | 3 | // ---=== Calc CRC16 ===--- 4 | uint16_t CRC16(uint8_t *ptr, uint8_t length) 5 | { 6 | uint16_t crc = 0xFFFF; 7 | uint8_t i; 8 | //------------------------------ 9 | while(length--) 10 | { 11 | crc ^= *ptr++; 12 | for (i = 0; i < 8; i++) 13 | if ((crc & 0x01) != 0) 14 | { 15 | crc >>= 1; 16 | crc ^= 0xA001; 17 | } 18 | else 19 | crc >>= 1; 20 | } 21 | return crc; 22 | } 23 | 24 | // ---=== Constructor ===--- 25 | AM2320::AM2320(TwoWire* com_wire) 26 | { 27 | communication = com_wire; 28 | } 29 | 30 | // ---=== Start convertion ===-- 31 | uint8_t AM2320::startConvert() 32 | { 33 | State = 1; 34 | // Wake 35 | communication->beginTransmission(AM2320_address); 36 | communication->endTransmission(); 37 | // Send request for read 4 bytes 38 | communication->beginTransmission(AM2320_address); 39 | communication->write(0x03); // request 40 | communication->write(0x00); // from 0 address 41 | communication->write(0x04); // 4 bytes for read 42 | State = communication->endTransmission(1); 43 | return State; 44 | } 45 | 46 | uint8_t AM2320::getData() 47 | { 48 | uint8_t buf[8]; 49 | uint8_t i; 50 | uint16_t crc_temp; 51 | //------------------------------ 52 | State = 2; 53 | for (i = 0; i < 8; i++) buf[i] = 0x00; 54 | // Read data (8 byte = (2 Header)+(2 Hum)+(2 Temp)+(2 CRC16)) 55 | communication->requestFrom(AM2320_address, 8); 56 | for (i = 0; i < 8; i++) buf[i] = communication->read(); 57 | // Check CRC16 58 | crc_temp = buf[7] << 8; 59 | crc_temp += buf[6]; 60 | if (crc_temp == CRC16(buf, 6)) 61 | { 62 | State = 0; 63 | cTemp = (((buf[4] & 0x7F) << 8) + buf[5]) / 10.0; 64 | cTemp = (buf[4] & 0x80) ? -cTemp : cTemp; 65 | Humidity = ((buf[2] << 8) + buf[3]) / 10.0; 66 | } 67 | return State; 68 | } 69 | 70 | // Read 71 | uint8_t AM2320::Read() 72 | { 73 | if (startConvert() != 0) return State; 74 | delay(2); // delay 2 ms 75 | return getData(); 76 | } 77 | --------------------------------------------------------------------------------