Code ya Arduino mpe Video ya senseur ya température mpe ya malili ya Aosong AM2320 na LCD1602 mpe module ya I2C
Na boyekoli oyo, tokotalela ndenge ya kosalela esaleli ya Aosong AM2320 ya température mpe ya mokili mwa mopɛpɛ (digital temperature and humidity sensor) elongo na display ya LCD1602 oyo esalelaka module ya I2C. Ntina ezali ete tomonisa température na Fahrenheit mpe na Celsius, mpe mpe mokili mwa mopɛpɛ (relative humidity). Esaleli oyo ezali malamu mpo na kokamba makambo ya environnement na ba projets ebele, mpe ezali esaleli ya ntina mingi mpo na bozui na yo ya Arduino.

Tokosalela esaleli ya AM2320 mpo na kozwa ba données ya température mpe ya mokili mwa mopɛpɛ, oyo ekotiama na LCD1602. Module ya I2C ekosalisa ete ba connexions ezala pete, mpo esengeli kaka ba connexions minei: VCC, GND, SDA, mpe SCL. Mpo na kososola malamu ndenge ya kosala ba connexions mpe ya programme, nalingi ete oyeba ete vidéo oyo ezali na ntina (na vidéo na 00:00).
Ba Esaleli oyo Ezali na Ntina
Ba composants ya ntina na projet oyo ezali esaleli ya AM2320, display ya LCD1602, mpe module ya I2C. AM2320 ezali esaleli ya numérique oyo epesaka ba mesures ya température mpe ya mokili mwa mopɛpɛ na nzela ya interface ya I2C. Esalaka na voltage ya 3.3V kino 5.5V mpe ezali na mesure ya -40°C kino +80°C mpo na température mpe 0% kino 100% mpo na mokili mwa mopɛpɛ.
Display ya LCD1602 esalaka ete tomona ba mesures ya esaleli. Ezali display ya ba caractères 16x2 oyo esololaka na protocole ya I2C, oyo ekitisaka motango ya ba connexions na minei kaka. Module ya I2C ebongolaka ba données parallèles ya LCD na format ya série, mpo ete ezala pete na connexion na Arduino.
Ba Détails ya Datasheet
| Mosali | Aosong |
|---|---|
| Numéro ya partie | AM2320 |
| Voltage ya logique/IO | 3.3 V - 5.5 V |
| Voltage ya alimentation | 3.3 V - 5.5 V |
| Courant ya sortie | ≤ 1.5 mA |
| Étendue ya mesure (Température) | -40°C kino +80°C |
| Étendue ya mesure (Mokili mwa mopɛpɛ) | 0% kino 100% |
| Temps ya réponse | ≤ 2s |
| Paquet | DIP-4 |
| Ba notes / ba variantes | Ba résistances ya pull-up ezali ntina mpo na ba connexions I2C. |
- Kokamba ete alimentation ezala malamu na kati ya étendue ya voltage oyo esengeli.
- Salela ba résistances ya pull-up (4.7kΩ ezali malamu) mpo na ba lignes SDA mpe SCL.
- Vérifier ete adresse ya I2C ezali malamu (adresse ya défaut ezali 0x27 mpo na ba LCD mingi).
- Kamba ba erreurs na kotala code ya erreur ya esaleli.
- Bongola LCD liboso ya kolakisa ba mesures ya sika mpo na kokima kokutana.
Ba Instructions ya Connexion

Mpo na kokɔta na esaleli ya AM2320, kanga pin ya liboso (VCC) na 5V na Arduino. Pin ya mibale (SDA) ekende na pin A4 na Arduino Uno (to pin 20 na Mega). Pin ya misato (GND) esengeli kokangama na terre, mpe pin ya minei (SCL) ekangami na pin A5 na Arduino Uno (to pin 21 na Mega). Libanda na yango, kanga résistance ya 4.7kΩ kati na pin SDA mpe 5V, mpe résistance mosusu ya 4.7kΩ kati na pin SCL mpe 5V mpo na kokamba ete ba signaux ezala malamu.
Mpo na display ya LCD1602 na I2C, kanga pin ya VCC na 5V mpe pin ya GND na terre. Kanga pin ya SDA ya LCD na pin SDA moko (A4) oyo esaleli esalelaka. Ndenge moko mpe, kanga pin ya SCL ya LCD na pin SCL moko (A5) oyo esaleli esalelaka. Connexion oyo ya kokabola esalaka ete ba esaleli nyonso mibale basolola na bus moko ya I2C.
Ba Exemples ya Code mpe Explication
Code oyo elandaka ebandisaka esaleli ya AM2320 mpe display ya LCD1602. Ebandaka na kosangisa ba bibliothèques ya ntina mpe na kosala instance ya esaleli.
#include
AM2320 senseur;
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
Awa, senseur ezalí esaleli esálémí na bibliotɛki ya AM2320, mpe lcd ezalí esaleli oyo ebandisami na adresi ya I2C ya display.
Na setup() fonction, tobandisaka senseur mpe LCD. Boye ezali kosangisa kopelisa backlight mpe kokoma message ya liboso.
void setup() {
sensor.begin();
lcd.begin();
lcd.backlight();
lcd.print("Robojax AM2320 ");
lcd.setCursor (0,1);
lcd.print("LCD1602 I2C Demo");
delay(3000);
}
Kode oyo ebongisaka display mpo na kolakisa message ya boyambi mpo na second 3, mpo na kopesa ntango na moto mpo na komona ete système ezali kobanda.
Fonction loop() ezalaka kolanda ntango nyonso mpo na ba mesures ya senseur mpe kobongisa display kolanda yango. Soki mesure ezali, ekosalisa kopetola écran mpe kokoma température na Fahrenheit mpe Celsius, elongo na humidité relative.
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);
}
Ekólo oyo elakisaka ndenge programme ezali kozwa mpe kolakisa ba données ya senseur mpe kolongola ba erreurs nyonso na kotala errorCode.
Démonstration / Nini Okokanela
Soki mise en place esili, kanela ete LCD ekoki kolakisa température na Fahrenheit mpe Celsius, mpe mpe pourcentage ya humidité relative. Soki senseur ezali na ligne to ezali na erreur ya CRC, display ekoki kolakisa ba messages ya erreur oyo ekokani. Ezali malamu mpo na komeka ete ba connexions nyonso ezali ya libanga mpo na kokima ba problèmes (na vidéo na 02:45).
Ba Timestamps ya Vidéo
- 00:00 - Introduction na projet
- 01:30 - Ba instructions ya câblage
- 03:15 - Explication ya code
- 04:45 - Démonstration ya fonctionnement
++
/**
* 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
}
/*********************************************************************
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
}
Nko o na nki.
-
Amazon
-
AliExpressPurchase AM2302, DHT11, or DHT22 sensor from AliExpresss.click.aliexpress.com
Mokili na bisika
-
L'extérieurAM2320 Library (from GitHub)github.com
-
L'extérieurLokasa ya mikanda ya nsinga ya Arduinoarduino.cc
Fichiers📁
Fiche technique (pdf)
-
Adafruit-am2320-temperature-humidity-i2c-sensor user's manual
adafruit-am2320-temperature-humidity-i2c-sensor.pdf1.27 MB
Fritzing File
-
AM2320 Humidity and Temperature Sensor
AM2320 Humidity and Temperature Sensor.fzpz0.01 MB