Build an Temperature Monitor with MicroPython ESP32
Are you ready to dive into the exciting world of IoT with MicroPython and ESP32? In this hands-on tutorial, we’ll guide you through creating a smart temperature and humidity monitor that’s perfect for beginners and seasoned makers alike. Get ready to code, connect, and create!
What You’ll Learn
- How to interface a DHT11 sensor with ESP32 using MicroPython
- Techniques for testing sensors with REPL commands
- Step-by-step guide to writing MicroPython scripts in Thonny
- Integrating an OLED display for real-time data visualization
- Tips and tricks for troubleshooting common issues
Let’s turn your ESP32 into a powerful IoT device!
Essential Gear for Your IoT Adventure
Before we embark on this coding journey, make sure you have:
- An ESP32 development board (our shining protagonist) (Affiliate) – Buy on AliExpress
- A DHT11 temperature and humidity sensor (Affiliate) – Buy on AliExpress
- 0.96″ OLED Display (I2C interface) (Affiliate) – Buy on AliExpress
- Breadboard and jumper wires (Affiliate): Buy on AliExpress
- Thonny IDE installed on your computer: Check the first post in this series
Got everything? Great! Let’s start building.
Mastering the DHT11 Sensor with ESP32
Quick Test: REPL Commands for Instant Gratification
- Connect your DHT11 sensor to the ESP32:
- VCC → 3.3V
- GND → GND
- DATA → GPIO 25 (feel free to change this pin)
- Fire up Thonny IDE and connect to your ESP32.
- In the REPL, let’s wake up our sensor. Copy&Paste the next code in the Shell and make sure the sensor is well connected or else it will show an error.
from machine import Pin import dht import time sensor = dht.DHT11(Pin(25)) sensor.measure() temp = sensor.temperature() hum = sensor.humidity() print(f'Temperature: {temp:.1f}°C') print(f'Humidity: {hum:.1f}%')
Voilà! You should see your first temperature and humidity readings. Exciting, right?
Leveling Up: Continuous Monitoring with Thonny Editor
Now, let’s create a script for non-stop environmental tracking:
- In Thonny, create a new file and save it as
dht11_monitor.py
on your ESP32. - Copy this code snippet:
from machine import Pin import dht import time sensor = dht.DHT11(Pin(25)) while True: try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() print(f'Temperature: {temp:.1f}°C') print(f'Humidity: {hum:.1f}%') except OSError as e: print('Sensor reading failed. Retrying...') time.sleep(2)
- Run the script and watch as your ESP32 becomes a weather station!
This script checks the sensor every 2 seconds, giving you a constant stream of data. The try-except block ensures your program keeps running even if there’s a hiccup in the sensor readings.
Adding Visual Flair: Integrating an OLED Display
Setting Up the SSD1306 Library
Before we can make our display shine, we need to install the SSD1306 library:
- In Thonny, navigate to Tools > Manage packages.
- Search for “ssd1306” and install the “micropython-ssd1306” package.
To verify, type
import ssd1306
in the REPL. No errors? You’re golden!
The Grand Finale: DHT11 and OLED in Perfect Harmony
Let’s bring it all together with a script that reads the DHT11 and displays data on the OLED:
- Connect your OLED display to the ESP32:
- VCC → 3.3V
- GND → GND
- SCL → GPIO 22
- SDA → GPIO 21
- Create a new file in Thonny and save it as
smart_temp_monitor.py
on your ESP32. - Paste in this code:
from machine import Pin, SoftI2C import ssd1306 import dht import time # Sensor setup sensor = dht.DHT11(Pin(25)) # OLED magic i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) while True: try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() oled.fill(0) oled.text('Smart Temp Monitor', 0, 0) oled.text(f'Temp: {temp:.1f} C', 0, 20) oled.text(f'Humidity: {hum:.1f}%', 0, 40) oled.show() print(f'Temperature: {temp:.1f}°C') print(f'Humidity: {hum:.1f}%') except OSError as e: print('Sensor error. Retrying...') time.sleep(2)
- Run the script and watch your creation come to life!
External Resources for Further Learning
To deepen your understanding of MicroPython and enhance your projects, check out these valuable resources:
- MicroPython Documentation – Official documentation with guides and API references.
- ESP32 Resources – A great collection of tutorials focused on ESP32 projects.
- DHT11 Sensor Guide – Detailed instructions on using DHT sensors effectively.
- SSD1306 OLED Display Guide – Learn how to interface with OLED displays.
Troubleshooting Tips for the Intrepid Maker
Hit a snag? Don’t worry, it happens to the best of us. Try these quick fixes:
- Double-check all your wiring connections.
- Ensure you’ve installed the necessary libraries.
- Verify you’re using the correct pin numbers for your specific ESP32 board.
- If all else fails, try turning it off and on again (yes, it works for IoT too!).
- Try to reset the board.
What’s Next on Your IoT Journey?
Congratulations, maker! You’ve just built an IoT temperature and humidity monitor. But why stop here? Consider these exciting enhancements:
- Add a web server to access your data from anywhere in the world.
- Implement data logging to track environmental trends over time.
- Integrate additional sensors for a comprehensive smart home system.
Conclusion: Your Gateway to the World of IoT
You’ve taken your first steps into the vast world of IoT with MicroPython and ESP32. Remember, every great invention started with a simple idea and a bit of code. Keep experimenting, keep learning, and most importantly, keep making!