Skip to content

hibikiledo/AM2320

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AM2320

Arduino library for Aosong AM2320 temperature and humidity sensor.

Downloads

Latest Version

API Reference

Constructor

A constructor should be called first. This is where you can obtain a variable of the sensor library.

Signature

AM2320();

Parameters

None

Usage Example

#include "AM2320.h"

// create a variable of sensor library
AM2320 sensor;

Initialize the sensor

This should be called once in void setup() of an Arduino sketch.

Signature

void begin(void);

Parameters

None

Return Value

None

Usage Example

#include "AM2320.h"
AM2320 sensor;

void setup() {
  // initialize sensor library
  sensor.begin();
}

void loop() {

}

Initialize the sensor with specific SDA and SCL pin numbers

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.

Signature

void begin(int sda, int scl);

Parameters

  • int sda - pin number of sda line of I2C bus
  • int scl - pin number of scl line of I2C bus

Return Value

None

Example

#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() {

}

Measure temperature and humidity (%RH)

Tell sensor to perform both temperature and humidity acquisitions.

Signature

bool measure();

Parameters

None

Return Value

  • true - the operation is successful
  • false - an error occurs. use getErrorCode() to get an error code. More info

Usage Example

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


Get the temperature

Retrieve the latest temperature measurement from the call to measure()

Signature

float getTemperature();

Parameters

None

Return Value

Floating point number representing the temperature in degree celcius.

Usage Example

See Example


Get the humidity

Retrieve the latest humudity measurement from the call to measure()

Signature

float getHumidity();

Parameters

None

Return Value

Floating point number representing the humudity in % RH (Relative Humidity).

Usage Example

See Example


Get error code

Retrieve the error code of latest call to measure(). Error code provides more detail on the errors.

Signature

int getErrorCode();

Summary

Return error code from latest operation.

Parameters

None

Return Value

Integer representing an error code. More info

Usage Example

See Example

Error Codes

Code 0

This indicates no error. Everything works fine.

Code 1

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().

Code 2

CRC validation failed. This happends when data transmitted from the sensor is received with errors.

Possible causes may be bad connections, lengthy cable, etc.

Example

// 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);
}

Contributions

Open an issue if something doesn't work right. Submit pull request if you want to improve something.

About

Arduino library for AM2320 temperature and humidity sensor.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages