搜索代码

Arduino Code and Video for an Aosong AM2320 Digital Temperature and Humidity Sensor with LCD1602 and I2C Module

Arduino Code and Video for an Aosong AM2320 Digital Temperature and Humidity Sensor with LCD1602 and I2C Module

In this tutorial, we will explore how to use the Aosong AM2320 digital temperature and humidity sensor alongside an LCD1602 display that employs an I2C module. The goal is to display temperature readings in both Fahrenheit and Celsius, as well as relative humidity. This setup is ideal for monitoring environmental conditions in various projects, making it a versatile addition to your Arduino toolkit.

We will be using the AM2320 sensor to gather temperature and humidity data, which will then be displayed on the LCD1602. The I2C module simplifies the wiring process, requiring only four connections: VCC, GND, SDA, and SCL. For a clear understanding of the wiring and programming, I recommend watching the associated video (in video at 00:00).

Hardware Explained

The key components in this project include the AM2320 sensor, the LCD1602 display, and the I2C module. The AM2320 is a digital sensor that provides accurate readings of temperature and humidity through an I2C interface. It operates on a voltage of 3.3V to 5.5V and has a measurement range of -40°C to +80°C for temperature and 0% to 100% for humidity.

The LCD1602 display allows for the visualization of the sensor readings. It is a 16x2 character display that communicates via the I2C protocol, which reduces the number of required connections to just four. The I2C module converts the parallel data from the LCD to a serial format, making it easier to connect to the Arduino.

Datasheet Details

ManufacturerAosong
Part numberAM2320
Logic/IO voltage3.3 V - 5.5 V
Supply voltage3.3 V - 5.5 V
Output current≤ 1.5 mA
Measurement range (Temperature)-40°C to +80°C
Measurement range (Humidity)0% to 100%
Response time≤ 2s
PackageDIP-4
Notes / variantsResistor pull-ups are recommended for I2C connections.

  • Ensure proper power supply within the specified voltage range.
  • Use pull-up resistors (4.7kΩ recommended) for SDA and SCL lines.
  • Check for correct I2C address (default is 0x27 for most LCDs).
  • Handle potential errors by checking the sensor's error code.
  • Clear the LCD before displaying new readings to avoid overlap.

Wiring Instructions

Arduino wiring for AM2320 sensor with LCD
Arduino wiring for AM2320 sensor with LCD

To wire the AM2320 sensor, connect the left pin (VCC) to 5V on the Arduino. The second pin (SDA) goes to the A4 pin on an Arduino Uno (or pin 20 on a Mega). The third pin (GND) should be connected to the ground, and the fourth pin (SCL) connects to the A5 pin on an Arduino Uno (or pin 21 on a Mega). Additionally, connect a 4.7kΩ resistor between the SDA pin and 5V, and another 4.7kΩ resistor between the SCL pin and 5V to ensure proper signal levels.

For the LCD1602 display with I2C, connect the VCC pin to 5V and the GND pin to ground. Connect the SDA pin from the LCD to the same SDA pin (A4) used for the sensor. Similarly, connect the SCL pin from the LCD to the same SCL pin (A5) used for the sensor. This shared wiring allows both devices to communicate over the same I2C bus.

Code Examples & Walkthrough

The following code initializes the AM2320 sensor and the LCD1602 display. It begins by including the necessary libraries and creating an instance of the sensor.

#include 
AM2320 sensor;
#include 
LiquidCrystal_I2C lcd(0x27, 16, 2);

Here, the sensor object is created from the AM2320 library, and the lcd object is initialized with the I2C address of the display.

In the setup() function, we initialize both the sensor and the LCD. This includes turning on the backlight and printing an initial message.

void setup() {
  sensor.begin();
  lcd.begin();
  lcd.backlight();
  lcd.print("Robojax AM2320 ");
  lcd.setCursor (0,1);
  lcd.print("LCD1602 I2C Demo");
  delay(3000);
}

This code sets up the display to show a welcome message for 3 seconds, allowing time for the user to see that the system is initializing.

The loop() function continuously checks for sensor measurements and updates the display accordingly. If a measurement is available, it clears the screen and prints the temperature in both Fahrenheit and Celsius, along with the relative humidity.

