Ultrasonic sensor with Arduino

ultrasonic sensor with Arduino Tutorial

Arduino Tutorial

Hey guys, welcome back to Techatronic. So, as we are at the last of this Arduino course. I hope you have learned a lot of things during this course session. We have uploaded a lot of detailed articles which absolutely help to learn the basics of the Arduino and the programming and in this article, we will perform a very good activity Ultrasonic sensor with Arduino. there is a lot of sensors available in the market but the ultrasonic is one of the most usable and important sensors in the series of Arduino sensor.

 

Ultrasonic sensor with Arduino

so, today we are giving all the detail required to make this activity work. I hope you will learn many things in this tutorial too. what you need to do is? you need to read the carefully full article and follow all the given instruction in the article you will definitely learn many things. The ultrasonic sensor used in many projects like automatic hand sanitizer is one of the most famous which was made by many companies in the covid time. same as this there is a lot of examples given on our website you can see this in the blog section. if you want to make any of this project you need to learn first how to interface ultrasonic sensor with Arduino.

In this activity, we will measure the distance by using the ultrasonic sensor. and the output will be measured in the Serial monitor. The serial monitor is the monitor given by the Arduino IDE where we can see all the output given by the sensor and also we can control the device by the user-defined command from the serial monitor. Now, we will talk about how to ultrasonic works and how can we use this sensor. 

 

What is Ultrasonic Sensor?

We use an ultrasonic sensor to measure the distance though, the ultrasonic sensor does not measure the distance even it does not detect any object near the sensor.it can only transmit and receives the ultrasonic waves from the two-port inbuilt it which help to calculate the distance from the object. It is up to you how you can use the sensor in ultrasonic sensor using Arduino

ultrasonic sensor work on the Ultrasound at the range of 40000 Hz. It has two components in the module. one is a transmitter which is known as a Trigger and another is a receiver which is called an Echo. The transmitter transmits the ultrasound and after the incident that sounds to any object, it will return back to the sensor, and this time the receiver receives the sound wave. the duration between transmitting and receiving we calculate by the Arduino and the sound wave speed we all know. At this same principle, we have made and a robotic car called obstacle avoiding robot. now we have both speed and time now we can calculate the distance by the time, speed formula. It totally depends on the ultrasonic sensor Arduino program

 

Distance calculation in the ultrasonic sensor?

sound wave generates and transmits by the transmitter. now, this sound wave collides with any objects and comes back to the receiver. the duration is taken by soundwave helps to calculate the distance cover by the soundwave. as we know the velocity of the soundwave. and we get the duration to come back the sound wave. now we have both duration and velocity. Now, we can calculate the distance by the formula?

ultrasonic sensor working

 

 

Why this activity?

This activity is to learn to find the distance and Detect objects using ultrasonic waves. also, you will revise the distance and time formula. the ultrasonic sensor is the most widely used sensor in the learning of robotics and stem.

Learn 10+ basic activity & sensor interfacing with our Arduino ebook. Well explained program. And brief circuit diagram WhatsApp and email support. which will help you to learn basic electronics, Arduino Coding, Sensor interfacing with Arduino, Arduino, and much more. buy Arduino Ebook to learn https://techatronic.com/arduino-ebook/

Components Required

  • Arduino Uno
  • Ultrasonic Sensor
  • Breadboard
  • Jumper wire
  • USB Cable type A to B
S.No Component Required Quantity Buy Link
1. Arduino UNO 1 https://amzn.to/3ue2vmh
2. Arduino UNO Cable 1 https://amzn.to/3obXjht
3 Ultrasonic Sensor 1 https://amzn.to/2YDdABE
4. Jumper Wire 40 https://amzn.to/39q43jr
5. Breadboard 1 https://amzn.to/39vsRX2

connect ultrasonic sensor to Arduino

Arduino UNO Ultrasonic Sensor
( +5V ) VCC VCC ( Positive + )
GND ( Ground ) GND ( Ground – )
D9 Pin Trig Pin
D10 Pin Echo Pin

connect ultrasonic sensor to Arduino

ultrasonic sensor Arduino program explain

Code Explain

const int trigPin = 9;

const int echoPin = 10;

In the above line, we are taking two variables to store the trig pin and echo pin value.

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

in the above line, we set the trig pin to the output and the echo pin to the input pin.

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

First, send a LOW signal for 2 microseconds and then send high on the same pin for 10 microseconds.

duration = pulseIn(echoPin, HIGH);

distanceCm= duration*0.034/2;

distanceInch = duration*0.0133/2;

These three lines are to store and calculate the distance by using the speed and time formula.

Serial.println(distanceCm);

The above statement is to print the Distance into the Serial Monitor.

ultrasonic sensor Arduino code,

 

 // TECHATRONIC.COM  
 const int trigPin = 9;  
 const int echoPin = 10;  
 long duration;  
 int distanceCm, distanceInch;  
 void setup()  
 {  
   Serial.begin(9600);   
   pinMode(trigPin, OUTPUT);  
   pinMode(echoPin, INPUT);  
 }  
 void loop()  
 {  
   digitalWrite(trigPin, LOW);  
   delayMicroseconds(2);  
   digitalWrite(trigPin, HIGH);  
   delayMicroseconds(10);  
   digitalWrite(trigPin, LOW);  
   duration = pulseIn(echoPin, HIGH);  
   distanceCm= duration*0.034/2;  
   distanceInch = duration*0.0133/2;  
   Serial.println("Distance: ");  
   Serial.println(distanceCm);  
   delay(300);  
 }  

 

Upload the given code and make all the connections as given. if you have any questions or query you can ask us in the comment section.

Spread the love, Share this post with friends