Ultrasonic Sensor with Arduino Tutorial

What is an Ultrasonic Sensor

An ultrasonic sensor is a sensor that can measure the distance to an object. It emits an ultrasound that travels through the air and if there is an object or obstacle on its path It will bounce back to the module. By using the travel time and the speed of the sound you can calculate the distance

The things we are using here:

1. Arduino UNO
2. Ultrasonic Sensor HC-SR04
3. Male to Male Jumper Wires
4. LED

 

 

After making this circuit please do connect your Arduino board with your pc
Download Arduino IDE from their website and open it.

Upload this code
Use the following code to connect

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

int led = 13;
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  pinMode(led, OUTPUT);  
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed

}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
 

if(distance < 10)
{
  digitalWrite(led, HIGH);
}
else {
   digitalWrite(led, LOW);
}
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

before you upload the code you have to choose the board and port. if u want to know how to do that please check the video ..
i am explaining everything about this sensor and circuit in this video. make sure you checkout that too ..

 

The connection and programming details showed in the video

Download Source Code : 

Share with your friends:

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

HTML Form to Email Using JavaScript and SMTP server 🚀

Hello Everyone, 🙋‍♂️ In this tutorial, we are going to look at how to send emails from HTML Form to […]

22 September, 2021

How to make pretty URL in PHP using .htaccess

Hello Everyone, Here in this tutorial, you will learn How to make a pretty URL in PHP using .htaccess. and […]

18 September, 2021

Submit Form Without Refreshing Page PHP and Javascript

Hello Everyone, In this tutorial, we are going to learn, How to submit a form without reloading. yeah, that’s so […]

HTML from to Email using SMTP in PHP with Source code

Hello Everyone, In this tutorial, I am going to teach you how to send an email from an HTML form […]

16 September, 2021

Newsletter

Subscribe to our weekly Newsletter and stay tuned.


Free PHP Invoicing Software

Gmax CRM Lite is an Invoicing and project management tool for small businesses, features including client management , invoicing , quotation and project management

Download for free