This Simple Projects post shows how to interface the ESP8266 NodeMCU development board with DHT11 digital humidity & temperature sensor and SSD1306 OLED display (128×64 Pixel).
The NodeMCU reads temperature and relative humidity values from the DHT11 sensor and prints it on Arduino IDE serial monitor and the SSD1306 OLED display.
The SSD1306 OLED used in this post projects is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode or I2C mode) such as soldering, placing jumpers ….
Related Projects:
To see how to connect the SSD1306 OLED with NodeMCU board see the following post:
ESP8266 NodeMCU interfacing with SSD1306 OLED
Arduino interfacing with DHT11 sensor and LCD
Interfacing NodeMCU with DHT22 sensor and SSD1306 OLED
Hardware Required:
- NodeMCU development board
- SSD1306 OLED screen (128×64 Pixel)
- DHT11 temperature and humidity sensor
- 4.7k ohm resistor
- Micro USB cable (for programming and powering the whole circuit)
- Breadboard
- Jumper wires
ESP8266 NodeMCU interfacing with DHT11 sensor circuit:
Project circuit schematic diagram is shown below.
and the second image shows fritzing breadboard circuit:
The SDA and SCL lines of the I2C bus come from GPIO4 (D2) and GPIO0 (D3) of the NodeMCU board (respectively), they are connected to SDA and SCL (SCK) pins of the SSD1306 display module.
Reset pin (RES) of the display module is connected to GPIO5 (D1) of the NodeMCU development board.
The DHT11 sensor has 4 pins (from left to right): VCC, data pin, NC (not connected pin) and GND. The data pin is connected to pin D5 (GPIO14). A pull-up resistor of 4.7k ohm is required for the data pin.
The SSD1306 display module and the DHT11 sensor are supplied with 3.3V which comes from the NodeMCU board.
ESP8266 NodeMCU interfacing with DHT11 sensor code:
In this project i used two libraries from Adafruit: the first one is for the SSD1306 OLED display and the second is for the DHT11 sensor.
Adafruit library for DHT series (after extracting put the folder named DHT in the library folder of Arduino):
Download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /************************************************************************************** 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); } |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.