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
Resources & references
No resources yet.
Files📁
Datasheet (pdf)
-
Adafruit-am2320-temperature-humidity-i2c-sensor user's manual
application/pdf1.27 MB
Fritzing File
-
AM2320 Humidity and Temperature Sensor
application/zip0.01 MB