Skip to content
Arduino library for AM2320 temperature and humidity sensor.
Branch: master
Clone or download
hibikiledo Merge pull request #3 from per1234/keywords-separator
Use correct field separator in keywords.txt
Latest commit 45cde3d Jul 17, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples/AM2320-Example Update example Nov 6, 2016
.gitignore Add library implementation Nov 4, 2016
AM2320.cpp Allow sda and scl pins to be configured Apr 12, 2018
AM2320.h Allow sda and scl pins to be configured Apr 12, 2018
README.md Update README.md Apr 12, 2018
keywords.txt Use correct field separator in keywords.txt Jul 17, 2018

README.md

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
// 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.

You can’t perform that action at this time.