0% found this document useful (0 votes)
3 views

Arduino

The document outlines the components and wiring needed for an Arduino-based project involving various sensors, including gas, ultrasonic, temperature, and motion sensors, along with a buzzer and LED. It provides detailed code snippets for each sensor's functionality, demonstrating how to read data and respond to sensor inputs. The project aims to create an interactive system that detects gas levels, measures distance, monitors temperature, and detects motion, with appropriate alerts and feedback mechanisms.

Uploaded by

barsharouth786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Arduino

The document outlines the components and wiring needed for an Arduino-based project involving various sensors, including gas, ultrasonic, temperature, and motion sensors, along with a buzzer and LED. It provides detailed code snippets for each sensor's functionality, demonstrating how to read data and respond to sensor inputs. The project aims to create an interactive system that detects gas levels, measures distance, monitors temperature, and detects motion, with appropriate alerts and feedback mechanisms.

Uploaded by

barsharouth786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Components Needed:

1. MQ-2 Gas Sensor


2. HC-SR04 Ultrasonic Sensor
3. Temperature Sensor (LM35 or similar)
4. PIR Motion Sensor
5. Buzzer
6. LED
7. Arduino Board (Uno, Nano, etc.)
8. Jumper Wires

Wiring Overview:

1. MQ-2 Gas Sensor:

○ VCC → 5V
○ GND → GND
○ Analog Output (A0) → A0 on Arduino
2. HC-SR04 Ultrasonic Sensor:

○ VCC → 5V
○ GND → GND
○ Trig Pin (9) → Digital Pin 9
○ Echo Pin (10) → Digital Pin 10
3. Temperature Sensor (LM35):

○ VCC → 5V
○ GND → GND
○ Analog Output (A0) → A1 on Arduino
4. PIR Motion Sensor:

○ VCC → 5V
○ GND → GND
○ OUT Pin → Digital Pin 5
5. Buzzer:

○ VCC → 5V
○ GND → GND
○ Pin (13) → Digital Pin 13 (same pin as LED_BUILTIN)
6. LED:

○ Anode (+) → Digital Pin 13


○ Cathode (-) → GND

1. MQ-2 Gas Sensor Code:

This code reads the values from an MQ-2 gas sensor to detect the
presence of smoke or gas. The MQ-2 sensor sends analog values to an
Arduino, which processes and outputs the sensor's value to the Serial
Monitor. If the value exceeds a threshold (in this case, 200), it
prints a message indicating that smoke has been detected.

● Functionality:
○ The sensor is connected to pin A0.
○ The Arduino reads the analog input to detect gas levels.
○ If the gas level exceeds a preset threshold, it triggers a
"Smoke detected!" message.

cpp
CopyEdit
#define MQ2pin A0
float sensorValue; // variable to store sensor value

void setup() {
Serial.begin(9600); // sets the serial port to 9600
Serial.println("Gas sensor warming up!");
delay(20000); // allow the MQ-2 to warm up
}

void loop() {
sensorValue = analogRead(MQ2pin); // read analog input pin A0
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
if (sensorValue > 200) {
Serial.print(" | Smoke detected!");
}

Serial.println("");
}

2. Ultrasonic Sensor HC-SR04 Code:

This code uses an HC-SR04 ultrasonic sensor to measure the distance


between the sensor and an object in front of it. It sends a pulse to
the sensor, waits for the pulse to return, calculates the time it
took, and then calculates the distance. This distance is then printed
to the Serial Monitor.

● Functionality:
○ The sensor uses two pins, one for triggering a pulse (pin
9) and one for receiving the pulse (pin 10).
○ The Arduino calculates the distance based on the time it
takes for the pulse to travel to the object and return.

cpp
CopyEdit
/* Ultrasonic Sensor HC-SR04 interfacing with Arduino */

const int trigPin = 9;


const int echoPin = 10;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

duration = pulseIn(echoPin, HIGH);


distance = duration * 0.034 / 2;

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

3. Temperature Sensor (LM35) Code:

This code reads data from an LM35 temperature sensor, which outputs an
analog voltage proportional to the temperature. The Arduino reads this
voltage, converts it to a temperature value, and prints it to the
Serial Monitor. The temperature is displayed in Celsius.

● Functionality:
○ The LM35 sensor is connected to analog pin A0.
○ The Arduino converts the voltage reading to temperature
using a scaling factor.

cpp
CopyEdit
float temp;
int tempPin = A0;

