Sensor Integration and Real
Sensor Integration and Real
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.
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.
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.
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.
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.
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
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>
cpp
CopyEdit
#include <DHT.h>
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
}
Serial communication allows your Arduino to send text data to your computer for display,
monitoring, or debugging.
Debugging tips:
Hands-On Activities:
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)
cpp
CopyEdit
if (temp > 30) {
digitalWrite(fanPin, HIGH);
} else {
digitalWrite(fanPin, LOW);
}
🎯 Learning Goals:
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:
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 🔥");
}