ESP8266 Cảm biến Nhiệt LM35 và Màn hình OLED

Hướng dẫn này cho biết cách sử dụng ESP8266 để lấy nhiệt độ từ cảm biến LM35 và hiển thị lên màn hình OLED.

Phần cứng cần chuẩn bị

1×ESP8266 NodeMCU ESP-12E
1×Recommended: ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×LM35 Temperature Sensor
1×breadboard
1×dây jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP8266
1×(Khuyến nghị) Power Splitter for ESP8266 Type-C

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về OLED và Cảm biến Nhiệt độ LM35

Nếu bạn chưa quen với OLED và Cảm biến Nhiệt LM35 (bao gồm sơ đồ chân, chức năng và lập trình), các bài hướng dẫn dưới đây có thể giúp bạn:

Sơ đồ đấu dây

sơ đồ kết nối ESP8266 NodeMCU, cảm biến nhiệt lm35 và oLED

This image is created using Fritzing. Click to enlarge image

Xem thêm Sơ đồ chân ESP8266Cách cấp nguồn cho ESP8266.

Mã ESP8266 - Cảm biến nhiệt LM35 - OLED

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU 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/esp8266/esp8266-lm35-temperature-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define ADC_VREF_mV 5000.0 // in millivolt #define ADC_RESOLUTION 1024.0 #define PIN_LM35 A0 // pin connected to LM35 temperature sensor Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C 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 temperature_str.reserve(10); // to avoid fragmenting memory when using String } void loop() { // get the ADC value from the LM35 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 temperature_C = milliVolt / 10; 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); // display temperature on OLED } 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(); }

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

Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:

  • Xem hướng dẫn ESP8266 - Cài đặt phần mềm nếu đây là lần đầu tiên bạn sử dụng ESP8266.
  • Nối các linh kiện như trong sơ đồ.
  • Kết nối bo mạch ESP8266 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 phù hợp, chẳng hạn (ví dụ NodeMCU 1.0 (ESP-12E Module)), và cổng COM tương ứng của nó.
  • Nhấp vào biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm kiếm “SSD1306” và tìm thư viện SSD1306 từ Adafruit.
  • Nhấp vào nút Cài đặt để thêm thư viện.
thư viện oLED cho ESP8266 NodeMCU
  • Bạn sẽ được yêu cầu cài đặt một số phụ thuộc thư viện bổ sung.
  • Để cài đặt tất cả chúng, hãy nhấp vào nút Cài đặt tất cả.
thư viện cảm biến adafruit gfx cho ESP8266 NodeMCU
  • Sao chép mã ở trên và mở nó trong Arduino IDE.
  • Nhấn nút Tải lên trong Arduino IDE để gửi mã đến ESP8266.
  • Đặt cảm biến vào nước nóng và nước lạnh hoặc giữ nó trong tay bạn.
  • Kiểm tra kết quả trên màn hình OLED và Serial Monitor.

※ Lưu ý:

Đoạn mã được đề cập sẽ căn giữa văn bản theo cả chiều ngang lẫn chiều 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.