An ultrasonic distance sensor is a sensor that can measure distance from a solid or physical object.
An ultrasonic range finder does this through sound waves.
It sends out high-frequency sound waves and then waits to listen back for these sound waves. If it has heard back these sound waves, then this means the sound waves have bounced off of a physical object and has come back to the sensor, which means a physical object is present in front of the sensor. If it does not hear back these sound waves, this means that no physical object has been detected.
The ultrasonic sensor we will use is the very cheap and popular HC-SR04 sensor. We will integrate this sensor with an arduino for a very good object detection system.
We will write code so that if the sensor detects an object that is less than 3 inches from it, it will turn on and light up an LED.
The ultrasonic range finder can detect objects anywhere from 2cm (less than an inch) to 400 cm (157 inches or 13 feet).
There are actually 2 types of sensors that are widely used and very popular for measuring distance from a physical object- these are the infrared proximity sensor and the ultrasonic range finder.
This sensor has a measuring angle of 15°. This means the object must be between 15° of the front of the sensors. If outside this range, it will not detect it. Ultrasonic range finders, in generals, have narrow measuring angles but wider ones do exist, so for your application, always check the datasheet of the sensor.
- HC-SR04 Ultrasonic Distance Sensor
The HC-SR04 Ultrasonic Distance Sensor is cheap, as said before. It can be gotten on ebay or many different online retailers for just over $1.
It runs on 5V of DC power and requires 2 of the Arduino’s digital pins for connection.
The datasheet of the sensor can be found at the following link: HC-SR-4 Ultrasonic Distance Sensor Datasheet.
The sensor has 4 pins in total. 2 are power pins and 2 are digital pin connections.
The pinout for the HC-SR04 is shown below.
he power pins are VCC and GND. The ultrasonic range finder works on 5V of power. This is connected to VCC. GND is connected to ground.
The trigger (trig) pin is the pin needed to trigger or start the sensor into ranging mode. A 10-microsend pulse needs to be sent to the trigger pulse in order for the sensor to be triggered on. Without this pulse and the sensor will never go into ranging mode to detect objects. Therefore, this is a crucial pin for that reason. A pulse of at least 10 microseconds needs to be sent for the sensor to start detecting objects. When it receives this pulse, it sends out 8 40KHz pulses to detect an object and waits to listen back for these sound pulses.
The echo pin is the pin that sends the result of the sent out pulses. If the pulses have been received back, this means that the pulses bounced off of an object and came back to the sensor, which means an object has been detected. The echo pin then returns the pulse representing the amount of time it took for the pulses to be received back. With this time, we can compute the distance the object is from the sensor. If no object has been detected, the pulses are not received back, so the echo pin returns no pulse or a 0.
Ultrasonic Distance Sensor Circuit
The Ultrasonic Distance Sensor Circuit we will use to measure distance from the object detected is shown below.
The circuit is very basic in connections.
The power pins are connected to the +5V and ground terminals of the arduino so that it can have sufficient power to operate.
Then the trigger pin is connected to pin 8, while the echo pin is connected to pin 7.
With this physical setup, all we need now is the code below.