Shop OBEX P1 Docs P2 Docs Learn Events
Sharp IR Sensor Distance — Parallax Forums

Sharp IR Sensor Distance

stuartXstuartX Posts: 88
edited 2013-09-20 08:10 in Accessories
I have a question in reference to the Sharp IR Sensor. It states that it has a range of 10cm - 80cm. But when I hook it up, the maximum distance it detected was less than 10cm.
Am I doing something wrong?



I used the code below:
The IR is connected to pin 9
irDetectLeft VAR Bit
DO
irDetectLeft = IN9
DEBUG HOME, "irDetectLeft = ", BIN1 irDetectLeft
PAUSE 100
LOOP

Thanks ( in advance).

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2012-02-13 07:51
    I am assuming you are using a Basic Stamp and that the Stamp will show a 1 for any voltage higher than 2.5 volts. Looking at the data sheet linked to on the Parallax page for the Sharp sensor, you should see a one at about 8-8.5 cm. The Sharp sensor is really designed to be used with a analog to digital converter.

    Take a look at this page http://www.acroname.com/robotics/info/articles/sharp/sharp.html for a tutorial on the Sharp sensors. Other good information can be found at other sites found with Google.

    John Abshier
  • stuartXstuartX Posts: 88
    edited 2012-02-13 08:05
    Yes Sir. I'm using a BS2 and the debug will show a 0 or a 1 based on detection. According to the Parallax page it states 10cm - 80cm. Am I off base? Or is there a caveat to that number? I'll review the link to understand the sensor better. I'll chime back after reading with any questions.
    thank you Sir.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2012-02-13 09:52
    10-80 is the nominal (not exact) range that Sharp uses for this sensor.

    John Abshier
  • ZootZoot Posts: 2,227
    edited 2012-02-13 10:01
    The IR is an analog device -- you need to measure the VOLTAGE (not a binary high/low state) from the sensor and that voltage can be converted to a useful measurement. You would need to use an ADC between the detector and the micro to measure the voltage, or you may be able to get away with a variation of an RCTIME circuit.

    P.S. -- if you want to just have a binary (yes/no) detector with the IR ranger, you could run the output through a voltage divider into your micro pin -- adjusting the values in the voltage divider would let you set a threshold (corresponding to some distance between 10cm - 80cm) and get a "1" or "0" depending on whether the target is closer than the set value or further.
  • ercoerco Posts: 20,255
    edited 2012-02-13 13:39
    +1 to Zoot's post. It's an analog device, output ranges from 0- 2.5V, so normally you would use an A/D converter with a Stamp per http://www.parallax.com/dl/docs/prod/acc/SharpGP2D12Snrs.pdf

    The Stamp's input pin high/low transition voltage is ~1.4V, and you can use that like a simple comparator if you want a digital go/no-go input. You can use a voltage divider to scale voltages higher than 1.4 down. I have also used an 8-resistor voltage divider string with a Sharp sensor and 8 IO pins to resolve distance in 8 different zones for a balancing robot.
  • stuartXstuartX Posts: 88
    edited 2012-02-13 19:48
    Thanks for clearing that up. I'll try and get the A/D chip and start from there. thanks for the link(s).
    It's becoming more clear. It will be even more so, when I test it out with the chip.
  • stuartXstuartX Posts: 88
    edited 2012-02-14 10:40
    I'm ordering my A/D chip today.
  • ercoerco Posts: 20,255
    edited 2012-02-14 11:19
  • stuartXstuartX Posts: 88
    edited 2012-02-14 11:42
    I agree on the price. I would rather support Parallax since I look for and get support and advice from all you genius(s) who have had better experience and superb help.
  • stuartXstuartX Posts: 88
    edited 2012-02-19 20:19
    The A/D chip worked like a charm. Thanks for all the input.
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-07 11:13
    this is an old thread but the links don't work since the site has been updated.

    I was hoping to use the Sharp sensor in place of the PING))) sensor.
    then as I'm hooking up the sensor I see the comments about it being analog.

    I'm using the Homework board and I assume that I need this converter but honestly not sure how/why I'd set it up.

    I just want the sensor to recognize movement between 3-6 feet in front of the senor and if something is there send a signal. a simple yes/no
  • ercoerco Posts: 20,255
    edited 2013-09-07 12:39
    Sorry to say you're better off with a PING or a cheap ultrasonic sensor at 3-6 feet. Most of the Sharp IR sensors (from my experience) are better in the 3"-8" range. See http://forums.parallax.com/showthread.php/135899-Two-Buck-Ultrasonic-Sensor?highlight=buck+ultrasonic+sensor for a $2 sensor.
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-07 12:42
    found the data sheet on the A/D chip on an old instructables post. (link above wasn't working)
    but still not entirely sure what I'm doing. will do some more research until I hopefully hear back.
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-07 13:20
    erco wrote: »
    Sorry to say you're better off with a PING or a cheap ultrasonic sensor at 3-6 feet. Most of the Sharp IR sensors (from my experience) are better in the 3"-8" range. See http://forums.parallax.com/showthread.php/135899-Two-Buck-Ultrasonic-Sensor?highlight=buck+ultrasonic+sensor for a $2 sensor.

    i was just reading that thread. and wondering the same thing. no extra resistors needed if I just use the same PING))) code but use sep pins for puls out and puls in? (normal 4 pin mode rather than 3)
  • ercoerco Posts: 20,255
    edited 2013-09-07 16:47
    Yes sir. Very easy to use and incredibly cheap.

    Hmmm. A soda, or a sensor? Same price. :)
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-07 20:03
    Thank you so much

    ordered today
    I'm getting there, it's slow going but moving in the right direction
  • ercoerco Posts: 20,255
    edited 2013-09-11 14:01
    Prolly too late to add that OBC has these cheap ultrasonic sensors in his store for $3, with faster ship within the US than waiting 10+ days from China: https://www.tindie.com/stores/propellerpowered so if & when you need them in a rush, talk to Jeff (Old Bit Collector, or OBC).
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-12 05:49
    good info thanks
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-12 07:16
    OK
    got sensor in, ordered just 't one from amazon. yes i know it was more expensive but I didn't want to wait weeks for it and currently I just need 1.
    anyway,

    I have tried several different forms of code. i first modified my current setup (old PING))) sensor. I changed pulsout(trig) and pulsin(echo) to the correct pins for the new sensor.
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    
    
    
    
    '--------------Main Routine----------------------
    
    
    counter       VAR Word
    CmConstant    CON   2260
    InConstant    CON   890
    
    
    cmDistance  VAR   Word
    inDistance  VAR   Word
    time        VAR   Word
    
    
    
    
    
    
    Sense:
    
    
    cmDistance = 0    ' reset distance variables
    INDistance = 0
    time = 0
    
    
    DO
      PULSOUT 14, 5                 'sense distance via hc-sr04 echo pin
      PULSIN 15, 1, time             ' trigger pin is input pin
    
    
      cmDistance = cmConstant ** time          'conversion to inches and cm
      inDistance = inConstant ** time
    
    
      DEBUG HOME, DEC3 cmDistance, " cm"
      DEBUG CR, DEC3 inDistance, " in"
    
    
      PAUSE 100
    IF (inDistance = < 48) THEN                  ' if <48in then spray silly string
      GOSUB Shoot
    ENDIF                                        ' if >48 in then keep looking
      LOOP
    
    
    
    
    
    
    Shoot:
      FOR counter = 1 TO 75 ' Loop for ~1 second
        PULSOUT 12, 1000      ' Servo counterclockwise
        PAUSE 10
      NEXT
    
    
      FOR counter = 1 TO 150 ' Loop for 2 seconds
        PULSOUT 12, 750 ' Servo center
        PAUSE 5
      NEXT
        RLY1 PIN 0
     HIGH RLY1
     PAUSE 1000
     LOW RLY1
     PAUSE 1000
      PAUSE 30000                               ' pause 30 seconds then
      GOTO Sense                                ' return to sense mode
    

    i just get zero on the two lines and the everything fires like there is an object in the way.

    so I switch to
    [COLOR=#333333]' {$STAMP BS2}[/COLOR]
    [COLOR=#333333]' {$PBASIC 2.5}[/COLOR]
    
    [COLOR=#333333]#SELECT $STAMP[/COLOR]
    [COLOR=#333333]#CASE BS2, BS2E[/COLOR]
    [COLOR=#333333]' Trigger CON 5 ' trigger pulse = 10 uS[/COLOR]
    [COLOR=#333333]Scale CON $200 ' raw x 2.00 = uS[/COLOR]
    
    [COLOR=#333333]#ENDSELECT[/COLOR]
    
    [COLOR=#333333]time VAR Word[/COLOR]
    [COLOR=#333333]RawToIn CON 889 ' 1 / 73.746 (with **)[/COLOR]
    [COLOR=#333333]RawToCm CON 2257[/COLOR]
    [COLOR=#333333]rawDist VAR Word ' raw measurement[/COLOR]
    [COLOR=#333333]inches VAR Word[/COLOR]
    [COLOR=#333333]IsHigh CON 1 ' for PULSOUT[/COLOR]
    [COLOR=#333333]IsLow CON 0[/COLOR]
    
    [COLOR=#333333]DO[/COLOR]
    
    [COLOR=#333333]PULSOUT 10, 5[/COLOR]
    [COLOR=#333333]PULSIN 11, 1, rawDist[/COLOR]
    [COLOR=#333333]rawDist = rawDist */ Scale ' convert to uS[/COLOR]
    [COLOR=#333333]rawDist = rawDist / 2 ' remove return trip[/COLOR]
    [COLOR=#333333]inches = rawDist ** RawToIn [/COLOR]
    [COLOR=#333333]DEBUG HOME, "inches = ", DEC3 inches[/COLOR]
    [COLOR=#333333]PAUSE 100[/COLOR]
    
    [COLOR=#333333]LOOP
    

    and all i get is inches = 000
    [/COLOR]' PingTest.bs2
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    time VAR Word
    
    
    DO
    
    
      PULSOUT 14, 5
      PULSIN 15, 1, time
      DEBUG HOME, "time = ", DEC5 time
      PAUSE 100
    
    
    LOOP
    
    gives me time of 00001 or 00002 without any correlation to anything.


    finally I tried adding a 10k and 1k resistor as detailed here, and i get no improvement

  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-12 07:19
    how do I tell if I got a bad sensor?
    if I simply reset pulsout and pulsin to same pin and swap in my PING))) my setup works fine. I assume it's a bad sensor
    all I get is zeros
  • ercoerco Posts: 20,255
    edited 2013-09-12 21:47
    When in doubt, simplify. I chopped your code down and it worked with an HC-SR04 right away.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    DO
      PULSOUT 14, 5                'sense distance via hc-sr04 echo pin
      PULSIN 15, 1, W0            ' trigger pin is input pin
      DEBUG DEC W0,CR
      PAUSE 50
    LOOP
    

    I think you may have your I/O pins swapped, based on your comments. Connect pin 14 to TRIG on the sensor and pin 15 to ECHO. Once you get some numbers coming back, you can throw your math back in to calibrate for inches & cm.
  • ercoerco Posts: 20,255
    edited 2013-09-12 22:11
    And here's my code to use one pin mode, more like a PING.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' HC-SR04/SR05 ultrasonic sensor 1-pin usage
    ' P1 to TRIG on sensor
    ' connect 1500-1800 ohm resistor between TRIG and ECHO
    
    MAIN:
    HIGH 1              ' send brief pulsout to be cut short ASAP
    PULSIN 1, 1, W0     ' immediately switch pin to input
    W0=W0/72            ' calibrate to inches
    PAUSE 50
    DEBUG DEC W0,CR
    GOTO  MAIN
    
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-13 02:27
    man, i was hopeful.
    double checked pins and they are connected as you suggested. (when I was troubleshooting I think I tried them both ways honestly, not sure I updated the comments)

    but no.

    shortened code didn't work

    your code also a no go. guess I should have ordered more than one.

    thanks for going through the trouble of testing the code. I'll order some more and just use my PING))) till they arrive.

    Shawn
  • ercoerco Posts: 20,255
    edited 2013-09-13 12:15
    Shawn: PM me your address. I have more sensors than I'll ever use, happy to send you some tested units.

    eric
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-13 14:26
    Awesome

    and thank you so much!
  • thomasfamilythomasfamily Posts: 17
    edited 2013-09-16 16:42
    As a f/u. got the new sensors and they worked!

    so I got a bad one initially.

    Thanks for all the help

    Shawn
  • ercoerco Posts: 20,255
    edited 2013-09-20 08:10
    As a f/u. got the new sensors and they worked!

    That makes TWO somewhat different meanings for the abbreviation f/u . :)
Sign In or Register to comment.