Last time I’ve connected ESP8266 NodeMCU development board (ESP-12E) with DHT11 sensor and SSD1306 OLED display. This post shows how to interface the NodeMCU with DHT22 (AM2302) digital humidity/temperature sensor and SSD1306 display (128×64 Pixel).
The NodeMCU reads temperature and relative humidity from the DHT22 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
ESP8266 NodeMCU interfacing with DHT11 sensor and SSD1306
Arduino with DHT22 sensor and LCD
Hardware Required:
- NodeMCU development board
- SSD1306 OLED screen (128×64 Pixel)
- DHT22 (AM2302) temperature and humidity sensor
- 4.7k ohm resistor
- Micro USB cable (for programming and powering the whole circuit)
- Breadboard
- Jumper wires
Interfacing NodeMCU with DHT22 sensor and SSD1306 OLED 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 DHT22 (AM2302) sensor has 4 pins (from left to right): VCC (+5V), 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 DHT22 (AM2302) sensor are supplied with 3.3V which comes from the NodeMCU board.
Interfacing NodeMCU with DHT22 sensor and SSD1306 OLED code:
In this project i used two libraries from Adafruit: the first one is for the SSD1306 OLED display (with graphics library) 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | /************************************************************************************** ESP8266 NodeMCU interfacing with SSD1306 OLED and DHT22 (AM2302) 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 // DHT22 data pin is connected to ESP8266 GPIO14 (NodeMCU D5) #define DHTTYPE DHT22 // DHT22 sensor is used DHT dht(DHTPIN, DHTTYPE); // configure DHT library char temperature[] = "000.0 C"; char humidity[] = "000.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("DHT22 TEMPERATURE:"); display.setCursor(19, 37); display.print("DHT22 HUMIDITY:"); display.display(); } void loop() { // Read humidity int RH = dht.readHumidity() * 10; //Read temperature in degree Celsius int Temp = dht.readTemperature() * 10; // print data on serial monitor Serial.printf("Temperature = %5.1f°C\r\n", (float)Temp/10); Serial.printf("Humidity = %5.1f %%\r\n\r\n", (float)RH/10); if(Temp < 0){ temperature[0] = '-'; Temp = abs(Temp); } else temperature[0] = ' '; temperature[1] = (Temp / 100) % 10 + '0'; temperature[2] = (Temp / 10) % 10 + '0'; temperature[4] = Temp % 10 + '0'; if(RH >= 1000) humidity[0] = '1'; else humidity[0] = ' '; humidity[1] = (RH / 100) % 10 + '0'; humidity[2] = (RH / 10) % 10 + '0'; humidity[4] = RH % 10 + '0'; // print data on the SSD1306 display display.setCursor(40, 20); display.print(temperature); display.setCursor(40, 52); display.print(humidity); display.drawRect(71, 20, 3, 3, WHITE); // Put degree symbol ( ° ) display.display(); delay(1000); } |
Arduino IDE serial monitor output for temperature and relative humidity is shown below:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.