I have bought several cheap echo location circuits from AliExpress and I'm planning to use them in one of my robotics projects.
|HC-SR04 Front View||HC-SR04 Back View|
They are very cheap especially if you buy a dozen you can get them bellow 2$ shipped. Once they've arrived, to test them, I wrote a quick C test program for an AVR board which displays the distance in cm to an obstacle. The results were pretty accurate for as far as 3-4m distance for hard obstacles. Unfortunately when there are no obstacles in front of this detector or when the obstacles don't have hard surfaces then erroneous results are returned.
I wanted to understand why this was happening so the first step was to reverse the HC-SR04 module schematic.
The circuit has 2 transducers one for emission and one for reception. To transmit the ultrasonic pulses a relatively high voltage is needed. A MAX232 (U3) is cleverly used to produce +/-10V (which are the normal USART voltages) from 5V. The transducer is connected between two outputs so it is in fact powered at 20V. Power is only applied to this circuit through Q2 some time before and during pulse emission because the internal switching charge-pump is noisy. When the circuit switches to receive mode the MAX232 power is cut off. The receive and emit circuits are controlled by an EM78P153S chinese microcontroller running at 27MHz. The receiver side uses LM324 which contains 4 OPAMPs. U2D is just a times 6 amplifier. U2C is a multiple feedback (1st order) pass band filter which is followed by another times 8 amplifier (U2B). The last OPAMP (U2A) is used together with Q1 as a hysteresis comparator. I've simulated the filter in PSpice and it is not centered at 40KHz as it should be but instead it has a 18KHz peak. By changing just two resistors (R13 to 2K2 and R11 to 18K) the filter response is shifted to the pulse frequency and this greatly improves the detection sensitivity.
|Bandpass filter centered at 18KHz||Bandpass filter centered at 40KHz|
Bellow are some waveforms captured from the circuit. If you want to see them in detail I have saved all traces in VCD format here and you can use the free gtkwave viewer to analyse them.
After the trigger input is raised the uP detects it and after some 10us powers on the MAX232. After 248us, time to ramp up the +/-10V of the charge-pump, the 8 pulses 40KHz train is produced and then the power is switched off. During this time the comparator threshold is also kept low to prevent any spurious signals to be detected in the receiver. The circuit then asserts the echo signal and waits for the echo to return. As soon as the first pulse is detected in the receiver the echo signal is deasserted and one can measure the width of the echo pulse to calculate the obstacle distance. There are several problems with this circuit. First the uP uses polling to detect a return pulse. This can be observed because sometimes only the second pulse is detected. There is no attempt to check is that first pulse is part of a 40KHz train of pulses. A noisy environment will probably produce false pulses which will pass trough the analog 40KHz filter (which has a modest 6db/octave attenuation). The circuit biggest problem is the false echo pulses which are returned when there is no obstacle in front. These have a plausible and variable range and cannot be filtered out in software. The processor is OTP so cannot be reprogrammed and there is no easy way to change it and hook another one in its place. The whole circuit is made to minimize the cost to extreme. The 27MHz crystal was unmarked and when I've measured it had almost 1/1000 deviation.
In the end I've decided to change the processor and the board so I can improve this design. The analog part is pretty decent so I will keep all the components except the uP and the crystal.
|First I've marked exactly where each component was placed on the original PCB|
|Then I've designed my own PCB keeping the same component locations|
I've used the AtTiny24 in a SOIC14 package with a 12MHz crystal. This is the new HC-SR04E Schematic (I've appended an 'E' to the module name), the Bill of Materials and the Gerber files or Gerber panel if you want to use the cheap ITeadStudio PCB manufacture and get 20pcs for a tenner. Soldering one of these new boards takes about 20 minutes. The way I do it is by placing the original and new PCB side by side and desoldering the components from one with a hot air rework station and soldering them back in the same position on the new board. In this way I don't have to pay attention to component's values. Also notice that there are now 6 pins instead of the 4 on the old connector.
The advantages of this new schematic are:
- The SPI interface of the AtTiny is available at the 6 pin connector and it can be programmed in place as many times you want.
- There are 3 signal pins available instead of 2 and if you program the Reset even 4 signal pins but you'll lose the in-circuit programming.
- The received analog signal also connects to the AtTiny internal comparator so a timer with capture can be used to store all returned pulses and filter them by position and getting rid of any potential noise.
- The I2C interface is available so multiple modules can be wired together and have different slave addresses.
I have several plans with these new boards. First I'll just re-implement the original functionality with one Trigger and one Echo output while correcting the false echoes problem. Then I'll make an I2C version where the module triggers itself and computes the distance in 'mm' locally and stores it as a 2 byte value. Another master processor asynchronously reads this data only when needed. One of the available pins will signal a proximity condition when the distance decreases under a certain value. Another iteration will use an open drain pin to sync the emission of several modules to be able to use multiple sensors on the same robot without having to wait for each one to complete a measurement. AVR software to follow when I'll have some more time available.
Since I've written this entry several new versions of the echo locator sensor have been commercialized. Paul has reversed the schematics of one of these and kindly provided a link .