DHT11 Temperature and Humidity

🎮 The Weather Station

Microproject Image

Module Goal: measure the air itself temperature and humidity in one white box. This sensor is different from everything before it: it has a tiny brain inside and speaks its own digital language. 

📦 CONCEPT BOX The sensor with its own brain

Every sensor until today answered with a signal: a pin going HIGH/LOW, or a voltage that the ADC converted. The DHT11 is different it does its own measuring and hands you a finished number. 

Inside that white box with the blue grid lives a humidity sensor, a temperature sensor, and a small chip. When you ask for a reading, the chip answers over the same wire with its own secret protocol: a precise burst of pulses that encodes "temperature: 24, humidity: 48". The dhtx library listens to that burst and decodes it for you. 

Three consequences of having a brain: 

  1. It's slow: the chip needs about 2 seconds to take a measurement. Ask faster and it refuses or repeats old data.
  2. It's digital: no calibration, no ADC, no jitter the chip already did the converting.
  3. It's precise about units: you get degrees Celsius and percent, ready to use. 

Some sensors speak in signals; others speak in messages. Knowing which kind you're facing tells you how to treat it. 

Goal: 

your first weather report temperature and humidity together. 

Concepts: 

Protocol Sensor · Two-Value Reading · Reading Rhythm

import dhtx  # Controls the temperature and humidity sensor
import time  # Controls pauses and timing

# Sets up the DHT11 sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0

while True:  # Measure forever
    temp = dhtx.DHT11(26).temperature()  # Ask the chip for temperature in degrees Celsius
    hum = dhtx.DHT11(26).humidity()  # Ask the chip for relative humidity in percent
    print("Temperature:", temp, "C | Humidity:", hum, "%")  # Report both values
    time.sleep(2)  # MANDATORY 2 s: the chip needs this time to measure!

Serial Monitor (breathe on the sensor to see both numbers jump): 

Temperature: 24 C | Humidity: 48 % 

Temperature: 24 C | Humidity: 49 % 

Temperature: 27 C | Humidity: 74 % ← your breath! 

Temperature: 25 C | Humidity: 55 % 

💡 Note: breathe slowly on the sensor your breath is warm AND humid, so both numbers rise together. Two sensors in one box, witnessed live. And respect the 2-second rhythm: remove the sleep(2) and the chip stumbles, returning errors or stale numbers. Compare with the ultrasonic: that sensor wanted fast checking (a fast world); this one demands patience (air changes slowly). The reading rhythm always belongs to the physics.

🎮 Describe the Environment

Microproject Image

Goal: 

translate both numbers into human comfort words. 

Concepts: 

Double Classification · Two Independent Funnels

import dhtx  # Controls the temperature and humidity sensor
import time  # Controls pauses and timing

# Sets up the DHT11 sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0

while True:  # Measure forever
    temp = dhtx.DHT11(26).temperature()  # Temperature in degrees Celsius
    hum = dhtx.DHT11(26).humidity()  # Humidity in percent
    print("Temperature:", temp, "C | Humidity:", hum, "%")  # Raw values first
    if temp < 15:  # Below 15 degrees?
        print("Feels: COLD grab a jacket")  # Cold verdict
    elif temp < 27:  # Between 15 and 27?
        print("Feels: COMFORTABLE perfect zone")  # Pleasant verdict
    else:  # 27 degrees or more
        print("Feels: HOT find some shade")  # Hot verdict
    if hum < 30:  # Under 30 percent of moisture?
        print("Air: DRY drink some water")  # Dry verdict
    elif hum < 60:  # Between 30 and 60?
        print("Air: COMFORTABLE breathing easy")  # Pleasant verdict
    else:  # 60 percent or more
        print("Air: HUMID sticky weather")  # Humid verdict
    time.sleep(2)  # Respect the chip's rhythm

Serial Monitor: 

Temperature: 24 C | Humidity: 48 % 

Feels: COMFORTABLE perfect zone

 Air: COMFORTABLE breathing easy 

Temperature: 28 C | Humidity: 71 % 

Feels: HOT find some shade

Air: HUMID sticky weather 

💡 Note: TWO funnels, completely independent temperature is judged and then humidity is judged, each with its own if/elif/else. Four verdict combinations are possible per reading (hot+humid = tropical, cold+dry = mountain winter...). Weather apps on your phone show exactly this pair of classifications, computed by exactly this pair of funnels.

🎮 The Heat Alert

Microproject Image

Goal: 

announce the moment temperature CROSSES into the hot zone once per crossing, not forever. 

Concepts: 

Zone Edges · State Memory · Applied Alerts

import dhtx  # Controls the temperature and humidity sensor
import time  # Controls pauses and timing

# Sets up the DHT11 sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0

hot = False  # Memory: is it currently hot?

while True:  # Watch forever
    temp = dhtx.DHT11(26).temperature()  # Temperature in degrees Celsius
    now_hot = temp >= 27  # Decide: is it hot RIGHT NOW?
    if now_hot == True and hot == False:  # Just ENTERED the hot zone!
        print(">>> HEAT ALERT! It reached", temp, "C")  # The crossing alarm
    if now_hot == False and hot == True:  # Just LEFT the hot zone
        print(">>> Cooling down. It's", temp, "C now.")  # The recovery message
    hot = now_hot  # Save the state
    time.sleep(2)  # Chip's rhythm

Serial Monitor (cup your hands around the sensor to warm it, then release): 

>>> HEAT ALERT! It reached 27 C 

(warm hands stay silence)

 >>> Cooling down. It's 25 C now. 

