Learn
Reading Temperature and Humidity from a DHT11 on a Raspberry Pi 4B
Wire a DHT11 temperature/humidity sensor to a Raspberry Pi 4B GPIO pin and read live values in Python — wiring, starter code, and how to confirm it's really working.
Try this directly in the free Raspberry Pi 4B simulator — no hardware or signup required. New to the GPIO header? Start with the interactive pinout guide.
What you’ll need
- Raspberry Pi 4B
- DHT11 Temp/Humidity Sensor
Step by step
- Drag Raspberry Pi 4B onto the Canvas.
- Drag the DHT11 onto the Canvas.
- Hover over its pins to confirm the actual labels — most likely VCC, DATA, GND.
- Wire it up: VCC → Pi's 5V (check the component's hover tooltip or settings panel to confirm the correct voltage, since DHT11 breakouts vary by brand). GND → Pi's GND. DATA → any GPIO pin (e.g. pin 37).
- Go to the Code tab and write a test script:
import adafruit_dhtimport boardimport asyncio dht = adafruit_dht.DHT11(board.D37) # adjust pin to match your wiring while True: try: temperature = dht.temperature humidity = dht.humidity print(f"Temp: {temperature}C Humidity: {humidity}%") except RuntimeError as e: print(f"Reading error: {e}") await asyncio.sleep(2)- Click Start.
What “working correctly” looks like
- A temperature and humidity value print repeatedly, staying reasonably stable — small natural fluctuation is fine, wild jumps on every read are not.
- If this component has a settings panel, check it when selected — you may be able to set a custom simulated value directly, worth testing by setting an extreme value (e.g. 40°C) and confirming the script reads back exactly that.
If something’s wrong
- Import error, or nothing happens at all → check this component's own example/starter code in the app for the actual expected approach, rather than trusting the script above as final.