DHT11 Temperature and Humidity

🎮 Temperature and Humidity
Goal
Read the temperature and humidity and display both values on the PC.
Concepts
Digital Sensor · Temperature · Humidity · Loop · Serial Output
import dhtx # Controls the DHT11 sensor
import time # Controls pauses and timing
# Sets up the DHT11 sensor on GPIO26
# GPIO26 corresponds to D2 on the Sensor Shield V5.0
sensor = dhtx.DHT11(26)
while True: # Repeats forever
temperature = sensor.temperature() # Reads the temperature
humidity = sensor.humidity() # Reads the humidity
print('Temperature:', temperature, 'C') # Sends temperature to the PC
print('Humidity:', humidity, '%') # Sends humidity to the PC
time.sleep(2) # Waits before reading again 🎮 Describe the Environment
Goal
Convert the readings into simple temperature and humidity categories.
Concepts
Temperature · Humidity · If/Elif/Else · Classification · Serial Output
import dhtx # Controls the DHT11 sensor
import time # Controls pauses and timing
# Sets up the DHT11 sensor on GPIO26
# GPIO26 corresponds to D2 on the Sensor Shield V5.0
sensor = dhtx.DHT11(26)
while True: # Repeats forever
temperature = sensor.temperature() # Reads the temperature
humidity = sensor.humidity() # Reads the humidity
if temperature < 18: # Checks the lower temperature range
temp_state = 'Cool' # Saves the temperature category
elif temperature < 26: # Checks the middle temperature range
temp_state = 'Comfortable' # Saves the temperature category
else: # Runs for higher temperatures
temp_state = 'Warm' # Saves the temperature category
if humidity < 40: # Checks the lower humidity range
humidity_state = 'Dry' # Saves the humidity category
elif humidity < 70: # Checks the middle humidity range
humidity_state = 'Normal' # Saves the humidity category
else: # Runs for higher humidity
humidity_state = 'Humid' # Saves the humidity category
print('Temperature:', temp_state) # Shows the temperature category
print('Humidity:', humidity_state) # Shows the humidity category
time.sleep(2) # Waits before reading again 🎮 Detect Threshold Crossings
Goal
Detect when the temperature crosses 25°C or the humidity crosses 60%.
Concepts
Temperature · Humidity · Thresholds · Comparison · State Change · Serial Output
import dhtx # Controls the DHT11 sensor
import time # Controls pauses and timing
# Sets up the DHT11 sensor on GPIO26
# GPIO26 corresponds to D2 on the Sensor Shield V5.0
sensor = dhtx.DHT11(26)
last_temperature = sensor.temperature() # Saves the first temperature
last_humidity = sensor.humidity() # Saves the first humidity
while True: # Repeats forever
temperature = sensor.temperature() # Reads the temperature
humidity = sensor.humidity() # Reads the humidity
if temperature >= 25 and last_temperature < 25: # Checks the temperature threshold
print('Temperature crossed 25 C') # Reports the crossing
if temperature < 25 and last_temperature >= 25: # Checks the opposite crossing
print('Temperature below 25 C') # Reports the crossing
if humidity >= 60 and last_humidity < 60: # Checks the humidity threshold
print('Humidity crossed 60%') # Reports the crossing
if humidity < 60 and last_humidity >= 60: # Checks the opposite crossing
print('Humidity below 60%') # Reports the crossing
last_temperature = temperature # Saves the current temperature
last_humidity = humidity # Saves the current humidity
time.sleep(2) # Waits before reading again 🎮 Minimum and Maximum Values
Goal
Track the minimum and maximum temperature and humidity observed while the program is running.
Concepts
Temperature · Humidity · Minimum · Maximum · Comparison · Serial Output
import dhtx # Controls the DHT11 sensor
import time # Controls pauses and timing
# Sets up the DHT11 sensor on GPIO26
# GPIO26 corresponds to D2 on the Sensor Shield V5.0
sensor = dhtx.DHT11(26)
temperature = sensor.temperature() # Reads the first temperature
humidity = sensor.humidity() # Reads the first humidity
min_temp = temperature # Sets the first minimum temperature
max_temp = temperature # Sets the first maximum temperature
min_humidity = humidity # Sets the first minimum humidity
max_humidity = humidity # Sets the first maximum humidity
while True: # Repeats forever
temperature = sensor.temperature() # Reads the temperature
humidity = sensor.humidity() # Reads the humidity
if temperature < min_temp: # Checks for a new minimum
min_temp = temperature # Saves the new minimum
if temperature > max_temp: # Checks for a new maximum
max_temp = temperature # Saves the new maximum
if humidity < min_humidity: # Checks for a new minimum
min_humidity = humidity # Saves the new minimum
if humidity > max_humidity: # Checks for a new maximum
max_humidity = humidity # Saves the new maximum
print('Temperature:', temperature, 'C') # Shows current temperature
print('Minimum temperature:', min_temp, 'C') # Shows minimum temperature
print('Maximum temperature:', max_temp, 'C') # Shows maximum temperature
print('Humidity:', humidity, '%') # Shows current humidity
print('Minimum humidity:', min_humidity, '%') # Shows minimum humidity
print('Maximum humidity:', max_humidity, '%') # Shows maximum humidity
print('---') # Separates each report
time.sleep(2) # Waits before reading again 🎮 Environment Monitor
Goal
Create an environment monitor that periodically displays temperature, humidity, their current categories, and the minimum and maximum values observed.
Concepts
Temperature · Humidity · Classification · Minimum · Maximum · Comparison · Serial Output
import dhtx # Controls the DHT11 sensor
import time # Controls pauses and timing
# Sets up the DHT11 sensor on GPIO26
# GPIO26 corresponds to D2 on the Sensor Shield V5.0
sensor = dhtx.DHT11(26)
temperature = sensor.temperature() # Reads the first temperature
humidity = sensor.humidity() # Reads the first humidity
min_temp = temperature # Sets the first minimum temperature
max_temp = temperature # Sets the first maximum temperature
min_humidity = humidity # Sets the first minimum humidity
max_humidity = humidity # Sets the first maximum humidity
while True: # Repeats forever
temperature = sensor.temperature() # Reads the temperature
humidity = sensor.humidity() # Reads the humidity
if temperature < min_temp: # Checks for a new minimum
min_temp = temperature # Saves the new minimum
if temperature > max_temp: # Checks for a new maximum
max_temp = temperature # Saves the new maximum
if humidity < min_humidity: # Checks for a new minimum
min_humidity = humidity # Saves the new minimum
if humidity > max_humidity: # Checks for a new maximum
max_humidity = humidity # Saves the new maximum
if temperature < 18: # Checks the lower temperature range
temp_state = 'Cool' # Saves the temperature category
elif temperature < 26: # Checks the middle temperature range
temp_state = 'Comfortable' # Saves the temperature category
else: # Runs for higher temperatures
temp_state = 'Warm' # Saves the temperature category
if humidity < 40: # Checks the lower humidity range
humidity_state = 'Dry' # Saves the humidity category
elif humidity < 70: # Checks the middle humidity range
humidity_state = 'Normal' # Saves the humidity category
else: # Runs for higher humidity
humidity_state = 'Humid' # Saves the humidity category
print('Temperature:', temperature, 'C') # Shows current temperature
print('Temperature state:', temp_state) # Shows temperature category
print('Humidity:', humidity, '%') # Shows current humidity
print('Humidity state:', humidity_state) # Shows humidity category
print('Min temperature:', min_temp, 'C') # Shows minimum temperature
print('Max temperature:', max_temp, 'C') # Shows maximum temperature
print('Min humidity:', min_humidity, '%') # Shows minimum humidity
print('Max humidity:', max_humidity, '%') # Shows maximum humidity
print('---') # Separates each report
time.sleep(2) # Waits before reading again 