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

HC-SR04 Ultrasonic Sensor

AdolfoAdolfo Posts: 2
edited 2020-01-29 16:45 in BASIC Stamp
I have tried different codes to operate the HC-SR04 sensor and it only reads zeros, the board I am occupying is BASIC Stamp HomeWork Board USB and the codes that I have used are the following:

Code 1:
' {$STAMP BS2}
' {$PBASIC 2.5}

time VAR Word

main:

DO
LOW 9
PULSOUT 9, 20
PULSIN 8, 1, time
time = time ** 2251
DEBUG CR, "Distance = ", DEC4 time, " cm"
PAUSE 5
LOOP

Code 2:
' {$STAMP BS2}
' {$PBASIC 2.5}

time VAR Word

main:

DO
PULSOUT 1, 100
PULSIN 1, 1, time
time = time ** 2251
DEBUG CR, "Distance = ", DEC4 time, " cm"
PAUSE 5
LOOP

Code 3:
' {$STAMP BS2}
' {$PBASIC 2.5}

DO
HIGH 1 ' start output "pulse" on P1
PULSIN 1, 1, W0 ' make P1 an input and measure return echo
W0=W0/72 ' convert to inches
DEBUG DEC W0,CR ' display data
PAUSE 50 ' delay stops ringing, reduces zeroes
LOOP

Code 4:
' {$STAMP BS2}
' {$PBASIC 2.5}

'*************************
' 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.
'*************************

I have used the two Echo and Trig pins separately, and also joining them with a resistance of approximately 1.8K. And I would like to know if anyone has a solution for this.

Thank you.

ultrasonic-sensor-HCSR04-2-247x233.jpg

Comments

  • PublisonPublison Posts: 12,366
    edited 2020-01-29 17:06
    Welcome to the forum!

    Give the code in this thread a try. I do not own a HC-SR04 to try

    https://forums.parallax.com/discussion/163555/ps2-hc-sr04-ultrasonic-sensor-code

    You could also find this under the search funtions, (upper right of the page):

    https://forums.parallax.com/search?Search=HC-SR04
  • Thank you very much for answering, but this code is the same as I had already tried, code 4
  • I have tried different codes to operate the HC-SR04 sensor and it only reads zeros, the board I am occupying is BASIC Stamp HomeWork Board USB and the codes that I have used are the following:

    Code 1:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    time VAR Word

    main:

    DO
    LOW 9
    PULSOUT 9, 20
    PULSIN 8, 1, time
    time = time ** 2251
    DEBUG CR, "Distance = ", DEC4 time, " cm"
    PAUSE 5
    LOOP

    Code 2:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    time VAR Word

    main:

    DO
    PULSOUT 1, 100
    PULSIN 1, 1, time
    time = time ** 2251
    DEBUG CR, "Distance = ", DEC4 time, " cm"
    PAUSE 5
    LOOP

    Code 3:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    DO
    HIGH 1 ' start output "pulse" on P1
    PULSIN 1, 1, W0 ' make P1 an input and measure return echo
    W0=W0/72 ' convert to inches
    DEBUG DEC W0,CR ' display data
    PAUSE 50 ' delay stops ringing, reduces zeroes
    LOOP

    Code 4:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    '*************************
    ' 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.
    '*************************

    I have used the two Echo and Trig pins separately, and also joining them with a resistance of approximately 1.8K. And I would like to know if anyone has a solution for this.

    Thank you.

    ultrasonic-sensor-HCSR04-2-247x233.jpg
  • Have you successfully used your HC-SR04 with any microcontroller?
    Do you own more than one HC-SR04?
    IIRC about 20% of the cheap HC-SR04 are dead on arrival.
    I'm pretty sure I have a couple HC-SR04 sensors and a Basic Stamp board. I'll try to test this out today. (If no one else beats me to it.)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2020-02-13 22:28
    Sorry but I haven't found my Basic Stamp board yet. I have found a few HC-SR04 sensors.
    I'll keep looking.

    Edit: I found it! Time to install Basic Stamp editor on this computer.
  • Isn't there a problem with using "PulseOut" and "Pause" with the HC-SR04?

    According to the data sheet pulseout isn't necessary because the HC-SR04
    generates it's own signal after the trigger pin is held high for a min of 10us,
    and pause is only capable of using 1ms increments.


    Bill M.
  • I just tried code 4 from above and it works with my hardware.
  • I was puzzling with this for a few minutes just yesterday. Most of the time, the HC-SR04 would send zero values... but on rare occasions, I actually got legitimate values... then it hit me like a ton of bricks!

    I was feeding power to the HC-SR04 from the power bus on my breadboard. Sure, it was 5v coming from a bench power supply, but... sometimes I could power the circuit on and it worked, and other times (most of the time) I powered the circuit on I got nothing but zeros. Reading the comments in this thread where they said there was a high failure rate on these sensors, I had almost decided to use a different method to solve my problem.

    But just for grins and giggles, at the top of my code I flipped one of the spare pins of the BS2 HIGH and used THAT (regulated) pin as a source of power before I entered the loop to began sending the PULSOUT to the trigger and reading the PULSIN from the signal line. BOOM! Works every single time I power it up.

    Sometimes the solution is the dumbest, most obvious thing I've simply overlooked.

  • @JerryHunter said:
    But just for grins and giggles, at the top of my code I flipped one of the spare pins of the BS2 HIGH and used THAT (regulated) pin as a source of power before I entered the loop to began sending the PULSOUT to the trigger and reading the PULSIN from the signal line. BOOM! Works every single time I power it up.
    Sometimes the solution is the dumbest, most obvious thing I've simply overlooked.

    I wouldn't recommend powering the HC-SR04 from an I/O pin. It sounds to me like you quite possibly didn't have a common ground with the BASIC Stamp board. Whether you're powering the sensor from the BASIC Stamp board VCC or another source, there should be a common ground with the BASIC Stamp board.

Sign In or Register to comment.