Arduino
Arduino
Wiring Overview:
○ 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:
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("");
}
● 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 */
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);
Serial.print("Distance: ");
Serial.println(distance);
}
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();
● 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
}
}
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;
}
}
}
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");
}
}
● 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);
Serial.print("Distance: ");
Serial.println(distance);
● 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);
Serial.print("Distance: ");
Serial.println(distance);
delay(10);
}