0% found this document useful (0 votes)
2 views2 pages

ae_1209_iot_lab_7

The document outlines a program for interfacing an ultrasonic sensor (HC-SR04) with an Arduino to control the blinking of Red, Orange, and Green LEDs based on distance measurements. It includes the program code that sets up the sensor and LEDs, calculates distance, and regulates LED states accordingly. The result demonstrates that the LEDs blink based on the proximity of an object detected by the ultrasonic sensor.

Uploaded by

ansh28405
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)
2 views2 pages

ae_1209_iot_lab_7

The document outlines a program for interfacing an ultrasonic sensor (HC-SR04) with an Arduino to control the blinking of Red, Orange, and Green LEDs based on distance measurements. It includes the program code that sets up the sensor and LEDs, calculates distance, and regulates LED states accordingly. The result demonstrates that the LEDs blink based on the proximity of an object detected by the ultrasonic sensor.

Uploaded by

ansh28405
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/ 2

Ansh Sharma

AE-1209
B.Sc. Computer Science Hons

IOT
GE Electronics (VI Semester)
LAB -7

Program 9 : Interfacing Ultrasonic Sensor (HC-SR04) with Arduino


Regulating blinking of Red, Orange and Green LEDs according to distance

Program Code : -

int trigPin = 2;
int echoPin = 3; // Added echoPin
int LEDR = 5;
int LEDO = 6;
int LEDG = 7;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT); //Corrected from the image.
pinMode(LEDR, OUTPUT);
pinMode(LEDO, OUTPUT);
pinMode(LEDG, OUTPUT);
Serial.begin(9600);
}

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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

if (distance <= 10) {


digitalWrite(LEDR, HIGH);
digitalWrite(LEDO, LOW);
digitalWrite(LEDG, LOW);
} else if (distance <= 20) {
digitalWrite(LEDR, LOW);
digitalWrite(LEDO, HIGH);
digitalWrite(LEDG, LOW);
} else {
digitalWrite(LEDR, LOW);
digitalWrite(LEDO, LOW);
digitalWrite(LEDG, HIGH);
}
delay(100);
}

Result :-

The LED’s blinks based on the distance between the ultrasonic sensor and the object.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy