Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Arduino Lab Supporting Documents

Uploaded by

gladyg345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arduino Lab Supporting Documents

Uploaded by

gladyg345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PIR Sensor Integration

A PIR sensor detects motion by measuring changes in infrared radiation in its environment.
When a warm body, like a human, moves across its field of view, the sensor detects the change
in infrared levels.

The sensor has three pins:

1. VCC: Power supply (usually 5V).


2. GND: Ground.
3. OUT: The output pin that goes HIGH when motion is detected.

Components Needed

● Arduino Uno
● PIR Motion Sensor
● Breadboard and jumper wires
● LED (optional, for visual feedback)

Circuit Diagram

1. Connect the VCC pin of the PIR sensor to the 5V pin on the Arduino.
2. Connect the GND pin of the PIR sensor to a GND pin on the Arduino.
3. Connect the OUT pin of the PIR sensor to a digital pin on the Arduino (e.g., pin 7).

Sample code

const int pirPin = 7; // Pin where the PIR sensor is connected


const int ledPin = 13; // Pin for the LED

void setup() {
Serial.begin(9600); // Start serial communication
pinMode(pirPin, INPUT); // Set the PIR pin as an input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}

void loop() {
int sensorValue = digitalRead(pirPin); // Read the PIR sensor value

if (sensorValue == HIGH) {
Serial.println("Motion detected!"); // Log to serial monitor
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(1000); // Keep LED on for 1 second
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}

delay(100); // Small delay to avoid excessive serial output


}

Explanation of the Code

● Setup Function: Initializes serial communication and sets the modes for the PIR sensor
and LED pins.
● Loop Function: Continuously checks the state of the PIR sensor:
○ If motion is detected (sensor value is HIGH), it prints a message to the serial
monitor and turns on the LED.
○ If no motion is detected, the LED remains off.

Adjustments

● The sensitivity and duration of the motion detection can usually be adjusted with
potentiometers on the PIR sensor module itself.

Ultrasonic sensor integration

The ultrasonic sensor works by sending out a sound pulse (ultrasonic waves) and measuring
the time it takes for the echo to return. The distance can be calculated using the speed of sound
(approximately 343 meters per second at room temperature).

Components Needed

● Arduino Uno
● HC-SR04 Ultrasonic Sensor
● Breadboard and jumper wires

Circuit Diagram

1. Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino.
2. Connect the GND pin of the HC-SR04 to a GND pin on the Arduino.
3. Connect the Trig pin of the HC-SR04 to a digital pin on the Arduino (e.g., pin 9).
4. Connect the Echo pin of the HC-SR04 to another digital pin on the Arduino (e.g., pin 10).
Sample Code

const int trigPin = 9; // Pin where the Trigger pin is connected


const int echoPin = 10; // Pin where the Echo pin is connected
const int ledPin = 13; // Pin for the LED (optional)

void setup() {
Serial.begin(9600); // Start serial communication
pinMode(trigPin, OUTPUT); // Set the Trigger pin as an output
pinMode(echoPin, INPUT); // Set the Echo pin as an input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output (optional)
}

void loop() {
// Clear the Trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Set the Trigger pin high for 10 microseconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the Echo pin


long duration = pulseIn(echoPin, HIGH);

// Calculate the distance in cm


long distance = duration * 0.034 / 2;

// Print the distance to the Serial Monitor


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Optional: Light up the LED if distance is less than a threshold


if (distance < 20) { // Adjust threshold as needed
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}

delay(500); // Wait before the next measurement


}
Explanation of the Code

● Setup Function: Initializes serial communication and sets the mode for the trigger, echo,
and LED pins.
● Loop Function:
○ It sends a trigger pulse (HIGH for 10 microseconds) to start the measurement.
○ It measures the duration of the echo received.
○ The distance is calculated using the formula: Distance (cm)=Duration
(µs)×0.0342\text{Distance (cm)} = \frac{\text{Duration (µs)} \times
0.034}{2}Distance (cm)=2Duration (µs)×0.034​
○ It prints the distance to the Serial Monitor.
○ If the measured distance is below a specified threshold, it turns on the LED.

Adjustments

● You can modify the threshold value for the LED based on your specific application.
● The delay between measurements can be adjusted for faster or slower readings.

You might also like