reading ultrasonic receve


Closed Thread
Results 1 to 19 of 19
  1. #1
    Join Date
    Feb 2013
    Posts
    11

    Default reading ultrasonic receve

    I am trying to read the ultrasonic receiver using the pic12f683 I thought about using the pulse out and pulsein command in picbasic pro but how do i change the internal oscillator frequency to drive it to 10us pulse to trigger the receiver

  2. #2
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    I am trying to get the pic to send out a 10us pulse in order to trigger the ultrasonic receiver and then convert it inches to turn on an led or to drive motors help is appreciated

  3. #3
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    The default clock on the 12F683 is 4MHz according to the datasheet found in REG 3-1 but still better to set it and define it along with turning OFF other peripherals not used:
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF
    
    OSCCON = %01100001  ' Osc 4MHz
    ADCON0 = 0          ' A/D module OFF
    CMCON0 = 7	    ' Analog comparators off
    GPIO = %00000000    ' Turn OFF all ports
    TRISIO = %00001101  ' Ports 0, 2 and 3 inputs, rest outputs (Example)
    
    DEFINE OSC 4        ' Declare FOR PAUSE and PAUSEUS statements
    But if using PBP3 then:
    Code:
    #CONFIG
        __CONFIG    _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF
    #ENDCONFIG
    
    OSCCON = %01100001  ' Osc 4MHz
    ADCON0 = 0          ' A/D module OFF
    CMCON0 = 7	    ' Analog comparators off
    GPIO = %00000000    ' Turn OFF all ports
    TRISIO = %00001101  ' Ports 0, 2 and 3 inputs, rest outputs (Example)
    
    DEFINE OSC 4        ' Declare FOR PAUSE and PAUSEUS statements
    Louie

  4. #4
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    here is the code i may not be understanding the pulse command the sensor is an hc-sr04 thanks for the help

    define osc 4
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4


    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word


    main:
    gosub distance
    low led
    while dist < 8
    high led
    'pause 500
    'low led
    'pause 500
    wend
    goto main

    distance:
    high trig
    pauseus 10
    low trig
    'pulsout trig,2 ' produces 10us pulse
    pause 50
    pulsin sens,1,dist 'reads echo pulse from sonar
    dist= dist/15 'convert to inches
    pause 100
    return

  5. #5
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    The PULSIN and PULSOUT commands at 4MHz have a 10us resolution per count.
    PULSIN will time out after 65535 counts or 655ms and return a value of 0 (zero) if it doesn't see the expected pulse.

    Make sure MCLR setting is OFF or pin 4 pulled HIGH with a 10K or something to keep it out of reset.

    Try this code but realize that if the PULSIN times out, the 0 (zero) will turn the LED ON too because it is <8.
    Code:
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4
    
    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word
    
    main:
    GOSUB distance    
        IF dist < 8 THEN
            HIGH led
        ELSE
            LOW led
        ENDIF
    GOTO main    
    
    distance:
        trig = 0            ' start output in LOW state
        PULSOUT trig,1      ' each count = 10us
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    If you don't want the LED ON when PULSOUT times out then I would use the SELECT CASE:
    Code:
    ADCON0 = 0      ' A/D module OFF
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4
    
    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word
    
    main:
    GOSUB distance    
        SELECT CASE dist
            CASE 0          ' PULSIN timed out and returns zero, turns LED OFF
                LOW led 
            CASE IS < 8     ' dist <8 inches but not zero, turns ON LED
                HIGH led 
            CASE ELSE       ' dist >8 inches turns LED OFF
                LOW led
        END SELECT
    GOTO main    
    
    distance:
        trig = 0            ' start output in LOW state
        PULSOUT trig,1      ' each count = 10us
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    Louie

  6. #6
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    thanks I will give it a try I have chuck hellebuyck's book on picbasic are there any other recommendations for it that could better benefit me .

  7. #7
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    ok I tried the case method but when the sensor detects anything within the required distance the led blinks instead of holding steady on

  8. #8
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    Quote Originally Posted by majorkuso View Post
    are there any other recommendations for it that could better benefit me .
    Chuck Hellebuyck's book is the best commercial basic-to-intermediate PBP book that I've seen. As far as other recommendations:

    1. The manual that comes with PB3! There is a wealth of knowledge in there, and it is put together very well. I still find something new every time I look in it--I keep a shortcut for it on the Windows task bar.

    2. The manual for whatever PIC you're using. It took me a while to learn how to understand the PIC manuals, it was a post in a precursor to this fine forum (email list) that clicked it for me. Which brings me to...

    3. This forum. Use Darrel's excellent improved search tool (here). Commit to reading every new post for a few weeks and get a feel for the common troubles many have, drink in the wisdom of the regular posters (not me), and follow some of the posted code as it gets improved.

    Best Regards,
    Paul
    The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.

  9. #9
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    the sensor works, but the led turns off and on rapidly instead of staying on what could be the problem

  10. #10
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    Hmmmmm.... try putting the trig = 0 before the main loop:
    Code:
        trig = 0            ' start output in LOW state
    
    main:
    GOSUB distance    
        SELECT CASE dist
            CASE 0          ' PULSIN timed out and returns zero, turns LED OFF
                LOW led 
            CASE IS < 8     ' dist <8 inches but not zero, turns ON LED
                HIGH led 
            CASE ELSE       ' dist >8 inches turns LED OFF
                LOW led
        END SELECT
    GOTO main    
    
    distance:
        PULSOUT trig,1      ' each count = 10us
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    Louie

  11. #11
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    I put the low trig before the main but it still blinks I wonder if the sensor is sending stray numbers when it starts the distance routine, is there a way I can sim the pic in multisim 11? I may try programming another pic and make sure it is not an internal error

  12. #12
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    You may right about the stray or false triggering as mentioned in the data sheet.
    Now try putting a little delay after the PULSOUT to give the device time to complete the transmit function before listening for an echo.

    Code:
    distance:
        PULSOUT trig,1      ' each count = 10us
        PAUSEUS 120      ' Wait for transmit pulse to complete
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    Louie

  13. #13
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    ok I tried pause from 50 to 200 milliseconds but it will not respond until I comment the pause command out or remove, it I am wondering if the pic itself is bad internally if so that could explain some erroneous behavior, i have a couple of pic 16f877A coming in and may use them to see if it clears up and then try these later on new 12f683 pics

  14. #14
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    I have found when I suspect a bad PIC, it turns out that it is just doing what I programmed it to do.


    ok I tried pause from 50 to 200 milliseconds but it will not respond until I comment the pause command out or remove
    Anyways, 50 to 200ms PAUSE's are way to long to wait since:
    Max distance measured is 4M or 157.48 inches
    And if I'm thinking straight, that works out to 157*15= 2362 counts
    Then with PUSLIN at 10us per increment is 2362*10us = 23.62ms

    So 23.62ms will be the maximum time measured for 4 meters. Unless I took a rong tern somewhere in the calculation.

    Do you have a scope to see how long it actually takes to complete the pulse transmission?
    Louie

  15. #15
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    I do I have two analogs and one digital I prefer the analogs , but don't have probes for them I will try to hook it up later and see what the timing is

  16. #16
    Join Date
    Jul 2003
    Location
    USA - Arizona
    Posts
    156


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    In the code that LinkMTech provided there should be no issue with the PAUSEUS or PAUSE commands.
    distance:
    PULSOUT trig,1 ' each count = 10us
    PAUSEUS 120 ' Wait for transmit pulse to complete
    PULSIN sens,1,dist ' will time out in 655ms if nothing received
    dist= dist/15 'convert to inches
    PAUSE 100
    RETURN

    END
    There is a minimum delay between the time the device reacts to the input (PULSOUT) and the time it provides an output (to be read by PULSIN). This time is defined by the 8 40kHz bursts it sends. The device does not raise its output high before the burst is sent (that is in the datasheet), so that means that you could use an approximate delay no longer than 200us between PULSOUT and PULSIN commands and still be ok. LinkMTech suggested 120us to allow for margin; that should be no problem. The other delay, the one which occurs after the PULSIN, represents how much to delay before the next time it tries the measurement again. This again should not be an issue.

    You certainly need a scope, but not to test your code (since your code looks fine, and it matches BASIC STAMP code and many others which work with modules like the PING, and Devantech's SRF04 modules. You need the scope to see if the module you have is doing what its datasheet claims it does. Also. maybe a pulldown (say 10-20k on the GPIO4 may help - perhaps the SR module does not pull to ground too well).

  17. #17
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    Well the problem was the breadboard I was developing on the configuration works fine on a different one so I am chalking it up to good ole conductivity. sometimes it is the simple things that get you

  18. #18
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    Ahhh, son of a garbanzo beans!
    Well glad to hear you got it fixed and on your way to becoming an expert.
    Louie

  19. #19
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    thanks for the help guys

Similar Threads

  1. Ultrasonic range meter
    By Crista in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2008, 18:01
  2. the ultrasonic SRF04 with pic16f88
    By tur_bot in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 20th March 2008, 17:24
  3. sensor ultrasonic 8051&PIC
    By hd_uni_pro in forum Schematics
    Replies: 1
    Last Post: - 13th September 2006, 12:58
  4. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 20:26
  5. SRF08 Ultrasonic Rangefinder
    By rad in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th July 2003, 21:01

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts