You are on page 1of 4

INTERNET OF THINGS

Worksheet 4

Student Name: Harsh UID: 22MCA21078


Section Subject Code: 22CAH-751 Branch-Sec/Group: UIC - 22MCA-4/A
Date of Performance: 14th February, 2024 Semester: 4th

Aim/Overview of the practical:


Interface an Arduino Uno with Ultrasonic Sensor to calculate the distance and print on a Serial monitor.
Follow the mentioned cases after the calculation of distance.
• If the distance X > 250 cm, then Green light is in ON condition.
• If the distance is 90 cm < X < 250cm, then yellow light is in ON condition and the green light
remains Off.
• If the distance X< 90 cm, then turn on the buzzer and all the lights will remain OFF.

Hardware Requirements: Arduino * 1, Bulb and Wire(Sets) * 1, Resistor * 1,


BreadBoard * 1, Laptop or Power Board * 1, Buzzer * 1, Ultrasonic Distance Sensor * 1.
Software Requirements: Arduino IDE, TinkerCad.

Circuit Diagram (TinkerCad):


• If the distance X > 250 cm, then Green light is in ON condition.
• If the distance is 90 cm < X < 250cm, then yellow light is in ON condition and the green light remains Off.

• If the distance X< 90 cm, then turn on the buzzer and all the lights will remain OFF.
Coding:
#define trig 3 // defines pins numbers By Marcos
#define echo 2
#define buzzerPin 4 // defines Buzzer & LED Pins numbers By Marcos
#define yellowPin 5 #define greenPin 6
long duration; // defines variables
int distance;
void setup(){
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Input
pinMode(greenPin, OUTPUT); // Sets the greenLightPin as an Output
pinMode(yellowPin, OUTPUT); // Sets the yellowLightPin as an Output
pinMode(buzzerPin, OUTPUT); // Sets the buzzerPin as an Output
Serial.begin(9600); // Starts the serial communication
} void
loop(){
digitalWrite(trig, LOW); // Clears the trigPin
delayMicroseconds(2);
digitalWrite(trig, HIGH); // Sets the trigPin on HIGH state for 10 microseconds
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2; // Calculating the distance
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 250) { // Cases based on distance
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPin,
LOW); digitalWrite(buzzerPin,
LOW);
} else if (distance > 90 && distance <= 250) {
digitalWrite(greenPin, LOW); digitalWrite(yellowPin,
HIGH); digitalWrite(buzzerPin, LOW);
} else if (distance <= 90) { digitalWrite(greenPin,
LOW); digitalWrite(yellowPin, LOW);
digitalWrite(buzzerPin, HIGH);
}
delay(500); // Add a delay for stability
}
Learning outcomes (What I have learnt):
1. Able to understand and explain Ultrasonic Sensor and its working.
2. Able to understand and explain how to connect Arduino, Ultrasonic Sensor, and use of it.
3. Able to understand and explain the concept of audible and inaudible range, pulseIn function and how
does sensor calculates the distance between object and it.

You might also like