Theory
8. Arduino Projects
5.1 Weather Monitoring Station Using Temperature and Humidity Sensor
💧 Humidity Sensor (DHT22)
- The DHT-22 is a digital-output sensor for measuring relative humidity and temperature.
- Utilizes a capacitive humidity sensor and a thermistor.
- Outputs a digital signal via the data pin.
- This section guides on integrating this sensor with an Arduino UNO to display room temperature and humidity on the serial monitor.
Code:
// Include the DHT library to interact with the humidity and temperature sensor
#include “dht.h”
// Define the pin the DHT sensor is connected to (Analog Input A1)
#define dht_apin A1
// Include the Wire library for I2C devices (not used in this example, but typically required for sensor communication)
#include <Wire.h>
// Initialize the DHT library with the sensor type
dht DHT;
void setup(){
// Begin serial communication at 9600 baud rate for output
Serial.begin(9600);
// Short delay to ensure that the serial communication is established
delay(500);
// Print a message to the serial monitor indicating the start of the program
Serial.println(“DHT11 Humidity & temperature Sensor\n\n”);
// Delay to ensure the sensor is ready for data reading
delay(1000);
}
void loop(){
// Read humidity and temperature data from the sensor
DHT.read11(dht_apin);
// Print the current humidity reading to the serial monitor
Serial.print(“Current humidity = “);
Serial.print(DHT.humidity);
Serial.print(“% “);
// Print the current temperature reading to the serial monitor
Serial.print(“temperature = “);
Serial.print(DHT.temperature);
Serial.println(“C “);
// Wait 1 second before reading from the sensor again to prevent data flooding
delay(1000);
}
📝 Arduino Code Overview
Initial Setup
#include “dht.h”
#define dht_apin A1 (Analog Pin connection)
#include <Wire.h>
Initialization:
Begin serial communication at 9600 baud rates.
Initial delays to stabilize system before sensor readout.
Setup message: “DHT11 Humidity & temperature Sensor\n\n”
Main Loop
Sensor reading with DHT.read11(dht_apin).
Displays current humidity (DHT.humidity) and temperature (DHT.temperature) in Celsius to the serial monitor.
Includes a 1-second delay before each sensor readout for data stability.
🔄 Operational Details
The program is crafted for both temperature and humidity data acquisition from a DHT11 sensor.
Unique points include:
Connection to an analog pin A1.
Sequential process: Initialize ➡ Print Setup Message ➡ Sensor Data Acquisition ➡ Display on Serial Monitor.
Utilizes delay for accurate and stable readings.
A practical application for environmental monitoring, potentially useful for climate control or environmental change detection.
Fire Alarm
🚨 Overview:
- An electronic device known as a fire alarm detects fire and alerts people to evacuate.
- 🔥 A flame sensor detects fire by sensing infrared radiation from flames.
- 🧠 An Arduino, a programmable small computer, can control electronic devices.
- 🔄 By connecting a flame sensor to an Arduino, the system can detect fire and trigger an alarm.
🚒 Operation:
- If the flame sensor detects fire, it sends a signal to the Arduino.
- 🎶 The Arduino then activates a buzzer, alerting people to the danger.
- This setup helps in extinguishing the fire early, preventing harm and damage.
Arduino Code
//define variables to store the pin numbers for each component
int FLAME_SENSOR = 2; //digital pin 2
int BUZZER = 11; //digital pin 11
void setup() {
//set the pinMode for each component
pinMode(FLAME_SENSOR, INPUT); //flame sensor as input
pinMode(BUZZER, OUTPUT); //buzzer as output
}
void loop() {
//if the flame sensor detects a flame, turn on the buzzer and water pump
if(digitalRead(FLAME_SENSOR)) {
digitalWrite(BUZZER, HIGH); //turn on the buzzer
}
//if the flame sensor does not detect a flame, turn off the buzzer and water pump
else {
digitalWrite(BUZZER, LOW); //turn off the buzzer
}
}
👨💻 Arduino Code Overview:
📌 Variable Definitions:
- int FLAME_SENSOR = 2; for the flame sensor connected to digital pin 2.
- int BUZZER = 11; for the buzzer connected to digital pin 11.
🛠 Setup Function:
- pin Mode (FLAME_SENSOR, INPUT); sets the flame sensor as input.
- pin Mode (BUZZER, OUTPUT); sets the buzzer as output.
🔁 Loop Function:
- If flame is detected (digital Read (FLAME_SENSOR) is TRUE), digital Write (BUZZER, HIGH); turns the buzzer on.
- If no flame is detected, digital Write (BUZZER, LOW); turns the buzzer off.
📖 Explanation:
- The code specifies pin connections for components and sets their modes (input or output).
- It continuously checks for flames, turning the buzzer on or off accordingly.
- This ensures the buzzer is activated upon fire detection, alerting for action
Flex sensor
🔍 Flex Sensor Overview
- 🌟 A flex sensor is a type of sensor that detects material bending.
- 📌 Typically made from flexible materials like plastic.
- 🔄 Can be bent to a certain degree, altering its resistance.
- 🛠️ Measured by Arduino to detect changes in bending.
🏗️ Applications
- 🤖 Robotics: Control movements of arms or hands.
- 🩺 Medical Devices: Monitor human body or limb movements.
- 🎮 Gaming: Control character movements with a flex sensor controller.
- 🎵 Musical Instruments: Adjust instrument sounds.
Arduino code to glow the LED in proportion to the bending of the flex sensor:
Arduino Code
int FLEX_PIN = A0; // connect the flex sensor to analog pin A0
int LED_PIN = 11; // connect the LED to digital pin 11
int flexValue = 0; // variable to store the value of the flex sensor
void setup() {
pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
Serial.begin(9600); // initialize the serial communication
}
void loop() {
flexValue = analogRead(FLEX_PIN); // read the value from the flex sensor
Serial.print(“Flex Value: “); // print the value to the serial monitor
Serial.println(flexValue);
flexValue = map(flexValue, 0, 1023, 0, 255); // map the value to a range of 0-255
analogWrite(LED_PIN, flexValue); // set the brightness of the LED based on the mapped value
delay(100); // wait for 100 milliseconds
}
💻 Arduino Implementation
📝 Code Setup:
- int FLEX_PIN = A0; Connect flex sensor to analog pin A0.
- int LED_PIN = 11; Connect LED to digital pin 11.
- int flexValue = 0; Store flex sensor value.
🛠️ Function Setup:
- setup() Method:
- pinMode(LED_PIN, OUTPUT); Set LED pin as output.
- Serial.begin(9600); Start serial communication.
loop () Method:
- Read flex sensor: flexValue = analogRead(FLEX_PIN);
- Print value: Serial.print(“Flex Value: “); Serial.println(flexValue);
- Map sensor value: flexValue = map(flexValue, 0, 1023, 0, 255);
- Adjust LED brightness: analogWrite(LED_PIN, flexValue);
- Wait 100ms: delay(100);
🔧 Code Explanation:
- The flex sensor’s bending is read by analog pin A0.
- The LED’s brightness changes with the sensor’s bending.
- analogRead() reads the sensor, map() scales the value, analogWrite() sets LED brightness.
- Serial communication displays the sensor value on the monitor.
- The process repeats every 100 milliseconds.
Force sensor
🌐 Description:
- 🤖 A force sensor is an electronic device designed to measure force.
- 🔌 It converts force into an electrical signal for measurement and various applications.
📌 Applications:
- 🦾 In robotics, it gauges the force exerted by robot arms on objects.
- 🏥 In medical devices, it monitors force during surgery or rehabilitation exercises.
Here’s an example code that explains how to use a force sensor with Arduino:
Arduino Code
int forceSensorPin = A0; // connect force sensor to analog pin A0
int ledPin = 11; // connect LED to digital pin 11
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(ledPin, OUTPUT); // set LED pin as output
}
void loop() {
int sensorValue = analogRead(forceSensorPin); // read force sensor value
Serial.print(“Force sensor value: “);
Serial.println(sensorValue); // print sensor value to serial monitor
int brightness = map(sensorValue, 0, 1023, 0, 255); // map sensor value to LED brightness
analogWrite(ledPin, brightness); // set LED brightness
}
Arduino Code Structure:
📂 Variables:
- int forceSensorPin = A0; ➡️ Connect force sensor to analog pin A0.
- int ledPin = 11; ➡️ Connect LED to digital pin 11.
🛠 Setup Function:
- Serial.begin(9600); ➡️ Initialize serial communication.
- pinMode(ledPin, OUTPUT); ➡️ Set LED pin as output.
🔁 Loop Function:
- Read sensor value: int sensorValue = analogRead(forceSensorPin);
- Print to serial: Serial.print(“Force sensor value: “); Serial.println(sensorValue);
- Map sensor value to brightness: int brightness = map(sensorValue, 0, 1023, 0, 255);
- Set LED brightness: analogWrite(ledPin, brightness);
📐 Tilt Sensor
- 🌐 A tilt sensor detects the orientation or tilt of an object.
- 🛠 Consists of a small metal ball or a mercury switch inside a metal canister.
- 🔁 When tilted, the ball/switch moves, altering an electrical connection to signal a change in orientation.
Here is an Arduino code with comments that explains how to use a tilt sensor to light up an LED when the sensor is tilted:
int tiltPin = 2; // the digital pin connected to the tilt sensor
int ledPin = 11; // the digital pin connected to the LED
void setup() {
pinMode(tiltPin, INPUT); // set the tilt sensor pin as input
pinMode(ledPin, OUTPUT); // set the LED pin as output
Serial.begin(9600); // initialize serial communication
}
void loop() {
int tiltValue = digitalRead(tiltPin); // read the tilt sensor value
if (tiltValue == HIGH) { // if the tilt sensor is tilted
digitalWrite(ledPin, HIGH); // turn on the LED
Serial.println(“Tilted”); // print “Tilted” on the serial monitor
}
else { // if the tilt sensor is not tilted
digitalWrite(ledPin, LOW); // turn off the LED
Serial.println(“Not tilted”); // print “Not tilted” on the serial monitor
}
delay(100); // wait for 100 milliseconds before reading again
}
🔌 Arduino Code for Tilt Sensor
📝 Setup:
- 🔵 int tiltPin = 2; ➡️ Digital pin for the tilt sensor.
- 🔴 int ledPin = 11; ➡️ Digital pin for the LED.
- 🔄 void setup():
- 📥 Sets tilt sensor pin as input.
- 💡 Sets LED pin as output.
- 📡 Initializes serial communication.
♾ Loop:
- void loop ():
- 📊 Reads tilt sensor value.
- 🚦 If tilted:
- 💡 Turns on the LED.
- 🖨 Prints “Tilted” on the serial monitor.
- 🚦 If not tilted:
- 💡 Turns off the LED.
- 🖨 Prints “Not tilted” on the serial monitor.
- ⏲ Waits 100 milliseconds before the next read.
🔧 Functionality:
- 🔄 When tilted, digitalRead() from tiltPin is HIGH ➡️ LED turns on.
- 🔄 When not tilted, digitalRead() is LOW ➡️ LED turns off.
- 📋 Uses Serial.println() to indicate tilt status on the serial monitor.
Smart streetlight
🚦 Smart Street Light
🛠 Smart Street Light Project using Arduino, LDR, and IR Sensors
- 🌞 Daytime Detection: Utilizes LDR (Light Dependent Resistor) to detect sunlight, automatically turning off LED street lights.
- 🌜 Nighttime Activation: Employs an IR (Infrared Sensor) to detect darkness, subsequently turning on LED street lights.
int OUTPIN = 13;
int INPIN = 2;
//LiquidCrystal lcd( 8,9,4,5,6,7 ); //interfacing pins
void setup() {
//put your setup code here, to run once:
pinMode(INPIN, INPUT);
pinMode(OUTPIN, OUTPUT);
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println(“LDR Sensor DEMO”); // print some text in Serial Monitor
}
void loop() {
//put your main code here, to run repeatedly:
if(digitalRead(INPIN)) {
digitalWrite(OUTPIN, false);
Serial.println(“1”);
}
else {
digitalWrite(OUTPIN, true);
Serial.println(“0”);
}
}
🔍 Applications of LDR
- 🌌 Astronomy: Used in infrared astronomy.
- 🚨 Safety Alarms: Integral to light failure alarm circuits and light meters.
- 🚒 Smoke Detection: A key component in smoke detectors.
- 📺 Television: Enhances automatic contrast and brightness control in TVs.
- 🔄 Relays: Found in photosensitive relays.
- 🔑 Security: Utilized in optical coding.
🚀 Smart Blind Stick
🔍 Project Overview:
🎯 Main Goal:
- Detect obstacles in front of the electronic stick and trigger an alarm.
🚶 User Assistance:
- Helps blind individuals during walks by providing an alarm if any hurdle is detected within the set range.
📏 Distance Measurement:
- Utilizes an ultrasonic sensor to determine the distance of obstacles.
🔊 Alarm Intensity:
- Slow beep for far obstacles (e.g., 100 cm), faster beep as obstacles get closer, aiding in understanding the obstacle’s distance.
- The main aim of this project is to detect the obstacle in front of the electronic stick and giving the alarm.
#define trigPin 2 //attach pin D2 Arduino to pin Trig of HC-SR04
#define echoPin 3 // attach pin D3 Arduino to pin Echo of HC-SR04
#define buzzerPin 11
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(buzzerPin, OUTPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println(“Ultrasonic Sensor HC-SR04 Test”); // print some text in Serial Monitor
Serial.println(“with Arduino UNO R3”);
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
if (distance < 30)
{
digitalWrite(buzzerPin, HIGH);
delay(30);
digitalWrite(buzzerPin, LOW);
delay(30);
}
if (distance > 30 && distance < 50)
{
digitalWrite(buzzerPin, HIGH);
delay(60);
digitalWrite(buzzerPin, LOW);
delay(60);
}
if (distance > 50 && distance < 70)
{
digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(100);
}
if (distance > 70 && distance < 100)
{
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(buzzerPin, LOW);
delay(200) ;
}
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
delay(50);
}
💻 Code Setup:
📌 Pin Configuration:
- #define trigPin 2 : D2 on Arduino to Trig on HC-SR04.
- #define echoPin 3 : D3 on Arduino to Echo on HC-SR04.
- #define buzzerPin 11
📊 Variables:
- long duration; : Duration of sound wave travel.
- int distance; : Distance measurement.
🔄 Initialization:
🔧 Setup Function:
- Sets trigPin as OUTPUT.
- Sets echoPin as INPUT.
- Sets buzzerPin as OUTPUT.
- Starts Serial Communication at 9600 baudrate.
- Prints test message on Serial Monitor.
🔄 Main Loop:
📡 Triggering Ultrasonic Sensor:
- Clears trigPin, sets it HIGH for 10 microseconds, then LOW.
📏 Distance Calculation:
- Reads echoPin, calculates distance using sound wave speed.
🔊 Distance-Based Buzzer Alert:
- <30 cm: Buzzer HIGH for 30ms intervals.
- 30-50 cm: Buzzer HIGH for 60ms intervals.
- 50-70 cm: Buzzer HIGH for 100ms intervals.
- 70-100 cm: Buzzer HIGH for 200ms intervals.
🖥️ Serial Monitor Display:
- Prints distance in cm.
⏲️ Loop Delay: 50ms.