• Rhea Mantri

HOW TO INTERFACE AN ULTRASONIC SENSOR WITH ARDUINO USING TINKERCAD


An #Ultrasonic Sensor is an electronic device that measures the distance of an object or obstacle via Ultrasonic sound waves (that travel faster than the speed of audible sound), and it converts the reflected ultrasonic waved into electric signals. It is one of the most reliable and accurate proximity sensors.

The #sensor is based on the measurement of the time of flight of an ultrasonic pulse, which is reflected by the ground, it sends out Ultrasonic waves that has a range of frequency above human hearing. Ultrasonic Sensor is commonly used in obstacle avoiding robots and automation projects.


LIST:

Software: #Tinkercad

Components:

  1. Ultrasonic Distance Sensor

  2. Arduino Uno R3

1. HCSR04: is the Ultrasonic Sensor used. An ultrasonic sensor contains two circular structures: one is the #transmitter and the other is a #receiver. The transmitter transmits the ultrasonic sound while the receiver receives the reflected signal.


Specifications:

2. Arduino Uno R3: The #Arduino is a micro-controller board that is based on the ATmega328P, it consists of digital and analog input/output pins that are interfaced with the ultrasonic sensor (used to send and receive data). the Arduino Uno IDE consists of a Serial Monitor that displays the output.


CIRCUIT:


Circuit connections:

Ultrasonic sensor has 4 pins:

1. Vcc: connected to the 5V in Arduino

2. TRIG (trigger): connected to PIN 10 of the Arduino

#Trigger is used as Transmitter, this pin is used to transmit the sound from the Ultrasonic sensor. Trigger should be high. It is connected to the digital pin of the Arduino and used as output pin.

3. ECHO: connected to PIN 9 of Arduino.

Echo is used as a Receiver, it receives the sound. It is connected to the digital pin of the Arduino and used as input pin. In this diagram, we have connected Echo to pin 9.

4. GND (ground): connected to the GND of Arduino


Circuit diagram:

CODE:

int trigPin = 10; 
int echoPin = 9; 
long time;
int distance;
 
void setup()
{
 pinMode(10, OUTPUT); 
 pinMode(9, INPUT); 
 Serial.begin(9600);
}
 
void loop()
{
 digitalWrite(10, LOW); 
 delayMicroseconds(2); 
 digitalWrite(10, HIGH); 
 delayMicroseconds(10);
 digitalWrite(10, LOW); 
 time = pulseIn(9, HIGH); 
 distance = time*0.034/2; 
 Serial.print("Dsitance: ");
 Serial.println(distance);
}



Explanation:


The variables and pin connections are declared and defined before both the loops


There are 2 loops in the code:

1. Void setup -code here is run only once

2. Void loop- code run continuously to measure the distance


1. Void setup:

  • The Trig pin connected to the digital pin of the Arduino is set as output and the Echo pin as input

  • Serial.begin(96000): to start communication

2. Void loop:

  • Initially, the transmitter has to be low. Thus we have to clear the trig pin by setting it as low.

  • Lower or delay this pin for 2 microseconds.

  • Now trig pin is set as high to activate the transmitter and transmit the Ultrasonic signal.

  • A delay of 10 microseconds is given before transmitting another signal.

  • Now we have to low the trig pin again (clear)

  • To receive a signal, the echo pin is made high, thereby activating the receiver.

  • The transmitted signal is received by the receiver and the time of flight i.e., time taken to receive the signal is calculated (in microseconds)

  • the pulseIn function is used to read the length of the pulse from the Echo pin.

  • Through this time, we calculate the distance of the object or any obstacle in the path by using the formula distance= Time of flight * 0.034 / 2

  • at 20°C, the speed of sound in air (in microseconds) = 0.034

  • We divide by 2 because the signal strikes the object and bounces back to the receiver, thus we do not want to find the total distance traveled, but only the distance between the transmitter and the object.

  • Now we print the distance measured

  • Simulate the project and output can be seen on the Serial Monitor

CODE IN NOTEPAD
.txt
TXT • 979B

WORKING:

the Ultrasonic Sensor has to silver cylindrical structures (called Ultrasonic transducers) which consist of a transmitter t(hat transmits the sound waves) and a receiver.

the sensor generates a sonic burst that is emitted from the transmitter and bounces to the receiver after hitting the object. A 8 cycle ultrasound burst at 40KHz is created. the echo pin reads the time the sound waves take, in microseconds.


How to calculate distance:




· Blue box: Ultrasonic sensor

· Yellow box: Transmitter

· Red box: Receiver

· Green box: object/ obstacle





An Ultrasonic Sensor measures the time of flight of an Ultrasonic wave and by measuring the time, it measures the distance of the object.

When an object is placed in front of an ultrasonic sensor, ultrasonic waves are emitted on the object which is reflected towards the receiver. When the waves are transmitted and received, the total time taken for the Ultrasonic waves the hit the object and reflect back is calculated. Distance is calculated through time.

General formula for distance= T x C

To calculate object distance: D = ½ T x C

D= distance of the object or obstacle

C= speed of sound in air = 0.034 microseconds

We divide the time of flight by 2 as we do not want to measure the total distance i.e., the distance taken for the pulse to hit the object and the distance is taken for it to reflect the receiver. We only want the calculate the proximity of the object.

This gives the distance of the object from the sensor; Thus, we have to divide the time by 2.

Distance (cm) = Speed of sound (cm/µs) × Time (µs) / 2


Serial Monitor: Output can be seen on the Serial Monitor after we start the simulation

working explanation:


CONCLUSION:

The working of the ultrasonic sensor is understood. The steps and methods to interface the ultrasonic sensor with an Arduino are described. When the code is run and the circuit is simulated on TinkerCad, the distance of the object or any obstacle in the path of the sensor is calculated. The output can be seen on the Serial Monitor, when we change the location of the object, the distance is calculated and displayed on the serial monitor.

SEE ALSO:

the detection range of the Ultrasonic Sensor is dependent on the position and target size. The main advantage of this sensor is that, as the target size, increases, the reflected signal becomes stronger and the distance calculated is more accurate. Thus, the Ultrasonic sensor is a very useful device that has various applications such as:

· Car backing systems

· #Automation & Robotics

· To detect the depth of snow

· To find the water level of the tank

· In the production line

· Used in civil and mechanical fields for small and accurate measurements.


similar articles:

Object detection using Ultrasonic Sensor & Arduino


183 views