รหัส Arduino และวิดีโอสำหรับเซนเซอร์วัดอุณหภูมิและความชื้นดิจิทัล Aosong AM2320 พร้อมด้วย LCD1602 และโมดูล I2C
ในบทช่วยสอนนี้ เราจะสำรวจวิธีการใช้เซนเซอร์วัดอุณหภูมิและความชื้นดิจิทัล Aosong AM2320 ร่วมกับจอแสดงผล LCD1602 ที่ใช้โมดูล I2C เป้าหมายคือการแสดงค่าอุณหภูมิทั้งในหน่วยฟาเรนไฮต์และเซลเซียส รวมถึงความชื้นสัมพัทธ์ การตั้งค่านี้เหมาะสำหรับการตรวจสอบสภาพแวดล้อมในโครงการต่างๆ ทำให้เป็นส่วนเสริมที่หลากหลายสำหรับชุดเครื่องมือ Arduino ของคุณ

เราจะใช้เซนเซอร์ AM2320 เพื่อรวบรวมข้อมูลอุณหภูมิและความชื้น ซึ่งจะแสดงบน LCD1602 โมดูล I2C ช่วยลดความซับซ้อนของขั้นตอนการเดินสายไฟ โดยต้องใช้การเชื่อมต่อเพียงสี่จุด: VCC, GND, SDA และ SCL เพื่อความเข้าใจที่ชัดเจนเกี่ยวกับการเดินสายไฟและการเขียนโปรแกรม ฉันแนะนำให้ดูวิดีโอที่เกี่ยวข้อง (ในวิดีโอที่ 00:00)
อธิบายฮาร์ดแวร์
ส่วนประกอบสำคัญในโครงการนี้ ได้แก่ เซนเซอร์ AM2320 จอแสดงผล LCD1602 และโมดูล I2C AM2320 เป็นเซนเซอร์ดิจิทัลที่ให้ค่าการวัดอุณหภูมิและความชื้นที่แม่นยำผ่านอินเทอร์เฟซ I2C ทำงานที่แรงดันไฟฟ้า 3.3V ถึง 5.5V และมีช่วงการวัดอุณหภูมิ -40°C ถึง +80°C และความชื้น 0% ถึง 100%
จอแสดงผล LCD1602 ช่วยให้เห็นภาพค่าที่อ่านได้จากเซนเซอร์ เป็นจอแสดงผลอักขระขนาด 16x2 ที่สื่อสารผ่านโปรโตคอล I2C ซึ่งลดจำนวนการเชื่อมต่อที่ต้องใช้เหลือเพียงสี่จุด โมดูล I2C จะแปลงข้อมูลแบบขนานจาก LCD เป็นรูปแบบอนุกรม ทำให้เชื่อมต่อกับ Arduino ได้ง่ายขึ้น
รายละเอียดจาก Datasheet
| ผู้ผลิต | Aosong |
|---|---|
| หมายเลขชิ้นส่วน | AM2320 |
| แรงดันไฟฟ้าลอจิก/IO | 3.3 V - 5.5 V |
| แรงดันไฟฟ้าจ่าย | 3.3 V - 5.5 V |
| กระแสเอาต์พุต | ≤ 1.5 mA |
| ช่วงการวัด (อุณหภูมิ) | -40°C ถึง +80°C |
| ช่วงการวัด (ความชื้น) | 0% ถึง 100% |
| เวลาตอบสนอง | ≤ 2 วินาที |
| แพ็คเกจ | DIP-4 |
| หมายเหตุ / รุ่นต่างๆ | แนะนำให้ใช้ตัวต้านทานแบบ pull-up สำหรับการเชื่อมต่อ I2C |
- ตรวจสอบให้แน่ใจว่าแหล่งจ่ายไฟอยู่ในช่วงแรงดันไฟฟ้าที่กำหนด
- ใช้ตัวต้านทาน pull-up (แนะนำ 4.7kΩ) สำหรับสาย SDA และ SCL
- ตรวจสอบที่อยู่ I2C ที่ถูกต้อง (ค่าเริ่มต้นคือ 0x27 สำหรับ LCD ส่วนใหญ่)
- จัดการข้อผิดพลาดที่อาจเกิดขึ้นโดยการตรวจสอบรหัสข้อผิดพลาดของเซนเซอร์
- ล้างหน้าจอ LCD ก่อนแสดงค่าที่อ่านได้ใหม่เพื่อหลีกเลี่ยงการซ้อนทับกัน
คำแนะนำการเดินสายไฟ

