Other Arduino Codes and Videos by Robojax
This page shows you how to use Aosong AM2320 Temperature and Humidity Sensor with LCD1602 and I2C Module and provides the Arduino code to download.
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 |
/**
* This is Arduino code for AM2320 Temperature and Humidity Sensor with LCD1602 and I2C Module
* which the temperature and RH (relative humidity) is displayed
* on LCD1602 with I2C module combined code 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 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/ym567hneDpE
**/
// Include library for AM2320
#include <AM2320.h>
// Create an instance of 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 blacklight 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 { // error has occurred
int errorCode = sensor.getErrorCode();
switch (errorCode) {
case 1: lcd.print("ERR: S. offline"); break;
case 2: lcd.print("ERR: CRC 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 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
}
this program will find the I2C address on the I2C device. Just upload the code into your Arduino and open serial monitor and wait. It will display the I2C address as 0x3C or similar.
/*********************************************************************
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 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("
I2C 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 Write.endTransmisstion to see if
// a device did acknowledge to 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
");
else
Serial.println("done
");
delay(5000); // wait 5 seconds for next scan
}
If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal