Raspberry Pi Cảm Biến Nhiệt Độ LM35

Hướng dẫn này sẽ chỉ bạn cách sử dụng Raspberry Pi để đọc nhiệt độ từ cảm biến LM35. Cụ thể, chúng ta sẽ học:

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Cảm Biến Nhiệt Độ LM35
1×Module ADC ADS1115
1×breadboard
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

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ảm biến nhiệt độ LM35 có ba chân:

  • Chân GND: Chân này cần được kết nối với GND (0V)
  • Chân VCC: Chân này cần được kết nối với VCC (5V)
  • Chân OUT: Chân tín hiệu này cung cấp điện áp đầu ra tỷ lệ tuyến tính với nhiệt độ, và cần được kết nối với chân analog trên Raspberry Pi.
lm35 cảm biến nhiệt độ sơ đồ chân

Nguyên Lý Hoạt Động

LM35 tạo ra điện áp đầu ra tỷ lệ tuyến tính với nhiệt độ Celsius. Hệ số tỷ lệ của LM35 là 10 mV/°C, có nghĩa là nhiệt độ có thể được xác định bằng cách chia điện áp (tính bằng mV) tại chân đầu ra cho 10.

Do Raspberry Pi không có chân đầu vào analog, chúng ta cần sử dụng module ADC như Module ADC ADS1115

Sơ Đồ Đấu Nối

Raspberry Pi lm35 cảm biến nhiệt độ sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi 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/raspberry-pi/raspberry-pi-lm35-temperature-sensor import Adafruit_ADS1x15 import time # Create an ADC instance (ADS1015 or ADS1115) adc = Adafruit_ADS1x15.ADS1115() # Analog input channel connected to the LM35 analog_channel = 0 def read_lm35_temperature(): # Read the raw ADC value raw_value = adc.read_adc(analog_channel, gain=1) # Convert the raw ADC value to millivolts millivolts = (raw_value * 4.096) / 32767.0 * 1000 # Convert millivolts to Celsius using the LM35 formula temperature_celsius = millivolts / 10.0 # Convert Celsius to Fahrenheit temperature_fahrenheit = (temperature_celsius * 9/5) + 32 return temperature_celsius, temperature_fahrenheit try: while True: celsius, fahrenheit = read_lm35_temperature() print(f'Temperature: {celsius:.2f}°C ~ {fahrenheit:.2f}°F') time.sleep(1) except KeyboardInterrupt: pass

Các Bước Thực Hiện

  • Đảm bảo bạn đã cài đặt Raspbian hoặc bất kỳ hệ điều hành tương thích nào khác trên Raspberry Pi.
  • Đảm bảo Raspberry Pi của bạn được kết nối với cùng mạng cục bộ với PC của bạn.
  • Đảm bảo Raspberry Pi của bạn được kết nối internet nếu bạn cần cài đặt một số thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH bằng SSH client có sẵn trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Cài đặt thư viện Adafruit_ADS1x15 bằng cách chạy các lệnh sau trên terminal Raspberry Pi:
sudo pip install Adafruit-ADS1x15
  • Tạo file script Python LM35.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi 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/raspberry-pi/raspberry-pi-lm35-temperature-sensor import Adafruit_ADS1x15 import time # Create an ADC instance (ADS1015 or ADS1115) adc = Adafruit_ADS1x15.ADS1115() # Analog input channel connected to the LM35 analog_channel = 0 def read_lm35_temperature(): # Read the raw ADC value raw_value = adc.read_adc(analog_channel, gain=1) # Convert the raw ADC value to millivolts millivolts = (raw_value * 4.096) / 32767.0 * 1000 # Convert millivolts to Celsius using the LM35 formula temperature_celsius = millivolts / 10.0 # Convert Celsius to Fahrenheit temperature_fahrenheit = (temperature_celsius * 9/5) + 32 return temperature_celsius, temperature_fahrenheit try: while True: celsius, fahrenheit = read_lm35_temperature() print(f'Temperature: {celsius:.2f}°C ~ {fahrenheit:.2f}°F') time.sleep(1) except KeyboardInterrupt: pass
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 LM35.py
  • Cầm cảm biến trong tay bạn.
  • Kiểm tra kết quả đầu ra trong Terminal.
PuTTY - Raspberry Pi
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

Script sẽ chạy liên tục trong vòng lặp vô hạn cho đến khi bạn nhấn Ctrl + C trong terminal.

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.