Using the DHT11 Temperature Sensor with MicroPython on a NodeMCU
DHT11 Temperature Sensor
- Connect the VCC pin to a 3.3v power pin
- Connect the GND pin to a GND pin.
- Connect the Data pin to pin D4.
- There is a DHT11 module built-in to MicroPython
- Import the dht module.
- Set which data pin on the NodeMCU is connected to the data pin on the DHT11.
- The data pin is the GPIO pin number, not the data name. So pin D4 is (GPIO) pin 2 on the ESP-12:
- Temperature is in Celsius, RH is in %.
- To convert from Celsius to Fahrenheit: F = (C * 1.8 + 32)
from machine import Pin
## D4 = GPIO2
d = dht.DHT11(Pin(2))
## Can only collect temperature every ~2 seconds, so sleep 5s.
c = d.temperature()
h = d.humidity()
f = (c * 1.8 + 32)
print( "Temp is " + str(c) + "C (" + str(f) + "F) and RH is " + str(h) )
# Temperature is in C, RH is in %.
Bare vs. Shielded DHT11
- A DHT 11 with a shield has 3 pins.
- A bare DHT11 has 4 pins.
- In either case, 3vs4 pins, there are only 3 pins used: Signal, Vcc (+), Ground (-).
- The shielded, or PCB mounted, version includes a 10K Ohm pull up resistor for the signal line.