Skip to content

Latest commit

 

History

History
208 lines (157 loc) · 3.84 KB

README.md

File metadata and controls

208 lines (157 loc) · 3.84 KB

AM2320

Arduino library for AM2320 temperature and humidity sensor.

Downloads

Latest Version

API Reference

AM2320();

Summary

A Constructor which should be called first to create an instace(variable) of sensor library.

Parameters

None

Return Value

An instance of AM2320 class.

Example

#include "AM2320.h"

// create a variable of sensor library
AM2320 sensor;

void begin(void);

Summary

Initialize the AM2320 sensor library.

Parameters

None

Return Value

None

Example

#include "AM2320.h"
AM2320 sensor;

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

void loop() {

}

void begin(int sda, int scl);

Summary

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.

Parameters

  • sda - pin number of sda line
  • scl - pin number of scl line

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

}

bool measure();

Summary

Tell the sensor to perform both temperature and humidity acquisition.

Parameters

None

Return Value

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

int getTemperature();

Summary

Return temperature from latest acquisition.

Parameters

None

Return Value

Integer represent temperature in degree celcius.


int getHumidity();

Summary

Return humudity from latest acquisition.

Parameters

None

Return Value

Integer representing humudity in % RH(Relative Humidity).


int getErrorCode();

Summary

Return error code from latest operation.

Parameters

None

Return Value

Integer representing an error code. More info


Error Codes

Code 0

This indicates no error. Everything works fine.

Code 1

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.

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.

Usage 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>
#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);
}

The making of AM2320 library

Part 1 Part 2 .. working on it