Arduino Hiển thị Nhiệt độ từ Cảm biến LM35 trên LCD
Trong hướng dẫn này, chúng ta sẽ học cách đọc nhiệt độ từ cảm biến LM35 và hiển thị nó trên LCD 16x2 I2C.

Phần cứng cần chuẩn bị
| 1 | × | Arduino Uno R3 | ||
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | ||
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | ||
| 1 | × | LCD I2C | ||
| 1 | × | LM35 Temperature Sensor | ||
| 1 | × | breadboard | ||
| 1 | × | dây jumper | ||
| 1 | × | (Khuyến nghị) Screw Terminal Block Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Breadboard Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Enclosure for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | ||
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | ||
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Về cảm biến nhiệt độ LM35 và LCD I2C
Nếu bạn chưa biết về Cảm biến Nhiệt LM35 và LCD I2C (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu chúng trong các bài hướng dẫn sau đây:
- Arduino - Màn hình LCD I2C hướng dẫn
- Arduino - Cảm biến nhiệt độ LM35 hướng dẫn
Sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image
Mã Arduino - Cảm biến nhiệt LM35 - LCD I2C
/*
* Mã Arduino này được phát triển bởi newbiely.vn
* Mã Arduino này được cung cấp để sử dụng công khai, không có ràng buộc.
* Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập:
* https://newbiely.vn/tutorials/arduino/arduino-display-temperature-from-lm35-sensor-on-lcd
*/
#include <LiquidCrystal_I2C.h> // LCD I2C library
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0 // pin connected to LM35 temperature sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
}
void loop() {
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in Celsius
float tempC = milliVolt / 10;
float tempF = tempC * 9 / 5 + 32; // convert Celsius to Fahrenheit
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print(tempC); // print the temperature in Celsius
lcd.print("°C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print(tempF); // print the temperature in Fahrenheit
lcd.print("°F");
// print the temperature to Serial Monitor
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
delay(500);
}
Hướng dẫn từng bước
- Mở Arduino IDE trên máy tính của bạn.
- Đi tới biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
- Tìm kiếm “LiquidCrystal I2C”, sau đó tìm thư viện LiquidCrystal_I2C của Frank de Brabander
- Nhấn nút Cài đặt để cài đặt thư viện LiquidCrystal_I2C.

- Sao chép mã ở trên và mở bằng Arduino IDE
- Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
- Đặt cảm biến vào nước nóng và nước lạnh, hoặc nắm cảm biến bằng tay của bạn
- Xem kết quả trên màn hình LCD và Trình theo dõi nối tiếp
※ Lưu ý:
Địa chỉ I2C của LCD có thể khác nhau tùy theo nhà sản xuất. Trong mã, chúng tôi đã sử dụng 0x27 được nhà sản xuất DIYables chỉ định.
Video Tutorial
Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.