PC Services


PC Services (Electronics)

Circuit Diagram Ultrasonic Distance Sensor HC-SR04

Tel: 0118 946 3634

linked-in profile facebook profile

   The Company 
   Resources / Examples 
       The Electronics 

Circuit Ultrasonic Range Sensing

I had a batch of these boards to test at least one was known to be faulty, so I did some work to get the tools to test and fix these boards, which resulted in these pages for students and others to follow. This section details with the electronics such items as

Previous Pages cover


First thing to understand is that the order of events is

  1. Micro sends a trigger pulse to the unit to start a measurement, which INSIDE the unit
    1. Sends a burst of 8 x 40 kHz pulses via Ultrasonic sender
    2. Sets an Echo output signal HIGH
    3. In the real world the sound wave is sent out and reflected back off objects and the FIRST reflection back (echo) is deemed as the NEAREST object (other echos may be received after this, but are ignored).
    4. The first echo cause the unit to set the Echo output signal LOW
  2. Micro has to time how long the Echo signal from the unit is high to determine the time of the echo
  3. Micro can then convert time of echo to distance away, knowing the time is time to the NEAREST object and back again (round trip)

This round trip or echo is also known as a PING from submarine and sea depth sensing.

Signals on HC-SR04
Scope shot of signal timing
Scope shot of signal timing

Picture Left is a scope screen shot showing the external and internal signals for this process, where -

  • TRIG (Yellow) and ECHO (Magenta) are the signals between unit and micro,
  • TX (Cyan) and RX (Green) are the internal signals of the burst being sent and the echos received.

Note that the RX signal has many pulses on it as there are many echos after the first echo is received. Even if you have one object to detect in an open field, the sound waves can be refelected off the object, a small portion picked up by the sensor, and the rest of the sensor reflects back some of the wave which gets reflected several times.

Back to top

Measured Timings on HC-SR04

This section breaks down the timings shown in above picture to show how the timings are made up, from images like the ones below. The detailed timings are shown for an object 9.5 cm away -

Timings from oscilloscope
Start End Time µs Comment
TRIG rising TRIG falling 12 Trigger pulse
TRIG falling TX Start 264 Start of Ultrasonic Transmit
TX Start TX End 192 Width of Ultrasonic Transmit
TRIG falling ECHO rising 480 Start of Wait for RX
TRIG falling ECHO falling 1020 First pulse from RX
ECHO rising ECHO falling 556 Echo pulse width

Oscilloscope Screen Shots
Scope shot of Timing from Trigger pulse to Burst start
Scope shot of Timing from Trigger pulse to Burst start
Scope shot of Timing for burst of Ultrasonic pulses
Scope shot of Timing for burst of Ultrasonic pulses

Back to top

Version of HC-SRO4

The HC-SR04 is a cheap Ultrasonic Distance Sensor available all over the price very cheap and fairly reliable, however its accuracy is +/- 3 mm. This accuracy means that anything less than 1 cm measurement is at best a guess.

Version of HC-SR04 as of January 2017
Top View of HC-SR04 board
Top View of HC-SR04 board
Bottom View of HC-SR04 board
Bottom View of HC-SR04 board

Back to top

HC-SRO4 Circuit Diagram

As stated before as I had a batch of these sensors to test and at least one had a faulty component for sure I did some reverse engineering side by side comparisons between working and failing units. Along with seraches online that got me to circuit descriptions and circuit diagrams, but these were earlier revisions of the circuit, but good starting points. The main sites you should also look at for alternative viewpoints are -

  • Emil's Projects oldest version of board found, but detailed description.
  • K.C. Lee newer version which is probably version before board I have
  • Robert Clemenzi Another site with links to the other two and discusses issues with some units I believe now fixed
  • Application Note CN0343 Analog Devices document on Ultrasonics with more physics

All the software examples use Arduino things like builtin functions pulseIn() or libraries like Ping or NewPing, whilst you can use these for your software as discussed in betterecho, I find these have too many overheads or issues.

Having looked at the circuit diagrams on these sites and reverse engineering, I draw up a circuit in my CAD software and produced much smaller file size diagrams, that were easier for me to follow and use. The circuit is shown below and the link to a PDF version of the circuit.

My version of circuit diagram is available in PDF version here (size 22 kB) or viewable on screen below

HC-SR04 Circuit Diagram (click to enlarge)
HC-SR04 Circuit Diagram (click to enlarge)

Back to top

HC-SRO4 Circuit Description

