当前位置:网站首页>4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED

4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED

2022-06-21 22:57:00 Zhang San programming sharing

Hardware environment

1、ESP8266 Development board

 Insert picture description here

2、OLED display

This screen is displayed by I2C Control by , A resolution of 128*65, The driver chip is SSD1306 0.96 Inch .
 Insert picture description here
Pins are defined as follows :

Pin number Definition describe
1GND Access the development board GND
2VDD Access the development board VCC(3.3V–5V)
3SCK(SCL) Access the development board I2C Of SCL
4SDA Access the development board I2C Of SDA

3、DHT11 Temperature and humidity sensor

DHT11 It is a temperature and humidity sensor , Can pass 2 Foot's Singal(out) Transmit the detected ambient temperature to in real time ESP8266.
 Insert picture description here
Pins are defined as follows :

Pin number Definition describe
1VCC Access the development board VCC(3.3V)
2Singal(out) Access the development board D5(GPIO14)
3NC It's empty
4GND Access the development board GND

4、 Wiring way

fritzing Sketch Map :
 Insert picture description here
 Insert picture description here
Connection physical drawing :
 Insert picture description here

Software environment

1、 Online installation DHT sensor library for ESPx

Click on Tools --> Management of the library Search for DHT11, Choose to install DHT sensor library for ESPx.
 Insert picture description here

2、 Experiment source code

/************************************************************************************** ESP8266 NodeMCU interfacing with SSD1306 OLED and DHT11 sensor This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h> // Include DHT library code

#define OLED_RESET 5 // define SSD1306 OLED reset at ESP8266 GPIO5 (NodeMCU D1)
Adafruit_SSD1306 display(OLED_RESET);

#define DHTPIN 14 // DHT11 data pin is connected to ESP8266 GPIO14 (NodeMCU D5)
#define DHTTYPE DHT11 // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE);             // Configure DHT library

char temperature[] = "00.0 C";
char humidity[]    = "00.0 %";

void setup(void)
{
    

 Serial.begin(9600);
 delay(1000);

 Wire.begin(4, 0);           // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz

 // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
 display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
 // init done

 dht.begin();                // Initialize the DHT library

 // Clear the display buffer.
 display.clearDisplay();

 display.drawFastHLine(0, 32, SSD1306_LCDWIDTH, WHITE);

 display.setTextSize(1);
 display.setTextColor(WHITE, BLACK);
 display.setCursor(10, 5);
 display.print("DHT11 TEMPERATURE:");
 display.setCursor(19, 37);
 display.print("DHT11 HUMIDITY:");
 display.display();
}

void loop()
{
    
 // Read humidity
 byte RH = dht.readHumidity();
 //Read temperature in degree Celsius
 byte Temp = dht.readTemperature();

 temperature[0] = Temp / 10 + '0';
 temperature[1] = Temp % 10 + '0';
 humidity[0]    = RH / 10   + '0';
 humidity[1]    = RH % 10   + '0';

 // print data on serial monitor
 Serial.printf("Temperature = %02u°C\r\n", Temp);
 Serial.printf("Humidity = %02u %%\r\n\r\n", RH);

 // print data on the SSD1306 display
 display.setCursor(46, 20);
 display.print(temperature);
 display.setCursor(46, 52);
 display.print(humidity);
 display.drawRect(71, 20, 3, 3, WHITE);     // Put degree symbol ( ° )
 display.display();

 delay(1000);

}

Temperature and humidity display experiment

Effect display video

ESP8266 Development version passed OLED real-time display DHT11 Temperature and humidity parameters

原网站

版权声明
本文为[Zhang San programming sharing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212111309841.html