๐Ÿ”ฆ Level 2 โ€“ Sensors & IR Control

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

PartHow many?Pin connection
D1 R321USB cable
DHT11 sensor1Pin 4 (data)
LCD16021RS=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:

  1. Setup sensor on pin 4
  2. Measure values
  3. Print temperature and humidity
  4. 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:

  1. Setup LCD pins
  2. Measure sensor
  3. 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:

  1. Measure sensor
  2. If temp > 30 โ†’ show โ€œHOT!โ€
  3. 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:

  1. Measure sensor
  2. If hum < 40 โ†’ show โ€œDRY!โ€
  3. 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:

  1. Measure sensor values
  2. Show values on LCD
  3. If temp > 30 โ†’ HOT alert
  4. If hum < 40 โ†’ DRY alert
  5. 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:

  1. Setup DHT11 sensor and LCD pins.
  2. Measure temperature and humidity.
  3. Display values on LCD.
  4. If temp > 30 โ†’ HOT alert.
  5. If hum < 40 โ†’ DRY alert.
  6. Print alerts on serial.
  7. 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.
On this page