Project 2.6: "Basic Weather Station"
๐ Project 2.6 โ Basic Weather Station (DHT11 + LCD1602)
ย
๐ฏ What Youโll Learn
- โ Goal 1: Read temperature and humidity with the DHT11 sensor.
- โ Goal 2: Display values on the LCD1602 screen.
- โ Goal 3: Trigger alerts when values pass thresholds.
Key Ideas
- Short definition: Sensors measure the environment, displays show the data.
- Realโworld link: Weather stations and smart homes use sensors + displays to monitor conditions.
๐งฑ Blocks Glossary (used in this project)
- Digital input (DHT11):
sensor = dht.DHT11(machine.Pin(X))โ Setup sensor. - Measure:
sensor.measure()โ Take a reading. - Temperature:
sensor.temperature()โ Get ยฐC. - Humidity:
sensor.humidity()โ Get %. - LCD setup:
lcd = LCD1602(rs, en, d4, d5, d6, d7)โ Setup display pins. - LCD print:
lcd.puts("text", row, col)โ Show text on screen. - Serial print:
print("...")โ Show values on computer. - Loop:
while True:โ Repeat forever. - Delay:
time.sleep()โ Pause between readings.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| DHT11 sensor | 1 | Pin 4 (data) |
| LCD1602 | 1 | RS=14, EN=12, D4=13, D5=5, D6=18, D7=19 |
๐ Wiring tip: Connect DHT11 data to pin 4, VCC to 3.3V, GND to GND. LCD1602 uses 6 pins for parallel data.
๐ Pin map snapshot: Pin 4 (DHT11), Pins 14/12/13/5/18/19 (LCD).
โ Before You Start
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 2.6.1 โ Read temperature and humidity
Goal: Show sensor values in serial monitor.
Blocks used: DHT11 setup, measure, print.
Block sequence:
- Setup sensor on pin 4
- Measure values
- Print temperature and humidity
- Repeat
MicroPython Code:
# Read temperature and humidity
import machine, time, dht # Import pin control, time, and DHT library
sensor = dht.DHT11(machine.Pin(4)) # Setup DHT11 sensor on pin 4
print("DHT11 ready") # Serial start message
while True: # Infinite loop
sensor.measure() # Take a measurement
temp = sensor.temperature() # Read temperature in ยฐC
hum = sensor.humidity() # Read humidity in %
print("Temp:", temp, "C Hum:", hum, "%") # Print values to serial
time.sleep(2) # Delay 2 seconds between readings
Reflection: You can now see live temperature and humidity values.
Challenge: Print โHot!โ if temp > 30, โCoolโ otherwise.
๐ฎ Microproject 2.6.2 โ Show values on LCD
Goal: Display sensor values on LCD1602.
Blocks used: LCD setup, LCD print.
Block sequence:
- Setup LCD pins
- Measure sensor
- Print values on LCD
MicroPython Code:
# Show values on LCD1602
import machine, time, dht, lcd1602 # Import pin control, time, DHT, and LCD library
sensor = dht.DHT11(machine.Pin(4)) # Setup DHT11 sensor
lcd = lcd1602.LCD1602(rs=14, en=12, d4=13, d5=5, d6=18, d7=19) # Setup LCD pins
print("LCD ready") # Serial start message
while True:
sensor.measure() # Take measurement
temp = sensor.temperature() # Temperature
hum = sensor.humidity() # Humidity
lcd.clear() # Clear LCD screen
lcd.puts("Temp:"+str(temp)+"C", 0, 0) # Print temperature on row 0
lcd.puts("Hum:"+str(hum)+"%", 1, 0) # Print humidity on row 1
print("Displayed on LCD") # Serial confirmation
time.sleep(2) # Delay
Reflection: The LCD shows both values clearly.
Challenge: Add degree symbol โยฐCโ if library supports it.
๐ฎ Microproject 2.6.3 โ Threshold alert
Goal: Show alert when temperature too high.
Blocks used: if/else, LCD print.
Block sequence:
- Measure sensor
- If temp > 30 โ show โHOT!โ
- Else โ show normal values
MicroPython Code:
# Threshold alert on LCD
import machine, time, dht, lcd1602
sensor = dht.DHT11(machine.Pin(4)) # Setup DHT11
lcd = lcd1602.LCD1602(rs=14, en=12, d4=13, d5=5, d6=18, d7=19)
print("Threshold alert ready")
while True:
sensor.measure() # Measure
temp = sensor.temperature() # Temperature
hum = sensor.humidity() # Humidity
lcd.clear() # Clear LCD
if temp > 30: # If too hot
lcd.puts("HOT!", 0, 0) # Show HOT message
print("HOT!") # Serial alert
else: # Otherwise
lcd.puts("Temp:"+str(temp)+"C", 0, 0) # Normal temp
lcd.puts("Hum:"+str(hum)+"%", 1, 0) # Normal humidity
time.sleep(2)
Reflection: The LCD warns when temperature is high.
Challenge: Add buzzer beep when HOT.
๐ฎ Microproject 2.6.4 โ Humidity alert
Goal: Show alert when humidity too low.
Blocks used: if/else, LCD print.
Block sequence:
- Measure sensor
- If hum < 40 โ show โDRY!โ
- Else โ show normal values
MicroPython Code:
# Humidity alert on LCD
import machine, time, dht, lcd1602
sensor = dht.DHT11(machine.Pin(4))
lcd = lcd1602.LCD1602(rs=14, en=12, d4=13, d5=5, d6=18, d7=19)
print("Humidity alert ready")
while True:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
lcd.clear()
if hum < 40: # If humidity too low
lcd.puts("DRY!", 0, 0) # Show DRY message
print("DRY!") # Serial alert
else:
lcd.puts("Temp:"+str(temp)+"C", 0, 0)
lcd.puts("Hum:"+str(hum)+"%", 1, 0)
time.sleep(2)
Reflection: The LCD warns when humidity is low.
Challenge: Add LED ON when DRY.
ย
๐ฎ Microproject 2.6.5 โ Combined weather station (continuaciรณn y completo)
Goal: Show both values and alerts for temperature and humidity.
Blocks used: DHT11, LCD, if/else, serial print.
Block sequence:
- Measure sensor values
- Show values on LCD
- If temp > 30 โ HOT alert
- If hum < 40 โ DRY alert
- Print alerts on serial
MicroPython Code:
# Combined weather station
import machine, time, dht, lcd1602 # Import pin control, time, DHT, and LCD library
sensor = dht.DHT11(machine.Pin(4)) # Setup DHT11 sensor on pin 4
lcd = lcd1602.LCD1602(rs=14, en=12, d4=13, d5=5, d6=18, d7=19) # Setup LCD pins
print("Weather station ready") # Serial start message
while True: # Infinite loop
sensor.measure() # Take measurement
temp = sensor.temperature() # Read temperature
hum = sensor.humidity() # Read humidity
lcd.clear() # Clear LCD before writing
lcd.puts("Temp:"+str(temp)+"C", 0, 0) # Show temperature on row 0
lcd.puts("Hum:"+str(hum)+"%", 1, 0) # Show humidity on row 1
if temp > 30: # If temperature too high
print("HOT ALERT") # Serial alert
lcd.puts("HOT!", 0, 10) # Show HOT on LCD at row 0, col 10
if hum < 40: # If humidity too low
print("DRY ALERT") # Serial alert
lcd.puts("DRY!", 1, 10) # Show DRY on LCD at row 1, col 10
time.sleep(2) # Delay between readings
Reflection: The LCD shows values and alerts together, making a complete weather station.
Challenge:
- Easy: Add buzzer beep when HOT or DRY.
- Harder: Blink LED twice when both HOT and DRY happen at the same time.
โจ Main Project โ Weather Station with Alerts
๐ง Blocks Steps (with glossary)
- Digital input (DHT11): Measure temperature and humidity.
- LCD output: Show values and alerts.
- Serial print: Log values and alerts.
- Logic (if/else): Decide when to show HOT or DRY.
- Loop: Repeat continuously.
Block sequence:
- Setup DHT11 sensor and LCD pins.
- Measure temperature and humidity.
- Display values on LCD.
- If temp > 30 โ HOT alert.
- If hum < 40 โ DRY alert.
- Print alerts on serial.
- Repeat loop.
๐ MicroPython Code (lineโbyโline comments)
# Project 2.6 โ Basic Weather Station (Main Project)
import machine, time, dht, lcd1602 # Import pin control, time, DHT library, and LCD library
sensor = dht.DHT11(machine.Pin(4)) # Setup DHT11 sensor on pin 4
lcd = lcd1602.LCD1602(rs=14, en=12, d4=13, d5=5, d6=18, d7=19) # Setup LCD1602 with pins
print("Weather Station Main ready") # Serial start message
while True: # Infinite loop
sensor.measure() # Take measurement from DHT11
temp = sensor.temperature() # Read temperature in ยฐC
hum = sensor.humidity() # Read humidity in %
lcd.clear() # Clear LCD before writing
lcd.puts("Temp:"+str(temp)+"C", 0, 0) # Show temperature on row 0
lcd.puts("Hum:"+str(hum)+"%", 1, 0) # Show humidity on row 1
if temp > 30: # If temperature too high
print("HOT ALERT") # Serial alert
lcd.puts("HOT!", 0, 10) # Show HOT on LCD
if hum < 40: # If humidity too low
print("DRY ALERT") # Serial alert
lcd.puts("DRY!", 1, 10) # Show DRY on LCD
time.sleep(2) # Delay between readings
๐ External Explanation
- What it teaches: How to combine sensor input with a display and alerts.
- Why it works: The DHT11 provides values, the LCD shows them, and logic decides when to alert.
- Key concept: Sensors + outputs = interactive weather station.
โจ Story Time
Your robot is now a tiny meteorologist ๐ฆ๏ธ. It measures the air, shows the data on its screen, and warns you when itโs too hot or too dry.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 2.6.A โ LCD shows random characters
Problem: LCD prints garbage instead of numbers.
Clues: Wrong wiring or missing lcd.clear().
Broken code:
lcd.puts("Temp:"+temp+"C", 0, 0) # Missing str() conversion
Fixed code:
lcd.puts("Temp:"+str(temp)+"C", 0, 0) # Convert number to string
Why it works: LCD requires text, not raw numbers.
Avoid next time: Always wrap numbers with str().
๐ Debugging 2.6.B โ Sensor always reads 0
Problem: DHT11 shows 0 for temp and humidity.
Clues: Forgot to call measure().
Broken code:
temp = sensor.temperature() # No measure() before
Fixed code:
sensor.measure() # Take measurement first
temp = sensor.temperature() # Then read values
Why it works: DHT11 must be triggered before reading.
Avoid next time: Always call measure() before reading.
โ Final Checklist
- I saw temperature and humidity values on LCD.
- HOT alert appeared when temp > 30.
- DRY alert appeared when hum < 40.
- Serial printed values and alerts.
- My challenge changes worked (buzzer or LED).
๐ Extras
- ๐ง Student tip: Try covering the sensor with your hand to see humidity rise.
- ๐งโ๐ซ Instructor tip: Show how
str()converts numbers to text for LCD. - ๐ Glossary: DHT11, LCD1602, humidity, threshold, alert.
- ๐ก Mini tips:
- Always call
sensor.measure()before reading. - Use
lcd.clear()to avoid overlapping text. - Keep delays around 2 seconds for stable readings.
- Always call