Sneaky Robots

Learn Build and Share with us

Tutorials

How to connect and run Ultrasonic Sensor (HC-SR04) with Arduino?(10 min project)

Ultrasonic sensors

In previous posts, Sensors and their applications and Types of Sensors we talked about what are sensors, types of sensors and where they can be used.

This type of sensor is used to measure the distance between an object and the sensor, this is done through the use of sound waves. We can the distance by measuring the time it takes for the sound to travel back and forth.

Speed of sound = 380 m/s

Speed = distance / time       so  Distance = speed(380 m/s) * time

Then the distance we get is back and forth, and we need only the distance one way. So the value is divided by 2.

 

Components Required

1.  Arduino uno
2. Male Jumper wires
3. Breadboard
4. HC-SR04 UltraSonic Sensor

Circuit Diagram/ How to connect them?

VCC pin to the Arduino 5v
GND pin to the Arduino GND
TRG pin to the Arduino Digital pin 9
ECHO pin to the Arduino Digital pin 10

BreadBoard Diagram For UltraSonic Sensor With Arduino
BreadBoard Diagram For UltraSonic Sensor With Arduino

Code Needed

Below is the code I copied and pasted to my Arduino IDE  which is explained more in details. If you follow the link:  https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

 


// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

l

After uploading the code to Arduino, Open the serial monitor which is found in the top right corner. (Make sure you have the right port selected for the Arduino as discussed in the first post). The results below show the distance when I move an object towards and away from the ultrasonic sensor. If it worked, Congratulations you just completed a tiny project. You are going in the right direction and hopefully get to finish harder projects together.

Results for ultraSonic Sensor with Arduino
Results for ultraSonic Sensor with Arduino

 

 

 

 

 

 

 

 

 

 

For a complete list of articles in understanding robots, check out this post. And if you’re looking for your next project/tutorial, check the following resources:

Controlling an LED wireless using Andriod App, Bluetooth HC-05, and Arduino Uno

How to control an LED with Arduino using a resistor?

How to build an RF BASED REMOTE CONTROLLED ROBOT?

 

 

 

Hamoudi
I’m an electrical engineer and a part-time blogger. I love to Play around with electronic parts (build small robots of any kind). I try to post information that can help others.

LEAVE A RESPONSE

Your email address will not be published. Required fields are marked *

You Might Also Like