Introduction
In this project, we will use an Ultrasonic Sensor With Arduino to measure the distance to an object. the ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It is also known as a transducer or sonar. Ultrasonic sensors are used in a wide variety of applications, such as measuring the distance to a parking spot in a car or detecting objects in a factory.
In this project, we will use an ultrasonic sensor with Arduino to make an easy distance measurement device. This device will use the ultrasonic sensor to measure the distance to an object, and will then display the distance on the Serial monitor. Also, Check this What is Sensor? Different Types of Sensor & Their Uses.
Thank You NextPCB:
This project is successfully finished because of the service and support from NextPCB. NextPCB is one of the most professional PCB manufacturers in Global and has specialized in the PCB and assembly industry for over 16 years. Not only could NextPCB deliver the most creative printed circuit boards and assembly technologies in the most elevated quality standards, but the fastest delivery turnaround was also as fast as 24 hours.
- Only 0$ for 5-10pcs PCB Prototypes:https://www.nextpcb.com/PCBPrototypes
- 4-layer PCB price reduction up to 40%:Â nextpcb.com/40%off
- Register and get $100 from NextPCB:Â Nextpcb.com/coupon
Required Components
To get started, you will need the following materials:
- Arduino Uno
- Ultrasonic sensor
- Breadboard
- Jumper wires
- 9V power supply
What is an Ultrasonic Sensor?
An ultrasonic sensor is a device that uses sound waves to measure distance. It can be used to measure the distance between two objects or to measure the thickness of a material. Ultrasonic sensors are used in a variety of applications, including automotive collision avoidance systems, security systems, and industrial process control.
How does an Ultrasonic Sensor work?
An ultrasonic sensor is a device that emits sound waves at a frequency too high. When these waves hit a thing, they bounce back and are detected by the sensor. The time it takes for the waves to bounce back is used to calculate the distance to the object.
used in devices such as parking sensors, level sensors, and flow sensors, including obstacle detection, They are also used in some medical applications, such as ultrasound imaging.
Learn more about Ultrasonic sensors: lastminuteengineers.com
HC-SR04 Ultrasonic Sensor
The HC-SR04 is an ultrasonic sensor that can be used to measure distance. It is often used in robotics and automation applications. The sensor works by emitting a sound wave and measuring the time it takes for the sound wave to bounce back. The distance to the object can then be calculated based on the time it took for the sound wave to return. The HC-SR04 sensor is fairly easy to use and is usually used in DIY projects. It is also relatively affordable, making it a popular choice for multiple applications. The sensor is rather accurate and can be used to calculate distances up to 400 cm.
Here are the complete HC-SR04 ultrasonic sensor specifications:
Operating Voltage | DC 5V |
Operating Current | 15mA |
Operating Frequency | 40KHz |
Max Range | 4m |
Min Range | 2cm |
Ranging Accuracy | 3mm |
Measuring Angle | 15 degree |
Trigger Input Signal | 10µS TTL pulse |
Dimension | 45 x 20 x 15mm |
HC-SR04 Ultrasonic Sensor Pinout
The ultrasonic sensor has four pins:
- VCC Pin – positive terminal of the source should be connected.
- GND Pin – negative or ground terminal of the source should be connected.
- TRIG Pin – Acts as an output that emits Ultrasonic Sound waves.
- ECHO Pin – Acts as an input that receives the reflected Ultrasonic Sound waves.
Ultrasonic sensor Arduino connection
This sensor is perfect for any project that requires accurate distance measurement. The sensor can be connected to an Arduino using only a few wires. First, let’s connect the ultrasonic sensor to the Arduino.
Ultrasonic sensor And Arduino Connections:
- Vcc Pin______5V
- Trig Pin_____D2
- Echo Pin____D3
- GND Pin_____GND
Connect the Vcc pin to the 5V power supply pin on the Arduino. Connect the Trig pin to digital pin 9 on the Arduino. Connect the Echo pin to digital pin 8 on the Arduino. Finally, connect the GND pin to GND
Source Code
Now that the sensor is connected, we can write code to measure the distance to an object. The code is very simple.
Here’s an example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* * Interface HC-SR04 Ultrasonic Sensor With Arduino. */ // defining the pins const int trigPin = 2; const int echoPin = 3; // defining 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); } |
- Open Arduino IDE Software and write down your code, or Copy the Above code and paste it
- Choose an Arduino board, by selecting Tools > Board > Arduino/Geniuno Uno
- Choose your COM Port Tools > Port > COM..
- Upload your source code by pressing Ctrl + U or Sketch > Upload
- To display the measurement data you can use Serial Monitor by pressing Ctrl + Shift + M
The values ​​of the distance that the HC-SR04 senses will appear on the serial monitor. Put an object in front of it and change its distance from the sensor and confirm that the distance shown on the serial monitor is correct.