Other Arduino Codes and Videos by Robojax

Arduino code and Video for Aosong AM2320 Digital Temperature and Humidity Sensor Robojax.com

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

As a reference the table below shows where TWI pins are located on various Arduino boards. See Arduino Wire

BoardI2C / TWI pins
Uno, EthernetA4 (SDA), A5 (SCL)
Mega256020 (SDA), 21 (SCL)
Leonardo2 (SDA), 3 (SCL)
Due20 (SDA), 21 (SCL), SDA1, SCL1
  1. Arduino Wire
  2. AM2320 Library (from Gethub)
  3. AM2320 Library (from Robojax.com)
  4. 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  
}