Shop OBEX P1 Docs P2 Docs Learn Events
PS2 | HC-SR04 Ultrasonic Sensor Code — Parallax Forums

PS2 | HC-SR04 Ultrasonic Sensor Code

NachfolgerNachfolger Posts: 3
edited 2016-02-18 14:50 in BASIC Stamp
I hope this is the right location to do this? I'm new to the forum.

A huge problem I discovered was trying to find the correct code for the Ping Ultrasonic HC-SR04 Sensor. So I figured I will release it for anyone who isn't capable of figuring it out, since I wasn't for awhile. This is based off of the Parallax Ping Sensor Code.

If something stop working for whatever reason, let me know. Or if you need some help related to Ping (HC-SR04 Preferred) feel free to ask me.
' {$STAMP BS2}
' {$PBASIC 2.5}

' 2/14/2016 @1:37PM
' By Alec / Nachfolger [****@unviewed.net]

'*************************
' Pin connections

EchoPin    CON   0        ' The pin that the terminal "echo" is connected to. Pin 0 is default.
TrigPin    CON   1        ' The pin that the terminal "trig" is connected to. Pin 1 is default.

' Connect "GND" to ground / VSS
' Connect "VCC" or "5+" to positive / VCC

'*************************

' Don't edit below.
InConstant  CON   890
inDistance  VAR   Word
time        VAR   Word

'*************************
' The code below gets measurements.

beginning:                         ' Simply used for "GOTO beginning"
DO                                 ' Start of the loop.
  PULSOUT TrigPin, 5               ' Send a 0.005 second pulse to Ping.
  PULSIN EchoPin, 1, time          ' Listens for the echo.

'*************************
' Coversion (Time to inches)

  inDistance = inConstant ** time  ' Convert time into inches. [inches = inConstant x time]

'*************************
' If statement.

IF ((inDistance = 0)) THEN DEBUG CR, "Not connected properly!": GOTO beginning: ENDIF ' If the distance is "0" then something isn't connected. And goto the beginning to remeasure.

'*************************
' Display management

  DEBUG CR, DEC inDistance, "in"         ' Display result in the debug terminal.
  PAUSE 200                        ' A 0.2 second delay until next measurement.

LOOP                               ' Start over.
'*************************

Comments

  • Welcome to the forums!

    The 4 pin sensor seems like it is a little harder to use than the 3 pin Parallax Ping. I do not own a HC-SR04 to test, but if you say it works, that's okie dokie. :)
  • Publison wrote: »
    Welcome to the forums!

    The 4 pin sensor seems like it is a little harder to use than the 3 pin Parallax Ping. I do not own a HC-SR04 to test, but if you say it works, that's okie dokie. :)
    Thanks!

    One might say it's a bit more complicated, but that's only due to the lack of popularity. If more people used it, there would be a lot more projects based off of it.
  • ercoerco Posts: 20,257
    Nachfolger wrote: »
    but that's only due to the lack of popularity. If more people used it, there would be a lot more projects based off of it.

    Ahem... these are not exactly a secret:
    http://forums.parallax.com/discussion/135899/one-buck-ultrasonic-sensor/p1

    They are not PING quality, and there are defective units. But for a dollar, I can live with their limitations in many cases. You can hack them for one-pin operation, so they work just like a PING: put a 1.8K resistor between TRIG and ECHO for one-pin mode, and run one wire to TRIG.




  • erco wrote: »
    Nachfolger wrote: »
    but that's only due to the lack of popularity. If more people used it, there would be a lot more projects based off of it.

    Ahem... these are not exactly a secret:
    http://forums.parallax.com/discussion/135899/one-buck-ultrasonic-sensor/p1

    They are not PING quality, and there are defective units. But for a dollar, I can live with their limitations in many cases. You can hack them for one-pin operation, so they work just like a PING: put a 1.8K resistor between TRIG and ECHO for one-pin mode, and run one wire to TRIG.
    I wasn't aware of the 1-pin capability. That's awesome. And by no means did I say they were secret, but rather, less common.

    I posted this thread because I couldn't find any sample code on how to run the sensor. Maybe my search key-terms weren't good enough.
  • Thank you for your sample code. This was very helpful!
Sign In or Register to comment.