114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
80% Percent Complete
By Kevin McAleer, 3 Minutes
In this lesson, you’ll build a practical application: a temperature alert system. This system will notify you when the temperature exceeds a maximum threshold or falls below a minimum threshold.
To create the alert system, define the acceptable temperature range:
MIN_TEMP = 20 # Minimum acceptable temperature in °C MAX_TEMP = 30 # Maximum acceptable temperature in °C
Update your script to check if the temperature is outside the defined range and print an appropriate alert message:
import machine import time # Initialize the ADC for the temperature sensor sensor_adc = machine.ADC(4) # Define temperature thresholds MIN_TEMP = 20 # Minimum acceptable temperature in °C MAX_TEMP = 30 # Maximum acceptable temperature in °C while True: # Read the raw ADC value raw_value = sensor_adc.read_u16() # Convert the ADC value to voltage voltage = raw_value * 3.3 / 65535 # Convert voltage to temperature temperature = 27 - (voltage - 0.706) / 0.001721 # Check temperature thresholds if temperature < MIN_TEMP: print(f"Alert: Temperature is too low! ({temperature:.2f}°C)") elif temperature > MAX_TEMP: print(f"Alert: Temperature is too high! ({temperature:.2f}°C)") else: print(f"Temperature is normal: {temperature:.2f}°C") # Wait for 1 second before checking again time.sleep(1)
temperature_alert_system.py
When the temperature is normal:
Temperature is normal: 25.84°C
When the temperature is too high:
Alert: Temperature is too high! (31.20°C)
When the temperature is too low:
Alert: Temperature is too low! (19.45°C)
You can enhance the alert system by:
In the next lesson, we’ll explore how to expand this system with more advanced features and ideas.
< Previous Next >