HC-SR04 Ultrasonic Sensor
Adolfo
Posts: 2
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:
Code 2:
Code 3:
Code 4:
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.
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.
Comments
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
Code 1:
Code 2:
Code 3:
Code 4:
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.
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.)
I'll keep looking.
Edit: I found it! Time to install Basic Stamp editor on this computer.
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.