This lab focuses on function organization and sensor data analysis by creating an environment monitoring system. You will learn to use functions effectively to read DHT22 sensor data, perform mathematical calculations, and generate statistical summaries of environmental conditions.
- Practice writing and calling functions with parameters and return values
- Learn to integrate sensor hardware (DHT22) with MicroPython
- Apply mathematical calculations for data analysis and comfort assessment
- Use conditional logic to classify environmental conditions
- Calculate and display statistical summaries (min, max, average)
- Practice error handling for hardware operations
- Organize code using modular function design
- Raspberry Pi Pico 2 W (or regular Pico 2)
- DHT22/AM2302 Digital Temperature Humidity Sensor Module
- Half-size breadboard (30 rows) - optional
- 3 jumper wires (female-to-male recommended)
Your environment monitoring system includes these 9 required functions:
-
Sensor Reading Function
- Reads temperature and humidity from DHT22
- Returns tuple:
(temperature_celsius, humidity_percent) - Handles sensor errors gracefully
-
Temperature Conversion Function
- Converts Celsius to Fahrenheit
- Formula:
F = (C × 9/5) + 32 - Handles None input
-
Comfort Assessment Function
- Calculates comfort level based on temperature and humidity
- Uses mathematical formulas to assess environmental comfort
- Returns comfort assessment string
-
Environmental Classification Function
- Classifies temperature and humidity levels
- Assigns comfort scores and categories
- Returns comprehensive environmental assessment
-
Individual Reading Display Function
- Displays individual sensor reading with analysis
- Shows temperature, humidity, comfort level, and environmental categories
- Handles missing data gracefully
-
Statistics Calculation Function
- Calculates min, max, and average from all readings
- Processes lists of temperature and humidity data
- Returns statistical summary dictionary
-
Summary Display Function
- Displays comprehensive statistics for all readings
- Shows temperature and humidity ranges and averages
- Provides formatted summary report
-
User Interaction Function
- Simple user interaction for pacing readings
-
main()- Coordinates all functions
- Manages program flow and user experience
Your DHT22 sensor module has 3 pins labeled:
- + (VCC/Power)
- OUT (Data)
- - (GND/Ground)
Connect directly from DHT22 to Pico 2 W (no breadboard needed):
- Female-to-Male jumper wires work best:
- DHT22 + pin → Pico 2 W 3V3 (Pin 36)
- DHT22 OUT pin → Pico 2 W GPIO 2 (Pin 4)
- DHT22 - pin → Pico 2 W GND (Pin 38)
If you want to use the breadboard as a connection point:
- Insert Pico 2 W into breadboard (spans the center gap)
- Use male-to-male jumper wires from Pico pins to breadboard rows
- Use female-to-male jumper wires from DHT22 to same breadboard rows:
- DHT22 + → breadboard row connected to Pico 3V3
- DHT22 OUT → breadboard row connected to Pico GPIO 2
- DHT22 - → breadboard row connected to Pico GND
Raspberry Pi Pico 2 W Pinout:
Pin 36 = 3V3 (3.3V Power)
Pin 4 = GPIO 2 (Digital I/O)
Pin 38 = GND (Ground)
-
Install Required Libraries (if not already available):
# DHT library is included with MicroPython on Pico import dht import machine import time
-
Test Sensor Connection:
import machine import dht dht_pin = machine.Pin(2) sensor = dht.DHT22(dht_pin) sensor.measure() print(f"Temperature: {sensor.temperature()}°C") print(f"Humidity: {sensor.humidity()}%")
- Reads temperature and humidity from DHT22 sensor
- Calculates comfort level based on environmental conditions
- Classifies environmental conditions
- Provides comfort level assessments
- Tracks multiple readings over time
- Calculates minimum, maximum, and average values
- Displays comprehensive statistical summaries
- Uses
min()andmax()functions for data analysis
- 8 well-structured functions for clear code organization
- 1-10 readings for flexible data collection
- Mathematical calculations for comfort assessment and statistics
- Classification logic for environmental assessment
Your program should handle:
- Sensor Communication Errors: Display error message, continue program
- Invalid User Input: Constrain values using
min()andmax() - Missing Data: Handle None values gracefully in all functions
- Mathematical Edge Cases: Handle division by zero and invalid inputs
- Test sensor wiring first - DHT22 is sensitive to connections
- Check function signatures - ensure parameters match requirements
- Test functions individually before running full program
- Handle edge cases - test with extreme temperature/humidity values
- Use print statements to trace function execution and data flow
- Sensor Read Errors: Usually wiring or timing issues
- Function Parameter Errors: Check argument order and types
- Statistical Calculation Errors: Handle empty lists and None values
- Import Errors: Ensure using MicroPython on Pico
Ensure your submission includes:
- All 8 required functions implemented correctly
- Proper DHT22 sensor integration
- Comfort assessment calculation using mathematical formula
- Environmental classification with conditional logic
- Statistical analysis with min, max, and average calculations
- Error handling for sensor operations and data validation
- Input validation using
min()andmax() - Mathematical calculations (temperature conversion, comfort assessment, statistics)
Submit to GitHub frequently! Use this workflow:
git add src/main.py writing/reflection.md
git commit -m "Complete environment monitor with functions and sensor data"
git pushVerify your submission online and ensure GatorGrade passes automated testing.
- Ask TLs or instructor for help with specific questions or hardware issues
- Work with classmates on understanding concepts (but write your own code)
- Use the lab time to understand function concepts and sensor integration
- Post questions in Discord
- Attend TL office hours and/or instructor office hours to seek help outside of class
- Review the slides for concept reinforcement
- Python Documentation
- MicroPython Documentation
- Course slides and examples from class activities
- Automated GatorGrade checks: 2.0 points (based on function implementation and sensor integration)
- Hardware execution verification: 1.0 point (program runs correctly on Pico with DHT22 circuit)
- Descriptive variable names and clean organization (0.6 pts)
- Appropriate comments explaining function logic (0.4 pts)
- Thoughtful reflection on function concepts and sensor data analysis
GatorGrade checks verify: function definitions, sensor integration, mathematical calculations, and code organization
- Grades will be released in Canvas after manual code review is complete
- Detailed feedback will be provided as a GitHub issue in your Lab 7 repository
- Check your repository for personalized feedback on code quality and suggestions for improvement
Example output from your environment monitoring system:
Lab 7: Environment Monitor with Sensor Data
==================================================
Enter your location name: My Room
How many readings to take (1-10)? 3
Monitoring environment at 'My Room'
Taking 3 readings...
--- Reading #1 ---
Temperature: 22.5°C (72.5°F)
Humidity: 45.0%
Comfort Level: Perfect Comfort Zone
Temperature Category: Comfortable
Humidity Category: Comfortable
Press Enter to take another reading...
--- Reading #2 ---
Temperature: 23.1°C (73.6°F)
Humidity: 48.2%
Comfort Level: Perfect Comfort Zone
Temperature Category: Comfortable
Humidity Category: Comfortable
Press Enter to take another reading...
--- Reading #3 ---
Temperature: 22.8°C (73.0°F)
Humidity: 46.5%
Comfort Level: Perfect Comfort Zone
Temperature Category: Comfortable
Humidity Category: Comfortable
==================================================
SUMMARY FOR 'MY ROOM'
==================================================
Total Readings: 3
TEMPERATURE:
Minimum: 22.5°C (72.5°F)
Maximum: 23.1°C (73.6°F)
Average: 22.8°C (73.0°F)
HUMIDITY:
Minimum: 45.0%
Maximum: 48.2%
Average: 46.6%
Monitoring complete for My Room!
You've learned to use functions for sensor data analysis!
Your DHT22 sensor will provide real temperature and humidity readings from your environment.