Sensor Integration and Real-Time Data Monitoring with
Arduino
🧭 Overview:
This week is a deep dive into sensor integration, with a strong focus on collecting, interpreting,
and visualizing real-world data using serial communication tools. Students will not only learn
how to interface with common environmental sensors like the DHT11 and DHT22, but also how
to utilize the Serial Monitor and Serial Plotter to gain real-time insights into sensor behavior.
These skills are foundational for any data-driven Arduino application—ranging from smart
weather stations to home automation systems.
This session also introduces third-party libraries—the powerful, pre-written code modules
used in nearly every Arduino project to simplify hardware control and data manipulation.
📚 Topics Covered in Detail:
🔍 1. What Are Sensors and How Do They Work?
Sensors are electronic devices that detect changes in environmental conditions (like temperature,
light, sound, pressure, and more) and convert these physical quantities into electrical signals
readable by microcontrollers. In the Arduino ecosystem, sensors typically output either analog
(varying voltage) or digital (on/off or data stream) signals.
Digital sensors (like DHT11) communicate using a digital data stream.
Analog sensors (like LDRs or thermistors) change voltage based on input.
The role of sensors is crucial—they serve as the eyes, ears, and skin of a smart system.
Understanding how to read and interpret their signals forms the basis of interactive and
autonomous devices.
2. Working with the DHT11 and DHT22
The DHT11 and DHT22 are two widely-used temperature and humidity sensors:
DHT11
o Range: 0–50°C, 20–80% RH
oSampling rate: 1Hz (1 reading per second)
oAccuracy: ±2°C and ±5% RH
DHT22
o Higher precision, broader range
o Range: -40–80°C, 0–100% RH
o Accuracy: ±0.5°C and ±2–5% RH
These sensors use a single digital pin and a proprietary protocol (one-wire communication),
which requires specific libraries to decode.
📚 3. Introduction to Arduino Libraries
Libraries in Arduino are collections of pre-written code that simplify tasks such as interfacing
with sensors, displays, motors, and more. Instead of writing hundreds of lines of complex low-
level code to read from a DHT sensor, we use a library that abstracts these complexities.
Students will learn:
How to install third-party libraries using the Arduino IDE Library Manager
How to include libraries using #include <LibraryName.h>
How to explore example sketches provided by the library
How to read documentation and recognize different functions within a library
🧰 4. Installing and Using the DHT Sensor Library
To read data from a DHT sensor, we use the DHT library by Adafruit, a highly reliable and
well-documented source.
Steps include:
Open Arduino IDE > Sketch > Include Library > Manage Libraries
Search for “DHT sensor library by Adafruit” and install
Also install Adafruit Unified Sensor library if prompted
Include library in your sketch:
#include <DHT.h>
Basic code structure:
cpp
CopyEdit
#include <DHT.h>
#define DHTPIN 2 // Pin connected to DHT sensor
#define DHTTYPE DHT11 // or DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temp: ");
Serial.print(temp);
Serial.print(" *C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(5000); // 5 seconds
}
🧪 5. Using the Serial Monitor and Serial Plotter
Serial communication allows your Arduino to send text data to your computer for display,
monitoring, or debugging.
Serial Monitor: Text output window to show real-time values
Serial Plotter: Graphical line chart showing data trends
Students will learn how to:
Use Serial.begin() to initiate serial communication
Send data with Serial.print() and Serial.println()
Plot values using Serial Plotter (only works if numbers are output in a simple format)
Debugging tips:
Use Serial.println() inside if statements to verify conditions
Print sensor status when readings fail (isnan(temp), etc.)
Hands-On Activities:
🔬 Activity 1: Sensor Hook-Up & First Read
Wire a DHT11 or DHT22 sensor to digital pin 2
Load example sketch from DHT library
Confirm temperature and humidity values display in Serial Monitor
Practice changing the delay to alter refresh rate
🧾 Activity 2: Data Logging Every 5 Seconds
Modify the code to log readings at 5-second intervals
Create headers in Serial Monitor (e.g., “Temp (°C) | Humidity (%)”)
Emphasize correct formatting for plot compatibility
📈 Activity 3: Visualizing Data with Serial Plotter
Use Serial Plotter to view temperature and humidity trends over 1–2 minutes
Compare real-time changes (e.g., touch the sensor to increase temperature)
Activity 4: Temperature-Based Fan (Concept Model)
Add a conditional block:
cpp
CopyEdit
if (temp > 30) {
digitalWrite(fanPin, HIGH);
} else {
digitalWrite(fanPin, LOW);
}
Use an LED to simulate the fan if no DC fan is available
Discuss relay modules or transistor switching if using an actual motor
🎯 Learning Goals:
By the end of this week, students should be able to:
1. Explain how sensors collect environmental data and how that data is read by
Arduino
2. Work with digital sensors using third-party libraries
3. Install and use Arduino libraries via the IDE
4. Use the Serial Monitor and Serial Plotter to debug and visualize sensor data
5. Create conditional logic that responds to environmental changes
6. Understand timing, polling intervals, and the importance of sampling rate
7. Develop functional sketches that integrate data collection with responsive behavior
8. Practice reading documentation and learning from example code
📝 Homework / Practice:
Assignment 1: “Comfort Monitor” Program
Objective: Create a sketch that reads from the DHT11 and prints a message based on comfort
ranges.
Example logic:
cpp
CopyEdit
if (temp >= 25 && temp <= 30 && humidity >= 40 && humidity <= 60) {
Serial.println("Comfortable Environment 😌");
} else if (temp < 25) {
Serial.println("Too Cold ❄️");
} else if (temp > 30) {
Serial.println("Too Hot 🔥");
}
Add icons/emojis using ASCII if desired
Encourage creative naming or labeling of output
Assignment 2: Serial Plotter Screenshot
Run your DHT sketch for at least 60 seconds
Use the Serial Plotter to graph temperature and humidity
Take a screenshot and submit it as part of your assignment
Add a 2–3 sentence explanation:
o “I touched the sensor at 30s which caused a temperature spike”
o “Humidity dropped slightly over time due to dry airflow”
🧭 Extension Ideas (Optional for Advanced Students):
Add an LCD or OLED screen to display temperature/humidity locally
Send data to a cloud service (if ESP8266/ESP32 is available)
Control a fan or relay module to physically change the environment
Log data to an SD card using an SD module