Arduino code and Video for Aosong AM2320 Digital Temperature and Humidity Sensor
This is the Arduino code for Aosong AM2320 Digital Temperature and Humidity Sensor
This page shows you how to use Aosong AM2320 Digital Temperature and Humidity Sensor and provides the Arduino code to download.
Pins
- VCC to 5V
- GNG to GND
- SDA to A5 or SDA (for uno)
- SCL to A4 or SCL (for uno)
As a reference the table below shows where TWI pins are located on various Arduino boards. See Arduino Wire
Board | I2C / TWI pins |
Uno, Ethernet | A4 (SDA), A5 (SCL) |
Mega2560 | 20 (SDA), 21 (SCL) |
Leonardo | 2 (SDA), 3 (SCL) |
Due | 20 (SDA), 21 (SCL), SDA1, SCL1 |
- Arduino Wire
- AM2320 Library (from Gethub)
- AM2320 Library (from Robojax.com)
- Aosong AM2320 Data sheet
/**
This is Arduino code for Aosong Digital Temperature and Humidity Sensor
This code is presented as part of Robojax tutorial.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2016 Ratthanan Nalintasnai
Modefied for Robojax.com video by
Ahmad S. on March 22, 2018 at 22:45 at Ajax, Ontario, Canada
This code with library and other codes are available at
http://robojax.com/learn/arduino/
Watch The video instruction for this code: https://youtu.be/3ifN0FhLB5E
**/
// Include library into the sketch
#include <AM2320.h>
// Create an instance of sensor
AM2320 sensor;
void setup() {
// enable serial communication
Serial.begin(9600);
Serial.print("Robojax AM2320 Demo ");
// 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.print(temp('C'));
Serial.print(" C, Humidity: ");
Serial.print(sensor.getHumidity());
Serial.println("%");
}
else { // error has occurred
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);
}
/*
* temp()
* returns temperature based on the T
* if T == F, will convert Celsius to Fahrenheit
* if anything else or empty inside single quotation, will return Celsius
* how to use:
* to get Fahrenheit, use temp('F')
* to get Celsius, use temp('C') or temp('')
* the temp('') is empty signle quoat
*
*/
float temp(char T)
{
if (sensor.measure()) {
if(T =='F')
{
// convert to FAHRENHEIT and return
// Robojax video tutorial
return sensor.getTemperature()* 1.8 + 32;
}else{
return sensor.getTemperature();// return CELSIUS
}
}// if sensor.measure
}