Arduino Cảm biến nhiệt độ Ma trận LED

Trong bài hướng dẫn này, chúng ta sẽ học cách lập trình Arduino để đọc nhiệt độ từ cảm biến DS18B20 1-Wire và hiển thị nó trên ma trận LED.

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×FC-16 LED Matrix 32x8
1×FC-16 LED Matrix 8x8
1×DS18B20 Temperature Sensor (WITH Adapter)
1×breadboard
1×dây jumper
1×(Tùy chọn) DC Power Jack
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ề ma trận LED và cảm biến nhiệt độ DS18B20

Nếu bạn chưa biết về ma trận LED và Cảm biến Nhiệt DS18B20 (bố trí chân, cách nó hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn dưới đây:

Sơ đồ đấu dây

sơ đồ nối dây cho Arduino ds18b20 cảm biến nhiệt độ và ma trận LED

This image is created using Fritzing. Click to enlarge image

Chúng tôi đề xuất mua cảm biến DS18B20 đi kèm với một adapter dây để kết nối dễ dàng. Adapter có một điện trở tích hợp, loại bỏ nhu cầu dùng một điện trở riêng trong dây dẫn.

Mã Arduino - Nhiệt độ từ cảm biến DS18B20 và hiển thị nó trên ma trận LED

/* * 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-temperature-sensor-led-matrix */ #include <OneWire.h> #include <DallasTemperature.h> #include <DIYables_LED_Matrix.h> #define SENSOR_PIN 2 // DS18B20 data pin OneWire oneWire(SENSOR_PIN); DallasTemperature tempSensor(&oneWire); float tempCelsius; float tempFahrenheit; #define CS_PIN 9 // Chip Select pin for MAX7219 #define NUM_MATRICES 4 // Number of cascaded MAX7219 modules #define SPACING 2 // Spacing between characters DIYables_Max7219 display(CS_PIN, NUM_MATRICES); void setup() { Serial.begin(9600); delay(500); // Initialize temperature sensor tempSensor.begin(); // Initialize the LED matrix display display.setBrightness(1); // Brightness level: 0 to 15 display.clear(); display.show(); } void loop() { // Request and read the temperature tempSensor.requestTemperatures(); tempCelsius = tempSensor.getTempCByIndex(0); tempFahrenheit = (tempCelsius * 9.0 / 5.0) + 32.0; // Print temperature to Serial Monitor Serial.print("Temperature: "); Serial.print(tempCelsius); Serial.print("°C ~ "); Serial.print(tempFahrenheit); Serial.println("°F"); // Clear the display, then print temperature (Celsius) display.clear(); // Convert to C-style string before passing to the print() function String tempStr = String(tempCelsius, 1) + "°C"; // 1 decimal place display.print(tempStr.c_str(), SPACING, 0); display.show(); delay(2000); // Wait 2 seconds before taking the next reading }

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

  • Mở Arduino IDE trên máy tính của bạn.
  • Điều hướng đến biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm kiếm “DIYables-LED-Matrix”, sau đó tìm thư viện LED Matrix của DIYables
  • Nhấp nút Cài đặt để cài đặt thư viện.
thư viện ma trận LED cho Arduino
  • Tìm kiếm “DallasTemperature”, sau đó tìm thư viện DallasTemperature của Miles Burton.
  • Nhấn nút Cài đặt để cài đặt thư viện DallasTemperature.
thư viện dallas temperature cho Arduino
  • Bạn sẽ được yêu cầu cài đặt phụ thuộc của thư viện
  • Nhấn vào nút Cài đặt tất cả để cài đặt thư viện OneWire.
thư viện onewire cho Arduino
  • 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
  • Đặ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 ma trận LED

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.