- Wed Sep 21, 2016 1:54 am
#55471
Success!!After a failed attempt at using a serial port to provide the precision timing necessary for reading the width of the echo pulse, I gave up on that idea. The theory was sound but the characteristics of the port and the truly blecherous code it required made this approach simply unmanageable.
So here's how I did it: I took a capacitor (47uf) and grounded the proper end. From the echo pin of the sensor, I ran through 3 diodes to cut the voltage to 3.2v then through a 330ohm resistor. The size of the capacitor and resistor were chosen so that I would have a relevant charge time from the echo pin while still keeping the current under control. The positive side of the capacitor is connected to the ADC pin of the ESP8266. It also connects through a resistor (180ohm) to a GPIO pin (#4 in my case because it was neither pulled up nor down by my development board). Another GPIO pin (#12 here because my board has an LED on that one) connects to the trigger pin on the sensor and with 5v and ground to the sensor, the circuit is complete.
The program (which I'll post later) starts by outputting a 0 on GPIO4 to drain the capacitor, and a 0 on GPIO12 to hold the trigger line low. These are left that way most of the time. To read a distance, I release GPIO4 by reading a value from it, placing it in input mode. This will allow the sensor to have free reign of the capacitor without interference. The I pulse the trigger pin by outputting a 1 then a 0 from GPIO12. I delay 30 milliseconds which gives the sensor time to do its work and return a high pulse of a width representing the distance. When the echo pin goes high, current can flow through the diodes at a 3.2v max. Since current is limited, the capacitor charges for as long as the pulse is present. The wider the pulse, the more the capacitor charges. Once the pulse goes low and the 30ms delay has expired, I read the ADC to get a voltage level in the capacitor. Then I discharge the capacitor by grounding GPIO4. This is not instantaneous and I have to let some time pass before I can assume the capacitor is discharged (as it goes through a current limiting resistor). Experimentally, about 100ms seems to work but as a general rule I don't think I'll poll much faster than about 5 times a second.
How well does it work? Pretty good. I wrote some code to do simple statistical analysis of the performance because the sensor does return some erroneous readings, usually due to longer distances. Still, it's pretty accurate from about 2 inches to about 2 feet, with few errors happening. As I go under 2 inches, there is distortion in the readings due to the physical separation of the sender and receiver, causing a longer triangular path. As I approach 4 feet, I get about 20% errors in my readings and the ADC saturates at 4 feet. My calculations say I should be able to go further than that distance but I think my 5v is a little bit over that (allowing the capacitor to charge to more than the ADC can measure). That will also cause it to charge too quickly, shortening the distance I can sense.
Substituting a 560ohm resistor for the 33ohm one extends my range to about 6 feet before I saturate the ADC. However, error rates increase to well above 50%. This is a limitation of the sensor. I'd call it good up to 4 feet if you want to do simplistic error checking and if you want to go to 6 feet you should add some simple statistical analysis to detect and discard errors. I'll post later about how to do this.
I will tweak this a little to try and get better performance but if I was to redesign it, I'd go with a slightly larger capacitor and larger resistors. Know that some good calculations do matter to get you in the right ballpark with both charge times and current limiting. This is why I started with 50uf for the capacitor, deriving that from current limiting resistors and necessary charge times.
The last thing I have to do is to make precise distance and ADC reading correlations and build a table so that I can translate from ADC readings to fairly precise distances. So far though, it looks like this can get to within a 1/3 inch of resolution over much of the range. Didn't expect that.