Ultrasonic Distance Measurement Board
An Ultrasonic Shortcut – Getting the HC-SR04 Transducer Up and Running Fast!
The HC-SR04 ultrasonic range sensor is a pretty cool pre-wired module that you actually enjoy using when you start playing with electronics, microcontrollers, and robotics. As you might have noticed, the most popular version of the module (comprised of an ultrasonic transmitter, ultrasonic receiver, and control electronics) has four interface pins — Vcc (5 V), Gnd (0 V), Trigger (Pulse In), and Echo (Pulse Out). The operating current of the module is about 15 mA at 5-V DC input, and its operating frequency is 40 KHz. The measuring range of the module is in the 2- to 400-cm range with a measuring angle of 15°.
The ultrasonic distance measurement principle of the module is simple and straightforward. When enabled, the ultrasonic transmitter (T) of the module sends an ultrasonic pulse train in one direction, which returns immediately to the ultrasonic receiver (R) when it encounters an obstacle in its way. Because the velocity of sound is almost constant (340 m/s), the information about the distance is strictly pertained to the elapsed time; i.e., the time of travel from the sender to receiver. With a known elapsed time, the distance of the object can be calculated using the formula: Distance (d) = Velocity of Sound (v) x Elapsed Time (t) / 2.
The operating method of the module is also not very hard. If it’s in a way that we all understand (see the timing diagram below), when the trigger pin (Trig) of the module is set to a high level for at least 10 µs, the transmitter starts sending an ultrasonic pulse train of eight pulses (40-KHz square wave) and then listens for the echo (reflected signal). The module will output a high-level pulse (approximately 100µs to 25 ms) on the echo (Echo) pin with a width that corresponds to the distance measured. Because the pulse width of the echo signal is proportional to the measured distance, a simple divide operation (as described before) can then scale the value to centimeters or inches if needed. That’s it.
Key components of the HC-SR04 module
- STC11F (single-chip microcontroller, based on 1T architecture 80C51 CPU)
- MAX232 (dual EIA-232 driver/receiver)
- TL074 (low-noise JFET-input operational amplifier)
- 40-KHz ultrasonic transmitter and receiver pair (40T/R-B/W)
Schematic of the HC-SR04 module
Get to the point
In truth, my intent is not to provide a new translation of some well-worn “Arduino & Ultrasonic Sensor” tutorial, which are available all over the web. A bit of Googling will hopefully get you tons of guides on that. Take note, I already penned a simple tutorial here in year 2014; it’s a done deed (https://www.electroschematics.com/10448/arduino-ultrasonic-proximity-sensor/).
On to the shortcut: Recently, I came across a cheap module with the label “HC-SR04 Ultrasonic Distance Measurement Control Board.” The module, also named “LC Mini Ultrasonic Tester,” consists of an 8-bit STM microcontroller as the brain (as informed by my supplier). It can be used for distance measurement with a plug-in HC-SR04 module to display results (distance in centimeters) on an integrated three-digit LED display. Because the module delivers the same data through two headers (tx/rx) onboard — yes, the serial interface — you can try it with your terminal application through a standard 5-V USB-TTL adapter, too.
Pin-out of the HC-SR04 tester module
Key components of the HC-SR04 tester module
- STM8S103F (multi-purpose 8-bit microcontroller) — https://www.st.com/resource/en/datasheet/stm8s103f2.pdf
- MCD3631BS (three-digit, common-anode, super-red LED display) — (http://mcdelectronics.com/userfiles/1185/files/LED%20Displays/triple_digit_7_segment_display_GNT-3631Ax-Bx.pdf)
Schematic of the HC-SR04 tester module
It’s undoubtedly a nice concept and clever design. The compact module helps us to test HC-SR04 modules quickly without a costly microcontroller setup (no code lines/solder smokes) and/or build an extremely portable minimalistic sonar device. I have seen too many posts highlighting the erroneous distance reports of the HC-SR04 ultrasonic sensor module. In principle, it can “conditionally” detect the distance of objects between 2 and 400 centimeters. Furthermore, the HC-SR04 tester introduced here is never 100% accurate (normally about 1 to 2 cm out). As for your measurement, it depends on where you are starting your manual measurement.
The designer of the “official” module (tailored on an STM chip) provided it with the single-wire interface module (SWIM) program header, so reverse-engineering of such modules may be possible to a certain extent. The STM8 MCUs debug system interface allows a debugging or programming tool to be connected to the MCU through a single wire. This results in a bidirectional communication based on an open-drain line and provides a non-intrusive read/write access to RAM and peripherals during the program execution. ST-LINK/V2 is an in-circuit debugger/programmer for the STM8 and STM32 microcontrollers with SWIM and JTAG interfaces. It can communicate with any STM8 (or STM32) microcontroller located on an application board. Nowadays, you can go with the original ST-Link V2 or, if you really like, go economical and opt for a $10 clone. Remember that the clone works fine but will never give you the full functionality and speed of the genuine debugger. Here’s one related link (not a close fit): https://www.electroschematics.com/11194/stm-mini-development-board-unboxed/
Lying in wait
By contrast, the board and microcontroller look identical. However, upon close inspection, I noticed that my module is not centered on STM8S103F but one $1 Nuvoton N76E003AT20 chip. Obviously, it’s not a cheap replica of the STM chip because in terms of hardware architecture and many other lustrous features, N76 actually varies very much from STM (http://www.nuvoton.com/resource-files/DS_N76E003_EN_Rev1.00.pdf). Likewise, there are other distinct HC-SR04 tester modules based on STC microcontrollers (STC11F04, STC12F04, etc.). One idea and more than three (?) microcontrollers from various vendors — that’s China!
Finally, some random “sonar” shots from my workbench: