Author Topic: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank  (Read 6243 times)

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« on: September 16, 2015, 09:10:33 PM »
After a fair bit of research on the subject, I landed a ten-pack of Chinese HC-SR04 4-Pin Ultrasonic Distance / Range Sensors to test their viability to measure the level of fluids in various containers. Originally intended for an outdoor water fountain, I installed one but never got a chance to test it out before the fountain was deployed. That fountain has been filling my garden with lovely trickling water sounds for two seasons now, but the water level sensor still remains connected to nothing. So it goes with many projects. One other application I had for it, however, employed such a sensor to measure the level of salt in my water softener. You can see my post about the project on the Venturii Blog here.  Over the past couple of nights I have been tinkering with the firmware in the VSAC (and made some significant improvements to the OWI code at the same time by the way...) but still get pretty consistent and random readings from the sensor. It is connected to a stable power supply (and not powered by a USB port.) Each firmware version I try causes me to swap out the Mega so I know it's not the Arduino hardware. The trouble is that some of the readings appear to be correct. With a measuring tape I've determined that the distance from sensor to salt is about 72 cm. My theory goes that as the salt is consumed, that distance will increase; some simple math will allow me to produce a percent full number and I can even set up alerts to let me know when I need to pick up more salt. However, all that is impossible until I can get the sensor to stabilize.

Here is an example of what I am seeing:



After much reading online, I've got two theories I'm going to pursue at this time. 1. The sensor is being polled too frequently. One reader commented to a post on this subject that [they] had encountered this situation where the outgoing pulses are being sent out in such short succession (since you have control over the timing of such pulses via the trigger pin) that it can actually mis-interpret the return of a sound wave from the last pulse as though it were returning from the current sound pulse, resulting in shorter measurements.

The other theory I read about is that a design flaw in the sensor itself can cause sonic disruptions in the air from outgoing pulses to be picked up prematurely by the receiver due to the two components being in too close physical proximity to each other. The reader who made this assertion claims he had resolved this issue by de-soldering the transmitting and receiving transducers and spacing them about 10cm apart. I'm not sure why you wouldn't just de-solder one of the two and leave the other on the PCB, most likely the approach I will take unless the reason for doing both becomes evident through experimentation. The author also noted that doing so did negatively impact measurements at shorter distances, which makes sense, and is likely not an issue for my application.

In any case, the shorter term fix may be in the firmware, I will extend the current "stun" feature to allow greater time between triggers, possibly even user-tuned. If, say, a once-per-second scan of the tank still yields wildly random results, I will sacrifice one of the sensors and attempt to spread the transducers out a bit more. If it does work, it should make for a very useful (and inexpensive) modification to any water softener. Prior to this, the only way I'd really notice I was out of salt was by accumulation of scale on the shower glass, but of course by that time it is too late!
Venturii - Integrate Everything

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
Re: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« Reply #1 on: September 17, 2015, 08:52:02 AM »
After a slight change in the code last night causing the HC-SR04 to only perform one scan about every 6 seconds, I checked the salt tank again this morning and the results are now [nada]. Apparently the sensor is detecting the pulse so close to the trigger time that it is saying the distance is 0 cm, which I've instructed the VSAC to discard, so effectively I get no updates from the device anymore. Definitely not what I was expecting. I tried connecting my test sensor directly to the VSAC in case there are other issues (sensor, mounting, location, salt content, etc.) but the results are identical. Next step: Remove the onboard distance-calculating math and send the time differential back to the host for off-board analysis.
Venturii - Integrate Everything

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
Re: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« Reply #2 on: September 17, 2015, 05:54:50 PM »
I thought I had it licked. It made perfect sense even, and I had a rational explanation for my findings - except that they were wrong. What I was noticing was that after I'd reposition the sensor, it would usually work fairly well. I've been mounting the sensor to the inside of the lid now as it has the best, widest, unfettered line of sight (and therefore sound) to the bottom of the tank. I only put one bag of salt in this last time because in order for this sensor to work properly it will have to be calibrated, and in order to calibrate it I need to get a base reading from when the tank is fully empty, and then another when it is brimful of salt. After starting with those two values, a simple percentage can be calculated, and I've got my datapoint. Unfortunately, every time I've walked away from the sensor in the tank, it invariably begins to give more and more sporadic readings until it eventually fails outright. The last couple of tests I did, moving the sensor, getting a [better] position and then hot gluing it in place, it would seem to work well (sometimes VERY well) giving fairly consistent measurement readings. I'd think I had it settled when I'd go to check the readings a few minutes later and the sensor is returning 0 cm. Power cycling didn't seem to help. Scratch head.

