├── library.properties ├── examples ├── AM2315_I2C │ └── AM2315_I2C.ino └── AM2315_TCA9548A_I2C │ └── AM2315_TCA9548A_I2C.ino ├── README.md ├── AM2315_I2C.h └── AM2315_I2C.cpp /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP8266 AM2315 Library 2 | 3 | version=2.2.1 4 | 5 | author=Mobizt 6 | 7 | maintainer=Mobizt 8 | 9 | sentence=ESP8266 AM2315 Library 10 | 11 | paragraph=ESP8266 AM2315 Library 12 | 13 | category=Sensors 14 | 15 | url=hhttps://github.com/mobizt/ESP8266-AM2315 16 | 17 | architectures=esp32 18 | -------------------------------------------------------------------------------- /examples/AM2315_I2C/AM2315_I2C.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Test setup for WEMOS D1 Mini (ESP8266) and AM2315 3 | 4 | --------------------------------------------------------------------------------- 5 | WEMOS D1 Mini AM2315 Supply 6 | --------------------------------------------------------------------------------- 7 | D2 (SDA) Yelloow (SDA) 4.7 k Pull-Up Resistor 8 | D1 (SCL) White (SCL) 4.7 k Pull-Up Resistor 9 | Red (VDD) +3.3 10 | GND Black (GND) GND 11 | 12 | */ 13 | 14 | 15 | #include 16 | 17 | //ESP8266 I2C Pins 18 | const uint8_t dataPin = D2; 19 | const uint8_t clockPin = D1; 20 | 21 | AM2315_I2C am2315; 22 | 23 | void setup() { 24 | Serial.begin(115200); 25 | 26 | Wire.pins(dataPin, clockPin); 27 | 28 | delay(100); 29 | 30 | Serial.println(); 31 | 32 | am2315.begin(dataPin, clockPin); 33 | 34 | if (!am2315.dataReady) 35 | Serial.println("AM2315 sensor was not found"); 36 | else 37 | Serial.println("AM2315 sensor is ready"); 38 | 39 | } 40 | 41 | void loop() { 42 | 43 | //Non-blocking data reading 44 | am2315.acquireData(); 45 | 46 | if (am2315.dataReady) { 47 | 48 | Serial.print("Humidity: "); 49 | Serial.println(am2315.humidity()); 50 | Serial.print("Temperature: "); 51 | Serial.println(am2315.temperature()); 52 | Serial.println(); 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AM2315 Digital Temp & Humid Sensor Arduino Library for ESP8266 2 | AM2315 I2C Library for Arduino 3 | 4 | ## Test Devices 5 | Aosong AM2315 6 | 7 | ## Public functions 8 | 9 | ```c++ 10 | 11 | void begin(uint8_t dataPin, uint8_t clockPin); 12 | void acquireData(); 13 | float temperature(); 14 | float humidity(); 15 | 16 | ``` 17 | ## Usages 18 | 19 | ```c++ 20 | 21 | /* 22 | Test setup for WEMOS D1 Mini (ESP8266) and AM2315 23 | 24 | --------------------------------------------------------------------------------- 25 | WEMOS D1 Mini AM2315 Supply 26 | --------------------------------------------------------------------------------- 27 | D2 (SDA) Yelloow (SDA) 4.7 k Pull-Up Resistor 28 | D1 (SCL) White (SCL) 4.7 k Pull-Up Resistor 29 | Red (VDD) +3.3 30 | GND Black (GND) GND 31 | 32 | */ 33 | 34 | 35 | #include 36 | 37 | //ESP8266 I2C Pins 38 | const uint8_t dataPin = D2; 39 | const uint8_t clockPin = D1; 40 | 41 | AM2315_I2C am2315; 42 | 43 | void setup() { 44 | Serial.begin(115200); 45 | 46 | Wire.pins(dataPin, clockPin); 47 | 48 | delay(100); 49 | 50 | Serial.println(); 51 | 52 | am2315.begin(dataPin, clockPin); 53 | 54 | if (!am2315.dataReady) 55 | Serial.println("AM2315 sensor was not found"); 56 | else 57 | Serial.println("AM2315 sensor is ready"); 58 | 59 | } 60 | 61 | void loop() { 62 | 63 | //Non-blocking data reading 64 | am2315.acquireData(); 65 | 66 | if (am2315.dataReady) { 67 | 68 | Serial.print("Humidity: "); 69 | Serial.println(am2315.humidity()); 70 | Serial.print("Temperature: "); 71 | Serial.println(am2315.temperature()); 72 | Serial.println(); 73 | 74 | } 75 | 76 | } 77 | 78 | ``` 79 | -------------------------------------------------------------------------------- /AM2315_I2C.h: -------------------------------------------------------------------------------- 1 | /* 2 | * AM2315 Temperature and Humidity Sensor I2C library for Arduino 3 | * 4 | * This library provides microcontroller to read data from Aosong Digital Temperature 5 | * and Humidity sensor through I2C interface with non-blocking operation. 6 | * 7 | * The library was also work well with AM2315 compatible sensors and WEMOS D1 Mini (ESP8266) using 8 | * the Arduino's standard I2C library. 9 | * 10 | * The MIT License (MIT) 11 | * Copyright (c) 2018 K. Suwatchai (Mobizt) 12 | * 13 | * 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 15 | * this software and associated documentation files (the "Software"), to deal in 16 | * the Software without restriction, including without limitation the rights to 17 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 18 | * the Software, and to permit persons to whom the Software is furnished to do so, 19 | * subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in all 22 | * copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 26 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 27 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 28 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | */ 31 | 32 | #ifndef AM2315_I2C_H 33 | #define AM2315_I2C_H 34 | 35 | #include "Arduino.h" 36 | #include "Wire.h" 37 | 38 | #define AM2315_I2C_ADDR 0x5C //I2C address 0xB8 (1011 1000) or 7-bit address 0x5c (101 1100) 39 | #define AM2315_READ_REG 0x03 40 | 41 | 42 | typedef union { 43 | byte byteData [2]; 44 | uint16_t uint16Data; 45 | } AM2315_I2C_dataUnion; 46 | 47 | 48 | class AM2315_I2C { 49 | public: 50 | AM2315_I2C(); 51 | void begin(uint8_t dataPin, uint8_t clockPin); 52 | void acquireData(); 53 | float temperature(); 54 | float humidity(); 55 | bool dataReady; 56 | 57 | private: 58 | float _humid, _temp; 59 | AM2315_I2C_dataUnion _dataUnion; 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /examples/AM2315_TCA9548A_I2C/AM2315_TCA9548A_I2C.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Test setup for WEMOS D1 Mini (ESP8266), two TCA9548As (8-Channel I2C Switch Module) and AM2315 3 | 4 | -------------------------------------------------------------------------------------------------------- 5 | WEMOS D1 Mini TCA9548A AM2315#1 AM2315#2 Supply 6 | -------------------------------------------------------------------------------------------------------- 7 | D2 (SDA) SDA 4.7 k Pull-Up Resistor 8 | D1 (SCL) SCL 4.7 k Pull-Up Resistor 9 | VIN Red (VDD) Red (VDD) +3.3 10 | GND GND Black (GND) Black (GND) GND 11 | SD0 Yelloow (SDA) 4.7 k Pull-Up Resistor 12 | SC0 White (SCL) 4.7 k Pull-Up Resistor 13 | SD1 Yelloow (SDA) 4.7 k Pull-Up Resistor 14 | SC1 White (SCL) 4.7 k Pull-Up Resistor 15 | */ 16 | 17 | #include "Wire.h" 18 | #include 19 | 20 | //ESP8266 I2C Pins 21 | const uint8_t dataPin = D2; 22 | const uint8_t clockPin = D1; 23 | 24 | #define MUX_Address 0x70 // TCA9548A I2C address 25 | 26 | AM2315_I2C am2315; 27 | 28 | void setup() { 29 | 30 | Serial.begin(115200); 31 | 32 | Wire.pins(dataPin, clockPin); 33 | 34 | delay(100); 35 | 36 | Serial.println(); 37 | 38 | selectI2CChannels(0); 39 | 40 | am2315.begin(dataPin, clockPin); 41 | 42 | if (!am2315.dataReady) 43 | Serial.println("Sensor 0 was not found"); 44 | else 45 | Serial.println("Sensor 0 is OK"); 46 | 47 | selectI2CChannels(1); 48 | 49 | am2315.begin(dataPin, clockPin); 50 | 51 | if (!am2315.dataReady) 52 | Serial.println("Sensor 1 was not found"); 53 | else 54 | Serial.println("Sensor 1 is OK"); 55 | } 56 | 57 | void selectI2CChannels(uint8_t i) { 58 | if (i > 7) return; 59 | Wire.pins(dataPin, clockPin); 60 | Wire.begin(dataPin, clockPin); 61 | Wire.beginTransmission(MUX_Address); 62 | Wire.write(1 << i); 63 | Wire.endTransmission(); 64 | delayMicroseconds(2000); 65 | } 66 | 67 | void loop() { 68 | 69 | //Non-blocking data reading 70 | selectI2CChannels(0); 71 | am2315.acquireData(); 72 | 73 | if (am2315.dataReady) { 74 | 75 | Serial.print("Humidity 0: "); 76 | Serial.println(am2315.humidity()); 77 | Serial.print("Temperature 0: "); 78 | Serial.println(am2315.temperature()); 79 | Serial.println(); 80 | 81 | } else { 82 | Serial.println("Sensor 0 Failed!"); 83 | Serial.println(); 84 | } 85 | 86 | 87 | selectI2CChannels(1); 88 | am2315.acquireData(); 89 | 90 | if (am2315.dataReady) { 91 | 92 | Serial.print("Humidity 1: "); 93 | Serial.println(am2315.humidity()); 94 | Serial.print("Temperature 1: "); 95 | Serial.println(am2315.temperature()); 96 | Serial.println(); 97 | 98 | } else { 99 | Serial.println("Sesor 1 Failed!"); 100 | Serial.println(); 101 | } 102 | 103 | 104 | } 105 | -------------------------------------------------------------------------------- /AM2315_I2C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | AM2315 Temperature and Humidity Sensor I2C library for Arduino 3 | 4 | This library provides microcontroller to read data from Aosong Digital Temperature 5 | and Humidity sensor through I2C interface with non-blocking operation. 6 | 7 | The library was also work well with AM2315 compatible sensors and WEMOS D1 Mini (ESP8266) using 8 | the Arduino's standard I2C library. 9 | 10 | The MIT License (MIT) 11 | Copyright (c) 2018 K. Suwatchai (Mobizt) 12 | 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy of 15 | this software and associated documentation files (the "Software"), to deal in 16 | the Software without restriction, including without limitation the rights to 17 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 18 | the Software, and to permit persons to whom the Software is furnished to do so, 19 | subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in all 22 | copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 26 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 27 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 28 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | */ 31 | 32 | #include "AM2315_I2C.h" 33 | 34 | AM2315_I2C::AM2315_I2C() {} 35 | 36 | void AM2315_I2C::begin(uint8_t dataPin, uint8_t clockPin) { 37 | 38 | Wire.setClock(400000L); //Set rate to 400 kbps 39 | Wire.begin(dataPin, clockPin); 40 | delayMicroseconds(2000); 41 | acquireData(); 42 | 43 | } 44 | 45 | void AM2315_I2C::acquireData() { 46 | 47 | dataReady = false; 48 | 49 | uint8_t totalReg = 4; 50 | uint8_t regData[totalReg + 2]; 51 | 52 | //Wake up 53 | Wire.beginTransmission(AM2315_I2C_ADDR); 54 | Wire.write(AM2315_READ_REG); 55 | Wire.endTransmission(); 56 | delayMicroseconds(2000); 57 | 58 | //Send the data request 59 | Wire.beginTransmission(AM2315_I2C_ADDR); 60 | Wire.write(AM2315_READ_REG); 61 | Wire.write(0x00); //Reg address offset 62 | Wire.write(totalReg); //Number of reg to read 63 | Wire.endTransmission(); 64 | delayMicroseconds(2000); 65 | 66 | 67 | 68 | Wire.requestFrom(AM2315_I2C_ADDR, totalReg + 2); 69 | 70 | for (uint8_t i = 0; i < totalReg + 2; i++) 71 | regData[i] = Wire.read(); 72 | 73 | 74 | if (regData[0] == AM2315_READ_REG && regData[1] == totalReg) { 75 | 76 | 77 | _dataUnion.byteData[0] = regData[3]; 78 | _dataUnion.byteData[1] = regData[2]; 79 | _humid = (float)_dataUnion.uint16Data / 10; 80 | 81 | _dataUnion.byteData[0] = regData[5]; 82 | _dataUnion.byteData[1] = regData[4] & 0x7f; 83 | _temp = (float)_dataUnion.uint16Data / 10; 84 | if (regData[4] & 0x80) _temp *= -1; //If bit 15 is set, give the negative temp value 85 | 86 | dataReady = true; 87 | 88 | } 89 | 90 | } 91 | 92 | 93 | float AM2315_I2C::temperature() { 94 | if (!dataReady) return NAN; 95 | return _temp; 96 | } 97 | 98 | float AM2315_I2C::humidity() { 99 | if (!dataReady) return NAN; 100 | return _humid; 101 | } 102 | --------------------------------------------------------------------------------