Interfacing of ultrasonic sensor with an Arduino using Tinkercad
In this Project, we will make an alert alarm using the HC-SR04 ultrasonic sensor. The ultrasonic sensor used in this is utilized as a distance sensor, it will give us the distance at which the obstacle is. Using this distance value, we can turn on or off the buzzer and also, we can flash the LED light.
Circuit
Diagram:
The
hardware a part of this project is extremely easy to place together. First of
all, make the connections for the ultrasonic sensor with the Arduino. The
connections for the ultrasonic sensor with the Arduino given below:
connection:
- Connect VCC on the ultrasonic sensor to the 5V pin on the Arduino.
- Connect the Trig pin on the ultrasonic sensor to pin 4 on the Arduino.
- Connect the Echo pin on the ultrasonic sensor to pin 5on the Arduino.
- Connect the GND on the ultrasonic sensor to GND on the Arduino.
After making the connection with ultrasonic sensor, make the connections for the buzzer, LED light and the Arduino. Connect the positive pin of the buzzer with pin 2 on the Arduino and Positive pin of the LED to the pin no 3 of an Arduino, buzzer’s negative pin with the GND pin on the Arduino also connect GND pin of LED connect to the GND pin of an Arduino.
How does this project work?
The ultrasonic sensor generates an ultrasonic sound wave from the trigger
which reflects back after hitting the obstacle in front of the sensor and it is received by the an echo of the
ultrasonic sensor. The echo will gives us the distance traveled in
microseconds. To send an ultrasonic sound
wave from the trigger of the sensor, we have to set the trigger high for
10us. This will send 8 cycle sonic burst at 40 kHz which will strike the
obstacle and is then received back by an echo.
Now we have the time in microseconds but we have to calculate the distance. So, for that we
can use the below equation:
S = v * t
We know the value of t that is time and we know that the speed
of a sound in air is 340m/s.
We need to convert the speed of sound into cm/us to calculate
the distance.
The speed of sound in cm/us is 0.034cm/us. Now the equation will
become.
S = (0.034 * t)
We need divide this equation by 2 because we only need the distance it takes to strike the obstacle
and not the distance it takes to strike the object and come back. So, the final
equation will become:
S = (0.035 * t)/2
We can get the distance value using an equation above and after
that, we will set a value which will help us make the buzzer on and off.
Specifications:
The specifications of the ultrasonic distance sensor HC-SR04
are below:
Minimum measuring range |
2 cm |
Maximum measuring range |
400 cm or 4 meter |
Operating Voltage |
5V |
Operating Current |
15mA |
Working Frequency |
40 KHz |
Trigger Input signal |
10us pulse |
Measuring angle |
15 degree |
Code Explanation (Block Based):
After declaring the variable we set that
variable to following:
Now we want to print the distance value on the
screen for that we will use the serial monitor, we will assign the distance
variable to the serial monitor:
The trigger pin and echo for the ultrasonic
sensor connected to an Arduino pin 4 and echo pin to pin 3 on the Arduino. We have
initialized these pins in the code. Then we initialized the buzzer pin and LED
pins.
Now we will use the main conditional statement
of the program that is ‘if’ block
In if block
we will use the condition for that we need the comparison operator here we have
used the less than “<” comparison operator.
Condition
is given that if the distance is less than 30 cm then pin 3 will high and low
with time interval of 0.5 sec. it acts like LED flasher. Same way we can add
the pin 2 as high for buzzer to produce the sound.
Arduino Sketch:
int distance = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin,
OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH
state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and
returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
}
void loop()
{
distance = 0.01723 *
readUltrasonicDistance(4, 5);
Serial.println(distance);
if (distance < 30) {
digitalWrite(3, HIGH);
delay(500); // Wait for 500
millisecond(s)
digitalWrite(3, LOW);
delay(500); // Wait for 500
millisecond(s)
digitalWrite(2, HIGH);
}
}
Watch Working Video:
Comments
Post a Comment
If you any query please comment