You’ll need a couple of digital I/O ports and a connection to the Arduino’s 5-Volt and Ground pins.Īctually if you’re short of pins you can even connect both the Trigger and Echo pins of the HC-SR04 to just one digital I/O pin on the Arduino and use code to switch the pin between output (to send the 10 uS pulse) and input (to receive the Echo pulse). The documentation recommends confining that window to 30 degrees (15 degrees on either side) for accurate readings.Ĭonnecting the HC-SR04 to the Arduino is pretty easy. As you can see the sensor is most accurate when the object to be detected is directly in front of it but you do get a response from objects within a 45 degree “window”. The illustration below shows the dimensions of the HC-SR04 Ultrasonic Distance Sensor as well as the effective angle of operation. Remember that the pulse indicates the time it took for the signal to be sent out and reflected back so to get the distance you’ll need to divide your result in half. The width of the received pulse is used to calculate the distance to the reflected object.This produces a pulse whose width varies between 150 uS to 25 mS, depending upon the time it took for the signal to be received. If the pulse IS reflected back the Echo pin goes low when the signal is received.This produces a 38 mS pulse that indicates no obstruction within the range of the sensor. If the pulse in NOT reflected back then the Echo signal will timeout after 38 mS (38 milliseconds) and return low.Meanwhile the Echo pin goes high to start forming the beginning of the echo-back signal. The eight ultrasonic pulses travel through the air away from the transmitter.This 8-pulse pattern makes the “ultrasonic signature” from the device unique, allowing the receiver to discriminate between the transmitted pattern and the ultrasonic background noise. The HC-SR04 responds by transmitting a burst of eight pulses at 40 KHz.A 5 volt pulse of at least 10 uS (10 microseconds) in duration is applied to the Trigger pin.The length of the pulse is proportional to the time it took for the transmitted signal to be detected. Echo – This is the pin that produces a pulse when the reflected signal is received.Trig – This is the “Trigger” pin, the one driven to send the ultrasonic pulses.VCC – This is the 5 Volt positive power supply.The HC-SR04 has the following four connections: If it receives them it produces an output pulse whose width can be used to determine the distance the pulse travelled. An Ultrasonic Receiver – The receiver listens for the transmitted pulses.An Ultrasonic Transmitter – This transmits the ultrasonic sound pulses, it operates at 40 KHz.
The sensors consist of two main components: Ultrasonic distance sensors use pulses of ultrasonic sound (sound above the range of human hearing) to detect the distance between them and nearby solid objects. And of course I’ll have some sample code and projects for you to try out. We’ll also put it through some tests to see how accurate it is and we’ll look at how we can possibly improve upon that accuracy.
So read on and I’ll show you how to hook up and use the HC-SR04 Ultrasonic Distance Sensor. And as a bonus it even looks cool, like a pair of Wall-E Robot eyes for your latest robotic invention! It’s a low current device so it’s suitable for battery powered devices. This ultrasonic distance sensor is capable of measuring distances between 2 cm to 400 cm (that’s about an inch to 13 feet for those of you who don’t “speak” Metric). It can also be used with the Raspberry Pi, however since the HC-SR04 requires 5-volt logic you’ll need a couple of resistors to interface it with the Pi’s 3.3 volt GPIO port. The HC-SR04 can be hooked directly to an Arduino or other microcontroller and it operates on 5 volts. This tiny sensor is capable of measuring the distance between itself and the nearest solid object, which is really good information to have if you’re trying to avoid driving into a wall! The HC-SR04 Ultrasonic Distance Sensor is an inexpensive device that is very useful for robotics and test equipment projects.