void setup() {
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin); // Read analog volt from sensor and
save to variable temp
temp = temp * 0.48828125; // Convert the analog volt to its
temperature equivalent

Serial.print("TEMPERATURE = ");
Serial.print(temp); // Display temperature value
Serial.print(" *C");
Serial.println();

delay(1000); // Update sensor reading each second


}

4. PIR Motion Sensor Code 1:

This code is for interfacing a Passive Infrared (PIR) motion sensor


with Arduino. The sensor detects motion and outputs a signal. The
Arduino reads this signal, and if motion is detected, it turns on an
LED and prints the state to the Serial Monitor.

● Functionality:
○ The PIR sensor is connected to pin 5.
○ When motion is detected (sensor state HIGH), an LED
connected to pin 13 turns ON.
○ The Serial Monitor shows the current state of the sensor
(HIGH or LOW).

cpp
CopyEdit
int ledPin = 13;
int sensorPin = 5;
int state = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}

void loop() {
state = digitalRead(sensorPin);

if (state == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println(state); // Motion detected
} else {
digitalWrite(ledPin, LOW);
Serial.println(state); // No motion
}
}

5. PIR Motion Sensor Code 2:

This version of the PIR motion sensor code adds more functionality by
printing different messages when motion is detected or stopped. It
also adds a small delay after detecting motion to prevent rapid
switching.

● Functionality:
○ It prints "Motion Detected" when motion is sensed and
"Motion Stopped" when the motion stops.
○ It also turns an LED on when motion is detected.

cpp
CopyEdit
int ledPin = 13;
int sensorPin = 5;
int state = LOW;
int val = 0;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}

void loop() {
val = digitalRead(sensorPin);

if (val == HIGH) {
digitalWrite(ledPin, HIGH);
delay(100); // Short delay to stabilize
if (state == LOW) {
Serial.println("Motion Detected");
state = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
delay(200); // Short delay
if (state == HIGH) {
Serial.println("Motion Stopped");
state = LOW;
}
}
}

6. PIR Motion Sensor Code 3:

This is a simpler version of the PIR motion sensor code that just
turns the LED on when motion is detected and off when no motion is
detected.

● Functionality:
○ It turns on the LED when motion is detected and off when no
motion is detected.
○ It prints "Motion Stopped" to the Serial Monitor when the
sensor goes LOW.

cpp
CopyEdit
int ledPin = 13;
int sensorPin = 5;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}

void loop() {
if (digitalRead(sensorPin) == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED ON
delay(1000); // Wait for a second
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
Serial.println("Motion Stopped");
}
}

7. Ultrasonic Sensor with Buzzer:

This code uses an ultrasonic sensor (HC-SR04) and a buzzer. If the


distance between the sensor and an object is less than a threshold (15
cm), it triggers the buzzer to sound, and the built-in LED is turned
on. If the object moves away, the buzzer turns off and the LED turns
off.

● Functionality:
○ The ultrasonic sensor measures the distance and triggers an
alarm (buzzer) if the object is too close.
○ The distance is printed to the Serial Monitor.

cpp
CopyEdit
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 13;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

duration = pulseIn(echoPin, HIGH);


distance = duration * 0.034 / 2;

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

if (distance < 15) {


tone(buzzerPin, 1000); // Activate buzzer
digitalWrite(LED_BUILTIN, HIGH); // Turn on built-in LED
} else {
noTone(buzzerPin); // Deactivate buzzer
digitalWrite(LED_BUILTIN, LOW); // Turn off built-in LED
}
}

8. Ultrasonic Sensor with LED and Buzzer (Extended):


This is an extended version of the previous ultrasonic sensor code,
where the buzzer and LED are triggered if the distance is less than or
equal to 30 cm. It also includes a delay between readings and prints
the distance to the Serial Monitor.

● Functionality:
○ The buzzer and LED are activated when the distance is less
than or equal to 30 cm.
○ It prints the distance to the Serial Monitor every 5
seconds.

cpp
CopyEdit
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 13;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

duration = pulseIn(echoPin, HIGH);


distance = duration * 0.034 / 2;

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

if (distance <= 399) {


digitalWrite(LED_BUILTIN, HIGH);
delay(5000);
Serial.print(distance);
Serial.println("");
} else {
digitalWrite(LED_BUILTIN, LOW);
delay(5000);
Serial.print(distance);
Serial.println("");
}

delay(10);
}

You might also like