Arduino library for Aosong AM2320 temperature and humidity sensor.
A constructor should be called first. This is where you can obtain a variable of the sensor library.
AM2320();
None
#include "AM2320.h"
// create a variable of sensor library
AM2320 sensor;
This should be called once in void setup()
of an Arduino sketch.
void begin(void);
None
None
#include "AM2320.h"
AM2320 sensor;
void setup() {
// initialize sensor library
sensor.begin();
}
void loop() {
}
Same as Initialize the sensor with support for ESP8266 Arduino core. This is useful on ESP-01 where SDA and SCL pins aren't default ones.
void begin(int sda, int scl);
int sda
- pin number of sda line of I2C busint scl
- pin number of scl line of I2C bus
None
#include "AM2320.h"
AM2320 sensor;
void setup() {
// initialize sensor library with SDA and SCL pins
// https://github.com/esp8266/Arduino/blob/master/doc/libraries.md#i2c-wire-library
sensor.begin(0, 2);
}
void loop() {
}
Tell sensor to perform both temperature and humidity acquisitions.
bool measure();
None
true
- the operation is successfulfalse
- an error occurs. usegetErrorCode()
to get an error code. More info
You are encouraged to check a returned value of measure()
.
Chances are that the call to measure()
hits the sensor when it is sleeping.
This is normal behavior of AM2320 to prevent measurement errors from self-heating.
By checking the returned value, you can decide what to do.
See Example
Retrieve the latest temperature measurement from the call to measure()
float getTemperature();
None
Floating point number representing the temperature in degree celcius.
See Example
Retrieve the latest humudity measurement from the call to measure()
float getHumidity();
None
Floating point number representing the humudity in % RH (Relative Humidity).
See Example
Retrieve the error code of latest call to measure()
. Error code provides more detail on the errors.
int getErrorCode();
Return error code from latest operation.
None
Integer representing an error code. More info
See Example
This indicates no error. Everything works fine.
Sensor is offline. This happens when Arduino cannot connect to the sensor.
If this happens all the time from call to measure()
. It is likely the the sensor is connected incorrectly or
the sensor does not functional properly.
If this happens occasionally, It is possible that the call to measure()
hits the sensor when it is sleeping.
Try to slow down the call to measure()
.
CRC validation failed. This happends when data transmitted from the sensor is received with errors.
Possible causes may be bad connections, lengthy cable, etc.
// Include library into the sketch
#include <AM2320.h>
// Create an instance of sensor
AM2320 sensor;
void setup() {
// enable serial communication
// Include library into the sketch
#include <AM2320.h>
// Create an instance of sensor
AM2320 sensor;
void setup() {
// enable serial communication
Serial.begin(115200);
// call sensor.begin() to initialize the library
sensor.begin();
}
void loop() {
// sensor.measure() returns boolean value
// - true indicates measurement is completed and success
// - false indicates that either sensor is not ready or crc validation failed
// use getErrorCode() to check for cause of error.
if (sensor.measure()) {
Serial.print("Temperature: ");
Serial.println(sensor.getTemperature());
Serial.print("Humidity: ");
Serial.println(sensor.getHumidity());
}
else { // error has occured
int errorCode = sensor.getErrorCode();
switch (errorCode) {
case 1: Serial.println("ERR: Sensor is offline"); break;
case 2: Serial.println("ERR: CRC validation failed."); break;
}
}
delay(500);
}
Open an issue if something doesn't work right. Submit pull request if you want to improve something.