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

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

Thông tin về cảm biến nhiệt TMP36

Bố trí chân

Cảm biến nhiệt độ TMP36 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 +Vs: là nguồn cấp cho cảm biến, có thể dao động từ 2.7V đến 5.5V
  • Chân Vout: là chân tín hiệu cho ra điện áp đầu ra tỷ lệ thuận với nhiệt độ, nên kết nối với một chân analog trên Arduino
bản đồ chân cảm biến nhiệt độ tmp36

Cách hoạt động

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

Sơ đồ đấu dây

sơ đồ nối dây cảm biến nhiệt độ tmp36 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 độ TMP36

  • 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_TMP36);
  • Chuyển đổi giá trị ADC sang điện áp.
// Replace 5.0 with 3.3, if you are using a 3.3V Arduino float voltage = adcVal * (5.0 / 1024.0);
  • Chuyển đổi điện áp sang nhiệt độ ở độ C
float tempC = (voltage - 0.5) * 100;
  • (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-tmp36-temperature-sensor */ #define ADC_VREF 5.0 // in volt #define ADC_RESOLUTION 1024.0 #define PIN_TMP36 A0 void setup() { Serial.begin(9600); } void loop() { // get the ADC value from the TMP36 temperature sensor int adcVal = analogRead(PIN_TMP36); // convert the ADC value to voltage float voltage = adcVal * (ADC_VREF / ADC_RESOLUTION); // convert the voltage to the temperature in Celsius float tempC = (voltage - 0.5) * 100; // convert the Celsius to Fahrenheit float tempF = tempC * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("TMP36 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 vào nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Hãy nắm cảm biến bằng tay của bạn
  • Xem kết quả trên Serial Monitor.
COM6
Send
TMP36 Temperature: 26.31°C ~ 79.36°F TMP36 Temperature: 26.44°C ~ 79.59°F TMP36 Temperature: 26.50°C ~ 79.70°F TMP36 Temperature: 26.56°C ~ 79.81°F TMP36 Temperature: 27.06°C ~ 80.71°F TMP36 Temperature: 27.75°C ~ 81.95°F TMP36 Temperature: 28.37°C ~ 83.07°F TMP36 Temperature: 29.00°C ~ 84.20°F TMP36 Temperature: 29.56°C ~ 85.21°F TMP36 Temperature: 30.00°C ~ 86.00°F TMP36 Temperature: 30.31°C ~ 86.56°F TMP36 Temperature: 30.62°C ~ 87.12°F TMP36 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 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 thành 3.3V (3300mV). Điện áp tham chiếu này có thể được thay đổi bằng cách kết nối hai chân 3.3V và AREF với nhau như sơ đồ dưới đây.

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

This image is created using Fritzing. Click to enlarge image

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) 3300 mV
Reading Resolution 5000/1024 = 4.88 mV 3300/1024 = 3.22 mV
Temperature Resolution 0.49°C 0.32°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-tmp36-temperature-sensor */ #define ADC_VREF 3.3 // in volt #define ADC_RESOLUTION 1024.0 #define PIN_TMP36 A0 void setup() { Serial.begin(9600); analogReference(EXTERNAL); // set the voltage reference from VREF pin } void loop() { // get the ADC value from the TMP36 temperature sensor int adcVal = analogRead(PIN_TMP36); // convert the ADC value to voltage float voltage = adcVal * (ADC_VREF / ADC_RESOLUTION); // convert the voltage to the temperature in Celsius float tempC = (voltage - 0.5) * 100; // convert the Celsius to Fahrenheit float tempF = tempC * 9 / 5 + 32; // print the temperature in the Serial Monitor: Serial.print("TMP36 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.