Arduino Code and Video for an Aosong AM2320 Digital Temperature and Humidity Sensor
In this tutorial, we will explore how to use the Aosong AM2320 digital temperature and humidity sensor with Arduino. This sensor communicates over I2C and allows us to read temperature in Celsius or Fahrenheit, and humidity in percentage. By the end of this tutorial, you'll be able to build a simple project that displays these readings on the serial monitor.

The AM2320 sensor is compact and provides reliable data for both temperature and humidity. It has a resolution of 0.1°C for temperature and a humidity range of 0-99%. The setup process involves wiring the sensor to the Arduino and writing a few lines of code to read and display the sensor values. This video tutorial provides a step-by-step guide, including wiring and coding examples (in video at 00:00).
Hardware Explained
The main component of this project is the Aosong AM2320 sensor. It operates using I2C communication, which simplifies the connection process as it requires only two data lines (SDA and SCL) along with power and ground. The sensor measures temperature in the range of -40°C to +80°C with an accuracy of ±0.5°C and humidity from 0% to 99% with a similar accuracy. The sensor is designed to be very low power, making it suitable for battery-operated devices.
Additionally, the AM2320 module includes pull-up resistors that are necessary for I2C communication, which helps stabilize the signals on the SDA and SCL lines. This feature simplifies the wiring process, as you won't need to add external pull-up resistors.
Datasheet Details
| Manufacturer | Aosong |
|---|---|
| Part number | AM2320 |
| Logic/IO voltage | 3.1 to 5.5 V |
| Supply voltage | 3.1 to 5.5 V |
| Temperature range | -40 to +80 °C |
| Humidity range | 0 to 99 % |
| Resolution (Temperature) | 0.1 °C |
| Resolution (Humidity) | 0.1 % |
| Accuracy (Temperature) | ±0.5 °C |
| Accuracy (Humidity) | ±3 % |
| Package | Module |
- Ensure correct voltage supply (3.1 to 5.5 V).
- Use pull-up resistors (typically 4.7 kΩ) for SDA and SCL lines.
- Keep the temperature range within -40°C to +80°C to avoid damage.
- Humidity readings are accurate within 0% to 99% range.
- Monitor for error codes during readings (e.g., sensor offline).
Wiring Instructions

To wire the AM2320 sensor to the Arduino, follow these steps carefully. First, connect the power pins: the leftmost pin of the AM2320 connects to the Arduino's 5V (or VCC), while the ground pin connects to the Arduino's GND. The second pin from the left (SDA) connects to the Arduino's analog pin A4 for an Arduino Uno or A20 for an Arduino Mega. The third pin (SCL) goes to A5 for an Arduino Uno or A21 for an Arduino Mega.
Additionally, you will need to connect a 4.7 kΩ resistor from the SDA pin to the 5V line and another 4.7 kΩ resistor from the SCL pin to the 5V line. This ensures proper I2C communication. If you are using other Arduino models like the Leonardo, the SDA and SCL pins will also be A4 and A5, respectively.
Code Examples & Walkthrough
Let’s take a look at some key parts of the Arduino code used in this project. First, we initialize the sensor and set up serial communication:
#include
AM2320 sensor;
void setup() {
Serial.begin(9600);
sensor.begin();
}
In this snippet, we include the necessary library with #include <AM2320.h> and create an instance of the sensor. The setup() function initializes the serial communication at 9600 baud rate and starts the sensor.
Next, we have the main loop that reads the temperature and humidity:
if (sensor.measure()) {
Serial.print("Temperature: ");
Serial.print(temp('C'));
Serial.print(" C, Humidity: ");
Serial.print(sensor.getHumidity());
Serial.println("%");
}
This code checks if the sensor measurement was successful. If so, it prints the temperature in Celsius and the humidity percentage to the serial monitor. The temp('C') function is called to retrieve the temperature in Celsius. If you want Fahrenheit, you can call temp('F').
Demonstration / What to Expect
When you run the program, you should see the temperature and humidity readings updating every half second in the serial monitor. For example, the output might show "Temperature: 23.5 C, Humidity: 50%". If you apply heat to the sensor, you should observe the temperature increase and the humidity decrease, demonstrating its responsiveness (in video at 11:15).
Video Timestamps
- 00:00 - Introduction to the AM2320 sensor
- 01:30 - Wiring instructions
- 03:45 - Code walkthrough
- 05:15 - Demonstration of readings
++
/**
This is Arduino code for Aosong Digital Temperature and Humidity Sensor.
This code is presented as part of a 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
Modified for Robojax.com video by
Ahmad S. on March 22, 2018 at 22:45 in Ajax, Ontario, Canada
This code, with library and other codes, is available at
https://robojax.com
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 a boolean value
// - true indicates measurement is completed successfully
// - false indicates that either the sensor is not ready or CRC validation failed
// use getErrorCode() to check for the cause of the 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 parameter T
* if T == 'F', will convert Celsius to Fahrenheit
* if T is anything else or empty, will return Celsius
* how to use:
* to get Fahrenheit, use temp('F')
* to get Celsius, use temp('C') or temp('')
* the temp('') uses an empty single quote
*
*/
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
}
资源与参考
尚无可用资源。
文件📁
数据手册 (pdf)
-
Adafruit-am2320-temperature-humidity-i2c-sensor user's manual
adafruit-am2320-temperature-humidity-i2c-sensor.pdf1.27 MB
Fritzing 文件
-
AM2320 Humidity and Temperature Sensor
AM2320 Humidity and Temperature Sensor.fzpz0.01 MB