Cytron Technologies

Giờ làm việc: 8:00 - 17:00

Thứ 2 - Thứ 6 (trừ ngày lễ)

Hotline 0362917357 

Đọc Nhiệt độ và Độ ẩm sử dụng DHT22 và Maker Nano

Trong dự án này, chúng ta sẽ xây dựng một mạch điện đơn giản để đọc giá trị nhiệt độ và độ ẩm từ cảm biến DHT22, sau đó hiển thị thông tin này trên màn hình OLED 0.96 Inch I2C sử dụng Maker Nano (bo mạch tương thích với Arduino Nano)

Phần cứng

Trong dự án này, bạn sẽ cần một số linh kiện như sau:

Sơ đồ kết nối

  • Một số màn hình OLED I2C có chân VCC và GND ngược nhau, bạn chú ý khi nối dây tránh làm hỏng bo mạch.
  • Đối với cảm biến DHT22 Cytron phân phối, chân số 1 đếm từ trái qua là chân VCC (3.3V), chân tiếp theo là DATA (nối tới chân D2 trên bo mạch Maker Nano), chân số 3 để trống và chân số 4 nối tới GND

Chương trình mẫu

Đây là chương trình mẫu của dự án.

Bạn lưu ý chỉnh sửa lại địa chỉ I2C của màn hình OLED tại dòng display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

/***************************************************************************
   Temperature and humidity display
   Use a DHT22 sensor to acquire data about room relative humidity and
   temperature and display the values on a 0.96" OLED screen. It may also
   log data with MATLAB.
   
    Copyright (C) 2018  Danilo Ciliberti [email protected]
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>
***************************************************************************/

// Libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(2, DHT22);

// Variables
float hum;                    // Stores humidity value
float temp;                   // Stores temperature value
float humidex;                // Stores humidex index value
unsigned int interval = 10;   // Time between logging in seconds

void setup() {
  Serial.begin(9600);

  // sensor init
  dht.begin();

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  // Show image buffer on the display hardware
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen
  display.display();
  delay(2000);

  // Clear the buffer
  display.clearDisplay();

  // Convert interval in milliseconds
  interval = interval * 1000;
}

void loop() {

  // Read humidity and temperature
  hum = dht.readHumidity();
  Serial.print(hum,1);
  Serial.print(":");

  temp = dht.readTemperature();
  Serial.print(temp,1);
  Serial.print(":");

  // Display values on screen
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Thong tin");
  display.print("Tmp: ");
  display.print(temp,1);
  display.println("C");

  display.print("Hum: ");
  display.print(hum,1);
  display.println("%");
  //display.display();

  // Calculates humidex index and display its status
  humidex = temp + (0.5555 * (0.06 * hum * pow(10,0.03*temp) -10));
  Serial.println(humidex,1);

  // Humidex category
  if (humidex < 20){
//    Serial.println("No index");
    display.println("No index");
  }
  else if (humidex >= 20 && humidex < 27){
//    Serial.println("Comfort");
    display.println("De Chiu");
  }
  else if (humidex >= 27 && humidex < 30){
//    Serial.println("Caution");
    display.println("Can Than!");
  }
  else if (humidex >= 30 && humidex < 40){
//    Serial.println("Extreme caution");
    display.println("Can Than!!");
  }
  else if (humidex >= 40 && humidex < 55){
//    Serial.println("Danger");
    display.println("Nguy Hiem!");
  }
  else {
//    Serial.println("Extreme danger");
    display.println("NGUY HIEM");
  }

  display.display();

  delay(interval);
  display.clearDisplay();

}

Nguồn tham khảo

Mã nguồn mở được lấy từ dự án Room Comfort Live Display (and Log with MATLAB) được chia sẻ trên diễn đàn Arduino với giấy phép GPL3+.

Theo dõi
Thông báo của
guest
0 Comments
Phản hồi nội tuyến
Xem tất cả bình luận