I started to wonder if it may be conductivity, or capacitance in the hot glue causing some sort of cumulative effect. I was, after all, getting some of the glue directly on electrical components on the HC-SR04, but I always thought hot glue was non-conductive. This time I tried to mount it by the wires only so it effectively dangled inside the tank, pointing more or less at the bottom. Obviously aiming was a lot more difficult like this, but at this point I was a few steps behind aiming the sensor.  Could it also be salt dust in the air? I know salt water is highly conductive... Not sure about salty air. No change. Sensor worked for a while, then eventually pinned back to 2 microsecond round trip times.

Remember, that last go round I took out all the math and just had the sensor send me the round trip time in microseconds. Regardless of where I mounted it, eventually it returned to a round trip time of 2-3 microseconds!  ??? Suddenly it dawned on me, the only way it would be getting such a low reading of distance was if the sound pulse emitting from the transmitter was actually traveling through the PCB and being picked up physically by the receiver. A theory began to form, I was testing the sensor long before the hot glue had solidified. What if, as the hot glue got cooler, it also became harder and therefore made the whole assembly more rigid, allowing physical transmission of sound better? It would explain why it took a few minutes of decent operation for the sensor to begin to get sporadic readings, and once it was fully hardened - remain consistent at returning 2-3 uS times...

To test this theory, I removed my rigid mount, peeled all the hot glue off the sensor (I've been trying multiple sensors along this time too, by the way. They all seem to behave identically, and none of them ideally.) I found a piece of foam, and just put a dab of hot glue on each corner, careful not to touch any of the circuitry and to only [just barely] hold the sensor. Once mounted, I closed the lit again and the readings were rock solid. It made so much sense in my mind, I had conquered this tricky problem. About half an hour later I checked the readings and the sensor was again returning 2 after 2 after 2. Back to the drawing board. 

Could it be orientation? Maybe, for some odd reason, these sensors do not like being mounted facing down? To test this, I detached the lid and just set it on top of the unit, facing upward. Same results. Worked for a while, then got more and more sporadic and eventually dropped and stayed at 2.

I know I had measured power on it before, maybe it's worth checking again. I'm also trying a slight modification to the code wherein I ground the echo pin for a few uS before sending the trigger. Who knows, maybe if there is some capacitive effect at play here that may help reduce the effect?

All this time I am swapping back and forth with two Megas, one in play while the other gets programmed and tested. The more frustrating part is that on the bench, the one [not hooked up to the water softener setup] seems to work reliably, indefinitely, further casting a coin in the coffer of environment as a cause. If environment is to blame though, [what?] Wire length? It's about 8 feet of 4/22. Power? I'll check again. Noise? There shouldn't be any electrical interference inside or near the Water Softener. Operating environment? It seems to do it in open air pointed at the ceiling. This is still very much a mystery. I might have to get someone with a scope in here to see if [together] we can't figure out what on earth is going on with these sensors!
Venturii - Integrate Everything

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
Re: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« Reply #3 on: September 17, 2015, 09:40:36 PM »
Re-checked the power. Voltage at the sensor was 3.4 VDC. Re-wired the power upstream and it's solid as a rock now. Sigh. Good thing to know for next time I guess. And, I can now monitor my salt levels!  :)
Venturii - Integrate Everything

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
Re: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« Reply #4 on: September 19, 2015, 04:44:38 PM »
Several days running and the readings have stayed within a 3 mm (yes, three millimeter) reading! Tonight the water softener is set to do a regeneration cycle; it will be interesting to see if the salt shifts as a result. Next up I need to find a good way to display this new-found data source; there aren't a lot of water softener icons out there...
Venturii - Integrate Everything

Cube

  • Administrator
  • Contributor
  • *****
  • Posts: 86
    • View Profile
    • The Venturii Adventure
Re: HC-SR04 Ultrasonic Range Finder - Random Readings in a Tank
« Reply #5 on: September 19, 2015, 04:49:11 PM »
Sigh. A quick search on Google for water softener icons came back to a site describing the exact same solution to water softener salt level reading, using an ultrasonic sensor and all. (Different model, mind you...) Here I thought I figured this one out all on my own. Well, I guess I did - it just turns out I am not the first person to do so... That, and they also have created an icon for depicting the salt tank level. Theirs uses flash though, so I am going to stay away from that and see if I can come up with my own method in HTML5 to make it much more portable and accessible.
Venturii - Integrate Everything