Ultrasonic Sensor
HOW TO MAKE A ULTRASONIC SENSOR
FIRST THE COMPONENTS REQUIRED ARE :
1)ARDUINO UNO
2)ARDUINO CABLE
3)BREADBOARD WITH JUMPER WIRES
4)LED
5)ULTRASONIC SENSOR
ULTRA SONIC SENSOR
it is a sensor that works on the principle of echo .
every sensor including this have a vcc(positive) and a negative pin .
but this sensor specially have trigger and echo pins.
the formula the program uses to find out the distance is
that we know the time taken and speed of sound , thus,
velocity of sound * time taken = distance
but that is twice the real distance as it is reflected back.
therefore , real distance = V*t divided by 2
= 330*t/2 or 165*t
Connections
first make positive and negative rails.
note that it should be of 5v only otherwise it may harm the sensor
then connect vcc(positive) of sensor to the positive rail.
and ground of the sensor to the negative rail.
then connect trigger to digital pin 10
and echo pin to digital pin 9
then connect the longer side of led to the digital pin 6 and ground it
from the shorter end.
lets move to programming after doing these connections.
Programming Using Tinkercad ( Easy Start for Beginners)
the program is quite easy
int ultrasonic = 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(6, OUTPUT); }
void loop()
{
while (1 == 1) {
Serial.println(0.01723 * readUltrasonicDistance(10, 9));
if (0.01723 * readUltrasonicDistance(10, 9) >= 29) {
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
}
}
it is this small only