ในการเดินสายไฟเซนเซอร์ AM2320 ให้เชื่อมต่อพินซ้าย (VCC) เข้ากับ 5V บน Arduino พินที่สอง (SDA) ไปที่พิน A4 บน Arduino Uno (หรือพิน 20 บน Mega) พินที่สาม (GND) ควรเชื่อมต่อกับกราวด์ และพินที่สี่ (SCL) เชื่อมต่อกับพิน A5 บน Arduino Uno (หรือพิน 21 บน Mega) นอกจากนี้ ให้เชื่อมต่อตัวต้านทาน 4.7kΩ ระหว่างพิน SDA และ 5V และตัวต้านทาน 4.7kΩ อีกตัวระหว่างพิน SCL และ 5V เพื่อให้แน่ใจว่าระดับสัญญาณถูกต้อง
สำหรับจอแสดงผล LCD1602 ที่มี I2C ให้เชื่อมต่อพิน VCC เข้ากับ 5V และพิน GND เข้ากับกราวด์ เชื่อมต่อพิน SDA จาก LCD เข้ากับพิน SDA เดียวกัน (A4) ที่ใช้สำหรับเซนเซอร์ ในทำนองเดียวกัน เชื่อมต่อพิน SCL จาก LCD เข้ากับพิน SCL เดียวกัน (A5) ที่ใช้สำหรับเซนเซอร์ การเดินสายไฟร่วมกันนี้ช่วยให้อุปกรณ์ทั้งสองสามารถสื่อสารผ่านบัส I2C เดียวกันได้
ตัวอย่างโค้ดและคำอธิบาย
โค้ดต่อไปนี้เริ่มต้นการทำงานของเซนเซอร์ AM2320 และจอแสดงผล LCD1602 โดยเริ่มต้นด้วยการรวมไลบรารีที่จำเป็นและสร้างอินสแตนซ์ของเซนเซอร์
#include
AM2320 sensor;
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
ที่นี่ ออบเจ็กต์ sensor ถูกสร้างขึ้นจากไลบรารี AM2320 และออบเจ็กต์ lcd ถูกเริ่มต้นด้วยที่อยู่ I2C ของจอแสดงผล
ในฟังก์ชัน setup() เราจะเริ่มต้นทั้งเซนเซอร์และ LCD ซึ่งรวมถึงการเปิดไฟแบ็คไลท์และการพิมพ์ข้อความเริ่มต้น
void setup() {
sensor.begin();
lcd.begin();
lcd.backlight();
lcd.print("Robojax AM2320 ");
lcd.setCursor (0,1);
lcd.print("LCD1602 I2C Demo");
delay(3000);
}
โค้ดนี้ตั้งค่าจอแสดงผลเพื่อแสดงข้อความต้อนรับเป็นเวลา 3 วินาที เพื่อให้ผู้ใช้เห็นว่าระบบกำลังเริ่มต้นทำงาน
ฟังก์ชัน loop() จะตรวจสอบการวัดค่าจากเซนเซอร์อย่างต่อเนื่องและอัปเดตจอแสดงผลตามนั้น หากมีการวัดค่าที่พร้อมใช้งาน ก็จะล้างหน้าจอและพิมพ์อุณหภูมิทั้งในหน่วยฟาเรนไฮต์และเซลเซียส พร้อมกับความชื้นสัมพัทธ์
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);
}
ข้อความนี้แสดงให้เห็นว่าโปรแกรมดึงข้อมูลและแสดงผลข้อมูลจากเซนเซอร์อย่างไร พร้อมทั้งจัดการข้อผิดพลาดที่อาจเกิดขึ้นโดยการตรวจสอบ errorCode
การสาธิต / สิ่งที่คาดหวัง
เมื่อการตั้งค่าเสร็จสมบูรณ์ คาดว่า LCD จะแสดงอุณหภูมิทั้งในหน่วยฟาเรนไฮต์และเซลเซียส รวมถึงเปอร์เซ็นต์ความชื้นสัมพัทธ์ หากเซนเซอร์ออฟไลน์หรือมีข้อผิดพลาด CRC จอแสดงผลจะแสดงข้อความแสดงข้อผิดพลาดที่เกี่ยวข้อง สิ่งสำคัญคือต้องแน่ใจว่าการเชื่อมต่อทั้งหมดแน่นหนาเพื่อหลีกเลี่ยงปัญหาใด ๆ (ในวิดีโอที่ 02:45)
ไทม์สแตมป์ของวิดีโอ
- 00:00 - แนะนำโปรเจกต์
- 01:30 - คำแนะนำการเดินสายไฟ
- 03:15 - คำอธิบายโค้ด
- 04:45 - การสาธิตการทำงาน
++
/**
* 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
}
สิ่งที่คุณอาจต้องการ
-
อเมซอน
-
อาลีเอ็กซ์เพรสPurchase AM2302, DHT11, or DHT22 sensor from AliExpresss.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
-
ภายนอกAM2320 Library (from GitHub)github.com
-
ภายนอกหน้าเอกสารประกอบ Arduino Wirearduino.cc
ไฟล์📁
แผ่นข้อมูล (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