void loop() {
  if (sensor.measure()) {
    lcd.clear();
    lcd.print("T:");
    lcd.print(temp('F'));
    lcd.print("F/");
    lcd.print(temp('C'));
    lcd.print("C");  
    lcd.setCursor (0,1);
    lcd.print("R.H. :");
    lcd.print(sensor.getHumidity());
    lcd.print("%"); 
  }
  else {
    int errorCode = sensor.getErrorCode();
    switch (errorCode) {
      case 1: lcd.print("ERR: Sensor offline"); break;
      case 2: lcd.print("ERR: CRC failed."); break;
    }    
  }
  delay(500);
}

This excerpt demonstrates how the program retrieves and displays the sensor data while handling any potential errors by checking the errorCode.

Demonstration / What to Expect

When the setup is complete, expect the LCD to display the temperature in both Fahrenheit and Celsius, as well as the relative humidity percentage. If the sensor is offline or there’s a CRC error, the display will show corresponding error messages. It’s important to ensure all connections are secure to avoid any issues (in video at 02:45).

Video Timestamps

  • 00:00 - Introduction to the project
  • 01:30 - Wiring instructions
  • 03:15 - Code explanation
  • 04:45 - Demonstration of functionality

图像

AM2320-sensor-1
AM2320-sensor-1
AM2320-sensor-2
AM2320-sensor-2
AM2320-sensor-3
AM2320-sensor-3
AM2320-sensor-4
AM2320-sensor-4
Arduino wiring for AM2320 sensor with LCD
Arduino wiring for AM2320 sensor with LCD
85-This is the Arduino code for an Aosong AM2320 temperature and humidity sensor with an LCD1602 and I2C module.
语言: C++
++
/**
 * This is Arduino code for AM2320 Temperature and Humidity Sensor with LCD1602 and I2C Module
 * which displays the temperature and RH (relative humidity)
 * on an LCD1602 with an I2C module.  The code was combined by Robojax.
 * 
    Original Code For AM2320 :https://github.com/hibikiledo/AM2320
    Copyright 2016 Ratthanan Nalintasnai
    
    Modified for Robojax.com video by
    Ahmad S. on March 22, 2018 at 23:55 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/ym567hneDpE
**/

// Include library for AM2320
#include <AM2320.h>
// Create an instance of the sensor
AM2320 sensor;


#include <Wire.h> 
// Include the LiquidCrystal library for display
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);


void setup() {
  // Call sensor.begin() to initialize the library
  sensor.begin();

  // Robojax code for LCD with I2C
  // Initialize the LCD, 
  lcd.begin();
   // Turn on the backlight and print a message.
  lcd.backlight();
  lcd.print("Robojax AM2320 ");
  lcd.setCursor (0,1);
  lcd.print("LCD1602 I2C Demo");
  delay(3000);
}

void loop() {
  if (sensor.measure()) {
    lcd.clear();
    lcd.print("T:");
    lcd.print(temp('F'));
    lcd.print("F/");
    lcd.print(temp('C'));
    lcd.print("C");  
    lcd.setCursor (0,1); // go to start of 2nd line
    lcd.print("R.H. :");
    lcd.print(sensor.getHumidity());
    lcd.print("%"); 
  

  }
  else {  // An error has occurred
    int errorCode = sensor.getErrorCode();
    switch (errorCode) {
      case 1: lcd.print("ERR: Sensor offline"); break;
      case 2: lcd.print("ERR: CRC failed."); break;
    }    
  }

  delay(500);
}

/*
 * temp()
 * Returns the temperature based on the input character T.
 * If T == 'F', converts Celsius to Fahrenheit.
 * Otherwise, returns Celsius.
 * How to use:
 *  To get Fahrenheit, use temp('F').
 *  To get Celsius, use temp('C') or temp('').
 *  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  
}
86-This is the Arduino code for the Aosong AM2320 temperature and humidity sensor with an LCD1602 and I2C module.
语言: C++
/*********************************************************************
Original source: http://playground.arduino.cc/Main/I2cScanner

This program will find the I2C address on the I2C device. Just upload the code into your Arduino
and open the serial monitor and wait. It will display the I2C address as 0x3C or similar.

 * Please view other RoboJax codes and videos at http://robojax.com/learn/arduino
 * If you are sharing this code, you must keep this copyright note.
 * 
*********************************************************************/


#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The I2C scanner uses the return value of
    // the Wire.endTransmission to see if
    // a device did acknowledge the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

|||您可能需要的东西

资源与参考

文件📁

数据手册 (pdf)

Fritzing 文件