Shop OBEX P1 Docs P2 Docs Learn Events
QuickStart with cheap eBay ultrasonic sensor — Parallax Forums

QuickStart with cheap eBay ultrasonic sensor

birdmunbirdmun Posts: 5
edited 2014-07-06 09:43 in Propeller 1
I purchased a number of HC-SR04 sensors off of eBay. I attempted to use the Vin(pin 40) and Gnd to power the sensor. I was getting 4.85+ volts at pin40. I attempted to use an object meant for the 4 pin ultrasonic sensors( http://forums.parallax.com/showthread.php?135899-Four-Buck-Ultrasonic-Sensor&p=1062380&viewfull=1#post1062380 ), but, I could not get a response. I wrote my own object and added a number of debug statements to tell me where in the program the function was. I could get as far as listening.
Thinking my issue was voltage, I managed to gain the use of a wall wart that could supply 5v. When I hooked it up a number of the LEDs on the QS board lit up in varying brightnesses. I am at a loss. I will gladly provide more information, when I know what it is anyone assisting me would like.

Comments

  • RaymanRayman Posts: 14,658
    edited 2012-06-12 04:00
    Looks like the output signal is 5V... You probably want a 10k resistor between that output and a Prop Pin.
    Otherwise, you could damage your Propeller.
  • birdmunbirdmun Posts: 5
    edited 2012-06-12 05:44
    You make a good point. I did add a 1k resistor between the Echo pin and the Prop pin. This was based on both, information in the object I originally tried using, and, information that I believe I read on one of the forum posts here.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-12 06:20
    birdman,

    I bet this is a power supply issue.

    The quickest way to fix this type of power supply issure is to use a battery pack with the QS. Looking back at the post you linked to, I think I remember using four AA NiMH batteries to power the QS and sensor.

    I find devices usually work better when powered by a battery pack than using power from the QS's USB power. I can get away with using small servos connected to the QS's Vin if I add a couple of capacitors to Vin when using USB for power. I have a 1000uF cap and sometimes a second smaller cap on my QS boards when powering additional items from USB power.

    I believe I purchased ten of those sensors myself. Two of the ten didn't work so there's a chance you have a bad unit. (I see you have several units yourself, so that's probably not the problem here.)

    I see I'm the one who used a 1K resistor. I generally use 10K resistors with 5V inputs. I'm not sure if I tried any resistors between 1K and 10K or not. As I mentioned in the thread, the Prop couldn't read from the sensor with a 10K resistor on the line. One really shouldn't use less than about 3K (I think the exact value is around 2.7K) when using 5V with a Prop. But lots of the old Prop threads mention a 1K resistor so I think 1K will work in a pinch but you shouldn't design a product with less the 2.7K ohm on any 5V line.

    I don't suppose you have access to a oscilloscope of a logic analyzer? It's always helpful to see if the lines are doing what you think they're doing. Have you tried more than one of your sensors with the code?
  • RaymanRayman Posts: 14,658
    edited 2012-06-12 06:36
    Sounds like Duane has good advice for you.
    Regarding your QS leds... Are you sure it's not from your hands touching the I/O pins on the 40-pin headers?
    If the lights are flashing without any metal being touched, then I think something is very, very wrong...
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-12 06:37
    birdman,

    Looking back at my code, I see I was using a serial 7-segment display for output. It sounds like you've already modified the code to use a terminal window. I'll do likewise and make sure it behaves the same with a terminlal as the display.

    I'll also see if I can power a unit from the QS's Vin line.

    Edit: I see the 1K resistor is just on the trigger line. The echo uses a 10K resistor. (At least that's what the comments in the demo object says.)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-12 10:01
    birdman,

    I modified the code to use PST as the serial driver.

    This is the output I get from the terminal window.
       Ultrasound Demo
       Range = 1820 mm
     
    


    I cleaned up the object a bit and updated the schematic.
      ┌───────────────────┐
      │┌───┐         ┌───┐│    Connection To Propeller
      ││ ‣ │  SR04   │ ‣ ││    Remember SR04 Requires
      │└───┘         └───┘│    +5V Power Supply
      │ Vcc Trig Echo Gnd │
      └──┬────┬────┬───┬──┘
     5V  │    │    │   │
        │ 1K  10K   │           
     └───┘    │    │   │
              │    │   │
     trigger ┘    │   │
        echo ─────┘   │
                       
    


    Since the trigger pin is an output it should be fine with just a 1K ohm resistor. The echo pin in the input pin and it works fine with a 10K resistor.

    I powered my SR04 from the QuickStart's Vin and Gnd pins without adding any extra capacitors. The QuickStart was powered by the USB connection.

    If the unit you have doesn't work with this code and setup, I'd suggest trying one of your other sensors in case your first one doesn't work.
  • NWUpgradesNWUpgrades Posts: 292
    edited 2012-06-12 12:18
    Can these also be used with a BoeBot BS2?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-12 12:45
    NWUpgrades wrote: »
    Can these also be used with a BoeBot BS2?

    I'm almost positive they can be used with a BS2. I think the code for the Ping sensor could be modified to use two pins instead of one.

    If you get one of these sensors and need help just let us know. If no one else has code to use these with a BS2, I'll write some. Based on the number of people who have purchased these, I'd be very surprised if there isn't already code available.
  • tomchapstomchaps Posts: 2
    edited 2012-09-19 23:18
    Duane Degn wrote: »
    I'm almost positive they can be used with a BS2. I think the code for the Ping sensor could be modified to use two pins instead of one.

    If you get one of these sensors and need help just let us know. If no one else has code to use these with a BS2, I'll write some. Based on the number of people who have purchased these, I'd be very surprised if there isn't already code available.

    Hi, I was kindly given a copy of the What's a Microcontroller kit with the BS2 Homework board at the NY Maker Faire by a Parallax employee. Awesome! My son and I have worked our way through most of the book, and can do some stuff with it despite our complete lack of programming or electronics experience. However, we bought one of these HC-SR04 sensors to play with, and it has defeated my rudimentary programming skills on the BS2. I have looked at Arduino code and PING sensor code, but can't figure out exactly how to translate it.

    In short, I would LOVE to take you up on that offer to write BS2 code.

    I've simply set pin 10 to TRIG, pin 11 to ECHO, and hit 10 with a PULSOUT 10, 5 (because it's in increments of 2uS, right?) then PULSIN 11, echo (with echo as a variable measuring the time). I get... numbers, but they seem to be unrelated to distance...
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-09-19 23:42
    tomchaps wrote: »
    In short, I would LOVE to take you up on that offer to write BS2 code.

    I think erco has some code to use these sensors with the BS2. I just asked him about it in another thread.

    If erco (or anyone else) doesn't have some BS2 code, I'll try changing some BS2 Ping code to work with these sensors.

    In the meantime, how about posting the code you wrote? (Tell us what the output was also.)

    See Phil's tutorial about posting code to the forum.

    attachment.php?attachmentid=78421&d=1297987572
  • tomchapstomchaps Posts: 2
    edited 2012-09-20 15:08
    I got it to work! I'm still not sure what was wrong, but I found some Ping code on the forums that was easy to adjust to the separate trigger and echo pins.

    Here is the skeleton of what worked:
    ' PingTest.bs2
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    time VAR Word
    
    DO
    
      PULSOUT 10, 5
      PULSIN 11, 1, time
      DEBUG HOME, "time = ", DEC5 time
      PAUSE 100
    
    LOOP
    

    Here is what I had been trying, unsuccessfully:
    ' testing the HC-SR04
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' i/o 10 is trigger
    ' i/o 11 is echo
    
    echo VAR Byte
    range VAR Byte
    
    DO
    
    PULSOUT 10, 5
    
    PULSIN  11, 1, echo
    
    range = (echo*10)/148
    
    DEBUG ? range, CR
    
    echo = echo/2
    
    PAUSE 250
    
    LOOP
    
    END
    

    Not really sure what the issue was, but now that I'm getting the echo readings and they seem accurate, I can build around it.

    Thanks!
  • DavidNymanDavidNyman Posts: 52
    edited 2013-06-10 18:03
    I hacked the Ping code and Tomchaps code and came up with this for the HC-SR04
    The comments may be off a bit off but the code seems to work well.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    #SELECT $STAMP
    #CASE BS2, BS2E
    ' Trigger CON 5 ' trigger pulse = 10 uS
    Scale CON $200 ' raw x 2.00 = uS

    #ENDSELECT

    time VAR Word
    RawToIn CON 889 ' 1 / 73.746 (with **)
    RawToCm CON 2257
    rawDist VAR Word ' raw measurement
    inches VAR Word
    IsHigh CON 1 ' for PULSOUT
    IsLow CON 0

    DO

    PULSOUT 10, 5
    PULSIN 11, 1, rawDist
    rawDist = rawDist */ Scale ' convert to uS
    rawDist = rawDist / 2 ' remove return trip
    inches = rawDist ** RawToIn
    DEBUG HOME, "inches = ", DEC3 inches
    PAUSE 100

    LOOP
  • 4j1a4j1a Posts: 1
    edited 2014-07-06 08:58
    I ran DavidNyman's and tomchaps code on my parallax boe-bot.
    The only thing that happens, is that the RX blinks on the Debug Terminal,
    and on the bottom half which is blue, it reads: inches = 0000
    On the Boe-Bot, the Red Led is flashing Pink.

    My goal is to get a read out on the Parallax LCD, in inches I would guess..

    I would appreciate it immensely if someone could help.
  • PublisonPublison Posts: 12,366
    edited 2014-07-06 09:43
    4j1a wrote: »
    I ran DavidNyman's and tomchaps code on my parallax boe-bot.
    The only thing that happens, is that the RX blinks on the Debug Terminal,
    and on the bottom half which is blue, it reads: inches = 0000
    On the Boe-Bot, the Red Led is flashing Pink.

    My goal is to get a read out on the Parallax LCD, in inches I would guess..

    I would appreciate it immensely if someone could help.

    How do you have it connected. A picture would be a great help.

    And which sensor are you using? ( be specific, and if you have a link, that would help).
Sign In or Register to comment.