ae_1209_iot_lab_7
ae_1209_iot_lab_7
AE-1209
B.Sc. Computer Science Hons
IOT
GE Electronics (VI Semester)
LAB -7
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);
Result :-
The LED’s blinks based on the distance between the ultrasonic sensor and the object.