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 : 

https://youtu.be/XX13-q6jY1M

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

How to use Temperature and humidity sensor DHT11 with Arduino tutorial

Hello Everyone, In this tutorial, we are going to make a small project with Arduino and Temperature and humidity sensor […]

September 6, 2021

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 […]

September 5, 2021

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now