💡 Note: edge detection now watches the weather. To trigger the alarm, warm the sensor gently with cupped hands and notice how the 2-second rhythm makes the alert feel slow and dignified compared to the instant alarms of the button world. A greenhouse controller uses this exact crossing logic to open windows and switch on fans. Your code is one if away from real climate control.

🎮 The Record Keepers

Microproject Image

Goal: 

run the station while it tracks four lifetime records: hottest, coldest, most humid, driest. 

Concepts: 

Best-So-Far ×4 · Long-Running Statistics

import dhtx  # Controls the temperature and humidity sensor
import time  # Controls pauses and timing

# Sets up the DHT11 sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0

print("=== RECORD KEEPERS ===")  # Title
print("Tracking records. Reset the board to stop.")  # Instructions
first_t = dhtx.DHT11(26).temperature()  # First temperature becomes the starting record
hottest = first_t  # Champion of heat
coldest = first_t  # Champion of cold
first_h = dhtx.DHT11(26).humidity()  # First humidity becomes the starting record
most_humid = first_h  # Champion of moisture
driest = first_h  # Champion of dryness

while True:  # Run the station forever
    temp = dhtx.DHT11(26).temperature()  # Current temperature
    hum = dhtx.DHT11(26).humidity()  # Current humidity
    if temp > hottest:  # Hotter than the champion?
        hottest = temp  # New champion!
        print("NEW RECORD hottest:", hottest, "C")  # Celebrate
    if temp < coldest:  # Colder than the champion?
        coldest = temp  # New champion!
        print("NEW RECORD coldest:", coldest, "C")  # Celebrate
    if hum > most_humid:  # More humid than the champion?
        most_humid = hum  # New champion!
        print("NEW RECORD most humid:", most_humid, "%")  # Celebrate
    if hum < driest:  # Drier than the champion?
        driest = hum  # New champion!
        print("NEW RECORD driest:", driest, "%")  # Celebrate
    time.sleep(5)  # Report pace: every 5 seconds
    print("Now:", temp, "C |", hum, "%  |  Records:", hottest, "/", coldest, "C —", most_humid, "/", driest, "%")  # Status line

Serial Monitor (leave it running, breathe on it, fan it): 

=== RECORD KEEPERS === 

Tracking records. Reset the board to stop. 

NEW RECORD hottest: 28 C 

Now: 28 C | 50 % | Records: 28 / 24 C 55 / 45 % 

NEW RECORD most humid: 71 % 

Now: 27 C | 71 % | Records: 28 / 24 C 71 / 45 % 

Now: 25 C | 52 % | Records: 28 / 24 C 71 / 45 % 

💡 Note: four champion variables, four comparisons, one status line your program just became a historian: it remembers not just now, but the extremes of its whole life. Try to break each record (breath for humidity, a fan or cold window for temperature) and watch the NEW RECORD lines fire. Weather services do exactly this daily: "record high for this date" is a best-so-far comparison that has been running, uninterrupted, for a hundred years.

🎮 The Weather Channel

Microproject Image

Goal: 

the complete broadcast every 10 seconds, a formatted TV-style bulletin with readings, verdicts and records. 

Concepts: 

Report Assembly · Broadcast Rhythm · Full Pattern Stack

import dhtx  # Controls the temperature and humidity sensor
import time  # Controls pauses and timing

# Sets up the DHT11 sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0

print("=== CLU-WEATHER CHANNEL ===")  # The station ident
hottest = 0  # Starting record for heat (impossible-low)
coldest = 99  # Starting record for cold (impossible-high)
bulletin = 0  # Counts the broadcasts delivered

while True:  # Broadcast forever
    bulletin += 1  # One more bulletin on air
    temp = dhtx.DHT11(26).temperature()  # Measure temperature
    hum = dhtx.DHT11(26).humidity()  # Measure humidity
    if temp > hottest:  # New heat record?
        hottest = temp  # Crown it
    if temp < coldest:  # New cold record?
        coldest = temp  # Crown it
    print("=================================")  # Broadcast frame
    print("BULLETIN", bulletin, "CLU-WEATHER")  # Headline
    print("Temperature:", temp, "C")  # The reading
    print("Humidity:   ", hum, "%")  # The reading (extra space aligns the columns)
    if temp < 15:  # Temperature verdict
        print("Outlook: COLD day")  # Verdict line
    elif temp < 27:  # Middle zone
        print("Outlook: PLEASANT day")  # Verdict line
    else:  # Hot zone
        print("Outlook: HOT day")  # Verdict line
    print("Station records:", hottest, "C max |", coldest, "C min")  # The history
    time.sleep(10)  # Next broadcast in 10 seconds

Serial Monitor: 

=== CLU-WEATHER CHANNEL === 

BULLETIN 1 CLU-WEATHER 

Temperature: 24 C 

Humidity: 48 % 

Outlook: PLEASANT day 

Station records: 24 C max | 24 C min 

BULLETIN 2 CLU-WEATHER 

Temperature: 28 C 

Humidity: 55 %

 Outlook: HOT day 

Station records: 28 C max | 24 C min text 

💡 Note: everything in this broadcast is recycled: the bulletin counter (breaths, Module 1), the records (20.4), the verdict funnels (20.2), the separator style (gardener's report). What's new is only the *presentation discipline*: fixed-width labels (`Humidity: ` with spaces) so the columns align like a real dashboard. Presentation is engineering tooa report nobody enjoys reading is a report nobody checks. Leave your channel running during a break and read the history it wrote for you. \