Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.
In this article, we will see how to work with DHT11 sensor with NodeMCU v2 using PlatformIO.
Hardware Check
We should always check datasheets before working with any hardware.
So, by checking the datasheet of DHT11 and NodeMCU v2, we can determine how connection should be done and how to create firmware for it.
You can check datasheets here:
- DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
- NodeMCU v2: https://asset.conrad.com/media10/add/160267/c1/-/en/001408527DS01/datablad-1408527-seeed-studio-nodemcu-v2-lua-based-esp8266-development-kit-developmentboard.pdf
Power Compatibility
As per the datasheet of DHT11, it can operate on a supply range of 3V to 5.5V DC.
Since the NodeMCU v2 runs on 3.3V.
You can power the sensor directly from the NodeMCU's 3V3 pin. This keeps your build simple and safe without needing extra voltage regulators.
Also they recommend to use a pull-up resistor of 4.7K to 10K between VDD and DATA pins.To make sure data is read properly.
Hardware Identification
Check which version of the DHT11 you have. This changes how many pins you’ll be dealing with.
| Feature | Bare DHT11 Sensor | DHT11 Module |
|---|---|---|
| Appearance | Blue plastic square with 4 metal pins. | Sensor soldered onto a small PCB. |
| Pin Count | 4 Pins. | 3 Pins. |
| Resistor | Required. You must add your own 4.7K to 10K resistor. | Built-in. Usually pre-soldered on the board. |
Step-by-Step Wiring Guide
For the 4-Pin Bare Sensor
If you are using the bare sensor, follow this pinout based on the official datasheet:
- Pin 1 (VDD): Connect to the 3V3 pin on the NodeMCU.
- Pin 2 (DATA): Connect to a digital pin like D1 or D2.
- Crucial Step: Place a 4.7K to 10K pull-up resistor between Pin 1 (VDD) and Pin 2 (DATA). This prevents the signal from "floating" and ensures stable data readings.
- Pin 3 (NULL): Leave this disconnected. It serves no electrical purpose.
- Pin 4 (GND): Connect to any GND pin on the NodeMCU.
For the 3-Pin Module
If you have the module, life is even easier. The board is usually labeled:
- VCC (+): Connect to 3V3.
- DATA (out): Connect to D1 or D2.
- GND (-): Connect to GND.
Why the Pull-up Resistor Matters?
Without a pull-up resistor, the DATA line doesn't have a clear "High" or "Low" state when the sensor isn't talking, which leads to erratic readings or "Sensor not found" errors.
If you're using the bare sensor, don't skip the 4.7K to 10K resistor (though anything from 4.7K to 10K usually works in a pinch!).
Coding Your DHT11 with PlatformIO
Now that the hardware is ready.
Since we are using PlatformIO, we’ll need to configure our project environment and use the industry-standard Adafruit library.
1. Configure platformio.ini
First, we need to tell PlatformIO which board we are using and which library to download. Open your platformio.ini file and ensure it looks like this:
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200
lib_deps =
adafruit/DHT sensor library @ ^1.4.6
adafruit/Adafruit Unified Sensor @ ^1.1.14
Note: The
Adafruit Unified Sensorlibrary is a hidden dependency that the DHT library needs to function correctly.
2. The Main Code (src/main.cpp)
In your project’s src folder, open main.cpp. We’ll use a modified version of your snippet specifically tuned for the DHT11.
#include <Arduino.h>
#include "DHT.h"
// Define the NodeMCU pin connected to the DHT11 data pin
// Using D2 (GPIO 4) as per our wiring guide
#define DHTPIN D2
// Since we are using the DHT11, we specify it here
#define DHTTYPE DHT11
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start serial communication at the speed defined in platformio.ini
Serial.begin(115200);
Serial.println(F("DHT11 Sensor Initializing..."));
// Start the sensor
dht.begin();
}
void loop() {
// Wait 2 seconds between measurements.
// DHT11 is slower than modern sensors; don't rush it!
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
float t = dht.readTemperature(); // Celsius
float f = dht.readTemperature(true); // Fahrenheit
// Check if any reads failed
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor! Check your wiring."));
return;
}
// Compute heat index (how it actually feels)
float hic = dht.computeHeatIndex(t, h, false);
// Print results to the Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% | Temp: "));
Serial.print(t);
Serial.print(F("°C | Heat Index: "));
Serial.print(hic);
Serial.println(F("°C"));
}
DHT11 Sensor Initializing...
Humidity: 45.00 % | Temp: 25.00°C | Heat Index: 25.00°C
Humidity: 46.00 % | Temp: 25.12°C | Heat Index: 25.12°C
Humidity: 45.00 % | Temp: 25.25°C | Heat Index: 25.25°C
Key Adjustments for the DHT11
-
DHTTYPE: Ensure this is set to
DHT11. While the code for a DHT22 is almost identical, the timing and data conversion logic inside the library are different for these two models. -
Sampling Rate: The DHT11 has a sampling rate of about 1Hz (one reading per second). Using a
delay(2000)(2 seconds) is a "safe bet" to ensure you don't get stale data or errors. - Accuracy Note: Don't be surprised if the readings are slightly different from your home thermostat. The DHT11 is an entry-level sensor with an accuracy of ±2°C for temperature and ±5% for humidity.
Conclusion
We could able to connect DHT11 sensor with NodeMCU v2 using PlatformIO and read temperature and humidity values.
In next article we will learn different sensor.
Any feedback or contributors are welcome! It’s online, source-available, and ready for anyone to use.
⭐ Star it on GitHub: https://github.com/HexmosTech/git-lrc


Top comments (0)