The Unit consists of three parts - microcontroller, Transmitter and Receiver (with associated amplifiers). Please use the circuit diagram above for reference to this description.

Microcontroller U1

The heart of the unit is is the EM78P153 8-bit microprocessor from Elan (U1). This handles

  • Interface to host (Trig and Echo pins)
  • Timing and sending antiphase burst for ping to send
  • Squelch control, where by during Ultrasonic transmit, a threshold for the incoming reciver is effectively disabled to avoid bogus echos. This is important as whilst sending vibrations from the TX transducer will be received through PCB and by air between the transducers on the RX transducer.
  • Receiving the processed signal from the Receiver section as an interrupt (Rising edge), this is actually a filtered and much amplified version of all the echos received.

Transmitter U3

On older versions this was a MAX232, as of yet as the pin out is vastly different I am not sure what actual device it is as the numbers have been scrubbed off. It appears to be some form of transistor array device or Buffer device or ASIC. However some of its functionality has been found out.

  • Voltage drive to TX transducer from the antiphase TX signals from the micro (U1). By using antiphase signals, a differential voltage can be driven across the transducer effectively +/-5V across the transducer. The chip provides a higher current driver than the micro can produce. I suspect these outputs are actually some form of PIN driver, for high current drive.
  • The other part is two transistors with a common base pin, collectors available on other pins. this transistot forms part of the feedback loop on the final part of the receiver chain, to change an analog signal into a TTL type digital signal, and is basically a switch driven by the final stage to pull signal to 0V and pulled up to 5V by a resistor (R17)

Receiver U2

The quad op-amp IC (U2) is a LM324 which is a fairly old low grade 1 MHz unity gain bandwidth device, with limited range I/O (outputs do not go to rails). It is a ubiquitous and cheap device. Considering some of the gain levels used in the stages means that 40 kHz signals are passing through stages with around 100 kHz bandwidth. Large offset errors may effect some of the stages as well, when gained up.

The receiver is a chain of three op-amps as small signal stages all AC-coupled, being used in single rail configuration, the final stage is a variable threshold hysteris comparator with output switch. Each of the first three stages are an inverting amplifier or filter with positive input to mid-rail supply rail to apply offset for single power rail operation. the mid-rail supply is provided by R14, R15 and C6.

C1, C3 and C4 provide AC coupling between the stages.

First stage (U2D, R1 and R2) is an inverting amplifier of the RX transducer signal, with gain around 5.6 (allowing for tolerances). The output is attenuated with reference to mid-rail by R3 and R4.

Second stage (U2C, C2, C3 and R5) is a bandpass filter hopefully with tolerances centred around 40 kHz the frequency of the emitted pulses.

Third stage (U2B, R6 and R7) is another amplifier with gain of approximately 10.

The last op-amp stage is a variable threshold hysteris comparator with output switch, the output witch is formed by a transistor in U3 and pullup resistor R17. This output is part of the feedback on the hysterisis comparator via R9. R10 and R16 si a potential divider to get the correct base voltage for the transistor in U3. The variable threshold comes from R13 and R12, with decoupling and ground path filtering by C8 and R11. One end of R13 is tied to mid rail for normal threshold, whilst a 'Threshold' signal from the micro via R12 pulls the threshold on pin 2 closer to ground, thus forcing the output higher and the transistor in U3 on, to clamp the signal to the micro during TX burst transmission. Thus avoiding false echos too early.

Unfortunately I have found that problems in this area like R16 open circuit or bad (wrong value) parts like C8 can cause all sorts of problems with erratic readings. personally this would have been better with a proper Schmitt Trigger op-amp stage with HARD threshold control without C8 better timed from the micro from start of Trig pulse to end of TX burst (setting ECHO HIGH).

Back to top

Known Hardware Faults On Faulty Units

Symptom First Point to Check
Only able to detect large objects (e.g. A4 paper size) at around minimum distance of 30 cm R16 on one unit found to be open circuit should be 3k9 0603 size
Causes the input signal to the micro to be clamped for too long
Unable to get any readings, power connections good and TRIG pulse is valid, no echo Metal disc rattling inside TX or RX Ultrasonic transducers (behind the grill)
Replace unit or transducer on unit
Valid readings when sampled at 1 second intervals, but random reading or many errors when sampled faster
Readings interspersed with many timeouts (time of 0)
Coming Shortly

Back to top

Previous Pages cover

Interested in something similar contact sales.

© 2017 onwards by PC Services, Reading UK Last Updated: 1st February 2017
If you encounter problems with this page please email your comments to webmaster