Arduino library for AM2320 temperature and humidity sensor.
AM2320();
A Constructor which should be called first to create an instace(variable) of sensor library.
None
An instance of AM2320 class.
#include "AM2320.h"
// create a variable of sensor library
AM2320 sensor;
void begin(void);
Initialize the AM2320 sensor library.
None
None
#include "AM2320.h"
AM2320 sensor;
void setup() {
// initialize sensor library
sensor.begin();
}
void loop() {
}
void begin(int sda, int scl);
Initialize the AM2320 sensor library with specified SDA and SCL pins.
This is useful on ESP-01 where SDA and SCL pins aren't default ones.
- sda - pin number of sda line
- scl - pin number of scl line
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() {
}
bool measure();
Tell the sensor to perform both temperature and humidity acquisition.
None
- true - the operation is successful
- false - an error occurs. use
getErrorCode()
to get an error code. More info
int getTemperature();
Return temperature from latest acquisition.
None
Integer represent temperature in degree celcius.
int getHumidity();
Return humudity from latest acquisition.
None
Integer representing humudity in % RH(Relative Humidity).
int getErrorCode();
Return error code from latest operation.
None
Integer representing an error code. More info
This indicates no error. Everything works fine.
Sensor is not online. This happens when Arduino cannot connect to the sensor. Possible causes may be bad connections, wrong wirings, etc. You may want to use I2CScanner sketch to check if Arduino can find the sensor or not.
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>
#include <Wire.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 not online"); break;
case 2: Serial.println("ERR: CRC validation failed."); break;
}
}
delay(500);
}
Part 1 Part 2 .. working on it