DoorDars-SMC: Transforming Education
Transforming Lab Experiments with DHT Sensors into AI-Powered Predictions
DHT Sensors data processing
Lutfullah Kakakhel
11/24/20242 min read


Introduction
Temperature and humidity are among the first physical quantities students learn to measure in school laboratories. Using DHT sensors, these experiments are often limited to recording environmental data and observing changes under controlled conditions—such as blowing over the sensor. While these are excellent starting points, what if we could go beyond this? What if students could use the same data to train a machine learning model to predict temperature? This blog post will guide you through an innovative project: collecting temperature and humidity data using a Raspberry Pi Pico and SD card, processing it with Python, and applying machine learning to predict temperature based on humidity.
Project Overview
The project has three main stages:
Data Collection: Using a DHT22 sensor connected to a Raspberry Pi Pico, we log temperature and humidity data overnight onto an SD card.
Data Processing: Using Python on a computer, we clean and visualize the collected data.
Machine Learning: Using Python libraries such as pandas, scikit-learn, and matplotlib, we train a machine learning model to predict temperature based on humidity.
Stage 1: Collecting Data with the Raspberry Pi Pico
Components:
Raspberry Pi Pico
DHT22 Sensor
MicroSD Card Module and SD Card
Code Explanation:
We use MicroPython to record data in CSV format. Here's the breakdown:
Initializing the SD Card: The SD card is connected via SPI. MicroPython's uos and sdcard modules help mount the SD card.
Reading Data from the DHT Sensor: The DHT22 sensor is connected to a GPIO pin. It provides temperature and humidity readings.
Writing to the SD Card: Each reading is appended to a CSV file along with a Unix timestamp (seconds since epoch).
Sample Code:
Stage 2: Processing the Data
Once the data is logged, the SD card is connected to a computer for processing using Python. Students can visualize how temperature and humidity vary over time.
Steps:
Loading the Data: Use pandas to read the CSV file.
Visualizing the Data: Use matplotlib to create plots of temperature and humidity over time.
Code Example:
Stage 3: Machine
Learning
Here, students use the collected data to predict temperature based on humidity. This transforms a simple lab exercise into an AI application.
Steps:
Prepare Data: Split the data into training and testing sets.
Train the Model: Use scikit-learn's LinearRegression to train the model.
Evaluate: Test the model on unseen
data and visualize predictions.
Code Example:
Beyond the Basics: Deployment
The trained model can be saved and deployed on microcontrollers like the Raspberry Pi Pico using frameworks like TensorFlow Lite. This step allows students to explore edge AI, where models make predictions on small devices without needing constant internet connectivity.
Conclusion
This project introduces students to modern AI applications by extending traditional sensor experiments. It shows how:
Data collection with sensors can be enhanced with AI.
Students can move from observing trends to making predictions.
Skills in Python and machine learning can empower them to solve real-world problems.
By leveraging tools like Raspberry Pi Pico and MicroPython, educators can inspire the next generation of data scientists and AI innovators. The journey from collecting DHT data to deploying a machine learning model brings technology and curiosity together in an accessible, engaging way.