Arduino Cảm biến nhiệt độ LM35

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×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

Sơ đồ chân

Cảm biến nhiệt độ LM35 có ba chân:

  • Chân GND: cần được kết nối với GND (0V)
  • Chân VCC: cần được kết nối với VCC (5V)
  • Chân OUT: tín hiệu cho ra điện áp tỷ lệ thuận với nhiệt độ, nên được kết nối với một chân analog trên Arduino.
sơ đồ chân của cảm biến nhiệt lm35

Cách hoạt động

LM35 xuất ra điện áp tỷ lệ thuận với nhiệt độ theo độ C. Hệ số tỉ lệ đầu ra của LM35 là 10 mV/°C. Điều này có nghĩa nhiệt độ được tính bằng cách lấy điện áp (mV) ở chân đầu ra chia cho 10.

Sơ đồ đấu dây

sơ đồ nối dây cảm biến nhiệt độ lm35 cho Arduino

This image is created using Fritzing. Click to enlarge image

Cách Lập Trình Cho Cảm Biến Nhiệt Độ LM35

  • Lấy giá trị ADC từ cảm biến nhiệt độ bằng cách sử dụng hàm analogRead().
int adcVal = analogRead(PIN_LM35);
  • Chuyển đổi giá trị ADC thành điện áp ở đơn vị mili volt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
  • Chuyển đổi điện áp thành nhiệt độ ở độ C
float tempC = milliVolt / 10;
  • (Tùy chọn) Chuyển đổi từ độ C sang độ F
float tempF = tempC * 9 / 5 + 32;

Mã Arduino

/* * 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-lm35-temperature-sensor */ #define ADC_VREF_mV 5000.0 // in millivolt #define ADC_RESOLUTION 1024.0 #define PIN_LM35 A0 void setup() { Serial.begin(9600); } void loop() { // get the ADC value from the temperature sensor 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; // convert the Celsius to Fahrenheit float tempF = tempC * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("Temperature: "); Serial.print(tempC); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(tempF); // print the temperature in Fahrenheit Serial.println("°F"); delay(1000); }

Hướng dẫn từng bước

  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Nắm cảm biến bằng tay của bạn
  • Xem kết quả trên Serial Monitor
COM6
Send
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Cải thiện độ chính xác của nhiệt độ

Trong đoạn mã ở trên, chúng ta sử dụng điện áp tham chiếu ADC theo mặc định (5V~5000mV). Chúng ta có thể tăng độ phân giải nhiệt độ bằng cách thay đổi điện áp tham chiếu sang INTERNAL (1.1V~1100mV). Điện áp tham chiếu này có thể được thay đổi bằng hàm analogReference().

Bảng dưới đây cho thấy sự khác biệt giữa việc sử dụng điện áp tham chiếu 5000 mV và 1100 mV.

Vref(mV) 5000 mV (by default) 1100 mV (INTERNAL)
Reading Resolution 5000/1024 = 4.88 mV 1100/1024 = 1.07 mV
Temperature Resolution 0.488 °C 0.107 °C
Temperature Range 0 to 500 °C 0 to 110 °C

Mã Arduino

/* * 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-lm35-temperature-sensor */ #define ADC_VREF_mV 1100.0 // in millivolt #define ADC_RESOLUTION 1024.0 #define PIN_LM35 A0 void setup() { Serial.begin(9600); // switch to Internal 1.1V Reference analogReference(INTERNAL); } void loop() { // get the ADC value from the temperature sensor int adcVal = analogRead(PIN_LM35); // convert the ADC value to voltage in millivolt float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION); // ADC_VREF_mV = 1100 mV // convert the voltage to the temperature in Celsius float tempC = milliVolt / 10; // convert the Celsius to Fahrenheit float tempF = tempC * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("Temperature: "); Serial.print(tempC); // print the temperature in Celsius Serial.print("°C"); Serial.print(" ~ "); // separator between Celsius and Fahrenheit Serial.print(tempF); // print the temperature in Fahrenheit Serial.println("°F"); delay(1000); }

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.

Tài liệu tham khảo về hàm