Arduino UNO R4 Cảm Biến Nhiệt Độ OLED

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

Arduino UNO R4 ds18b20 cảm biến nhiệt độ oLED

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×Cảm Biến Nhiệt Độ DS18B20 (CÓ Adapter)
1×Cảm Biến Nhiệt Độ DS18B20 (KHÔNG Adapter)
1×breadboard (Bo mạch thí nghiệm)
1×Dây Nối Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Giới Thiệu Về OLED và Cảm Biến Nhiệt Độ DS18B20

Tìm hiểu về OLED và Cảm biến Nhiệt độ DS18B20 (bao gồm sơ đồ chân, chức năng và lập trình) trong các hướng dẫn sau:

Sơ Đồ Kết Nối

Arduino UNO R4 ds18b20 cảm biến nhiệt độ oLED sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Chúng tôi khuyên bạn nên mua cảm biến DS18B20 có sẵn adapter kết nối để thiết lập đơn giản. Adapter đã bao gồm điện trở, vì vậy bạn không cần thêm điện trở nào khác cho việc kết nối.

Code Arduino UNO R4 - Đọc Nhiệt Độ từ Cảm Biến DS18B20 và Hiển Thị trên OLED

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-temperature-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <OneWire.h> #include <DallasTemperature.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define SENSOR_PIN 8 // The Arduino UNO R4 pin connected to DS18B20 sensor Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature DS18B20(&oneWire); // pass oneWire to DallasTemperature library String temperature_str; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display DS18B20.begin(); // initialize the sensor temperature_str.reserve(10); // to avoid fragmenting memory when using String } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius temperature_str = String(temperature_C, 2); // two decimal places temperature_str += char(247) + String("C"); Serial.println(temperature_str); // print the temperature in Celsius to Serial Monitor oled_display_center(temperature_str); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

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

Thực hiện theo các hướng dẫn sau từng bước:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino UNO R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối OLED và cảm biến nhiệt độ với bo mạch Arduino UNO R4 theo sơ đồ đã cung cấp.
  • Kết nối bo mạch Arduino UNO R4 với máy tính của bạn bằng cáp USB.
  • Khởi chạy Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino UNO R4 phù hợp (ví dụ: Arduino UNO R4 WiFi) và cổng COM.
  • Click vào biểu tượng Libraries ở phía bên trái của Arduino IDE.
  • Gõ "SSD1306" vào ô tìm kiếm, sau đó tìm thư viện SSD1306 của Adafruit.
  • Nhấn nút "Install" để thêm thư viện.
Arduino UNO R4 oLED thư viện
  • Bạn sẽ cần cài đặt thêm một số thư viện bổ sung
  • Click nút Install All để cài đặt tất cả thư viện cần thiết.
Arduino UNO R4 adafruit gfx sensor thư viện
  • Gõ "DallasTemperature" vào ô tìm kiếm, và tìm thư viện DallasTemperature của Miles Burton.
  • Nhấn nút Install để cài đặt thư viện DallasTemperature.
Arduino UNO R4 dallas temperature thư viện
  • Bạn cần cài đặt thư viện phụ thuộc.
  • Click nút Install All để cài đặt thư viện OneWire.
Arduino UNO R4 onewire thư viện
  • Copy đoạn code ở trên và mở nó trong Arduino IDE
  • Nhấn nút Upload trong Arduino IDE để chuyển code lên Arduino UNO R4
  • Đặt cảm biến vào nước nóng hoặc lạnh, hoặc cầm nó trong tay
  • Kiểm tra kết quả hiển thị trên màn hình OLED

※ Lưu ý:

Code tự động căn giữa văn bản theo chiều ngang và dọc trên màn hình OLED.

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.

Bình Luận