-->
Page 2 of 2

### Re: Hooking up the HC-SR04 ultrasonic distance sensor

Posted: Wed Sep 21, 2016 2:26 am
Here it is, complete with circuit diagram:

HC-S04 sensor circuit.jpg

And here's the code I used to test it:

Code: Select all`io(po,12,0)io(po,4,0)c = 0av = 0esum2 = 0dim hist(8)dim er(8)meter c, 0, 1023wprint "<br>"meter av, 0, 1023wprint "<br>"meter esum2, 0, 10timer 500, [tick]wait[tick]io(pi, 4)io(po, 12, 1)io(po, 12, 0)delay 20t = io(ai)if t<1023 then  c = t  e = 0 else  e = 1  endifio(po,4,0)sum = 0esum = 0hist(8) = cer(8) = efor t=0 to 7  er(t) = er(t+1)  hist(t) = hist(t+1)  sum = sum+hist(t)  esum = esum+er(t)  next tav = sum/8esum2 = esumwait`

This is test code, meaning that it does more than just sample the sensor. This will make 2 samples per second, report the last readable results in the top meter, the average distance over the last 8 samples in the second meter, and the percentage of known errors in the bottom meter over the last 10 samples.

For a stripped down version to just do individual samples, the following will work:

Code: Select all`' Initialization of pinsio(po,12,0)io(po,4,0)'  Your code continues herewait[sample]io(pi, 4)io(po, 12, 1)io(po, 12, 0)delay 20sample = io(ai)io(po,4,0)return`

Code: Select all`' Initialization of pins  ' Be sure we are not pulsing the sensorio(po,12,0)  ' Ground the capacitor to keep it dischargedio(po,4,0)'  Your code continues herewait  ' GOSUB here to sample the sensor.  SAMPLE contains the result, 0-1022.  1023 = error.[sample]  ' Allow capacitor to floatio(pi, 4)  ' Pulse the sensor to initiate a readio(po, 12, 1)io(po, 12, 0)  ' Wait for the sensor to respond, allowing it to charge the capacitordelay 20  ' Read the state of charge of the capacitorsample = io(ai)  '  Start the capacitor discharging.  Should allow about 100 milliseconds for discharge, maybe more.io(po,4,0)  '  All done!  Here is where you might add statistical analysis and/or precise distance conversion to  '  burn up some of that 100ms you need to waste.return`

Now remember, this returns state of charge of the capacitor which is NOT linear. The way to turn this into a fairly precise distance is to build a table of sampled values of known distances and do a simple interpolation. I'll get to that another time.

Reminder: The circuit diagram is pictured with a 330ohm resistor. That will limit you to about 4 feet but with fewer sensor errors. A 560ohm resistor gets to about 6 feet but with increasing sensor errors. Your choice.

### Re: Hooking up the HC-SR04 ultrasonic distance sensor

Posted: Wed Sep 21, 2016 3:51 am
Very well done livetv, your thought and perseverence was rewarding and admirable.

### Re: Hooking up the HC-SR04 ultrasonic distance sensor

Posted: Fri Nov 04, 2016 4:19 pm
Thanks for posting the circuit! I'm using an HC-SR04 in my sump basket in my basement, but I don't want to tie up the GPIO on my RPi anymore, so I was going to make this on a USB device, but then I thought a micro and an esp8266, but after reading your post, now I think I'll just use the esp8266 so it can take the reading and send the update to the sparkfun server and I'll use the micro for something else.