iT邦幫忙

2023 iThome 鐵人賽

DAY 24
0
SideProject30

人機控制介面之開發應用系列 第 25

溫溼度顯示於背光顯示面板-ESP32

  • 分享至 

  • xImage
  •  

ESP32程式碼

#include "rgb_lcd.h"
#include <DHT11.h>

DHT11 dht11(4);
rgb_lcd lcd;

void setup()
{
    Serial.begin(9600);
    lcd.begin(16, 2);
    
    lcd.print("Temp:       ");
    lcd.setCursor(0, 1); // move cursor to start of 2nd row
    lcd.print("Humidity:   ");
}

void loop()
{
    char _buffer[10];  // Extended buffer to fit Chinese characters

    int temperature = dht11.readTemperature();
    int humidity = dht11.readHumidity();

    if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT &&
        humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT)
    {
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.println("C");

        Serial.print("Humidity: ");
        Serial.print(humidity);
        Serial.println("%");
    }
    else
    {
        if (temperature == DHT11::ERROR_TIMEOUT || temperature == DHT11::ERROR_CHECKSUM)
        {
            Serial.print("Temperature Reading Error: ");
            Serial.println(DHT11::getErrorString(temperature));
        }
        if (humidity == DHT11::ERROR_TIMEOUT || humidity == DHT11::ERROR_CHECKSUM)
        {
            Serial.print("Humidity Reading Error: ");
            Serial.println(DHT11::getErrorString(humidity));
        }
    }

    delay(1000);

    sprintf(_buffer, "%03d C", temperature);
    lcd.setCursor(10, 0);
    lcd.print(_buffer);
    Serial.println(_buffer);

    sprintf(_buffer, "%03d %%", humidity);  // Changed "in" to "%" for humidity.
    lcd.setCursor(10, 1);
    lcd.print(_buffer);
    Serial.println(_buffer);
}

上視圖
https://ithelp.ithome.com.tw/upload/images/20231019/20160744CDkOdR92Kp.jpg

程式碼ino檔 github下載

實作影片


上一篇
光敏電阻與七段顯示器結合應用-Arduino
下一篇
Linux 常用指令
系列文
人機控制介面之開發應用37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言