MicroPython
Interfacing Sensors
![]() |
DHT-11 Sensor interphase with NodeMCU |
At the end of this, you will be able to interphase DHT11 Temperature Sensor with ESP 8266 NodeMCU bare-metal and also by attaching external LED's. The whole process will be done by MicroPython programming. So, Let's begin with !
Note: You must have your MicroPython firmware pre-flashed onto your board before beginning with this. If not, follow up these before getting back here.
MicroPython #1: Introduction and Gearing Up
MicroPython #3: PWM, LED Fade
Introduction to DHT 11 and Sensor Interphase
The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin. Here, we'll operate it on Analog mode.
There is a dht module that comes with the MicroPython firmware by default. So, it is easy to get temperature and humidity.
Implementation on ESP8266 (NodeMCU)
Using MicroPython Temperature data acquisition can be easily implemented and you will be able to read Temperature and Humidity data. Let's go through then !
> Call the time
> Now, Import the standard module for reading DHT sensor by 'import dht' and assign pin definition by 'dht.DHT11(PIN(14))'. DHT11/22 is an option and any ADC pin on bare-metal can be assigned for the same. (pin 14 here)
> Now, it's all about acquiring the data from dht. We have already imported and set all factors and made declarations. Call temperature as 'sensor.temperature()' and humidity as 'sensor.humidity()'
Putting these functions in while loop with try/except logic its all set to go.
------------------------------
from time import sleep
import dht
sensor = dht.DHT11(Pin(14))
while True:
try:
sleep(2)
sensor.measure()
temp = print('Temp:',sensor.temperature(),'*C')
hum = print('Humi:',sensor.humidity(),'%')
except OSError as e:
print('Failed to read sensor.')
You will find logging of Temperature and Humidity data on the terminal with the delay being set.
This can be upgraded to the next level by setting a temperature triggered Buzzer or LED. Add a statement saying 'if (temperature>38): machine.Pin(2, OUT)' saying to turn LED on a certain threshold. In the below image, the green LED indicates humidity is above 90% and red one says the temperature is above 37*C. That's something interesting !
![]() |
DHT-11 Temperature/Humidity Alerting System |

Result and Display:
Just drop down your comments, troubles or any bugs you found on the way in reaching so far in the comment section below.
GitHub link for source codes: https://github.com/NavadeepGaneshU/CL3VERTRONICS
Hope you followed up things tight.
Cheers !!!