Need help setting up my ultrasonic distance sensor


Closed Thread
Results 1 to 19 of 19
  1. #1
    Join Date
    Nov 2009
    Location
    London
    Posts
    251

    Default Need help setting up my ultrasonic distance sensor

    Hi, I just bought my Ultrasonic distance sensor (Datasheet attached).
    I would like some help, may be few lines of code to give me a starting point for how to set it up for my purpose which is to sense the trigger pin going low when distance is less than 3 meters. I will be using 16F877A with this distance sensor which can take care of few other jobs as well.
    I would also like to know how I can do the similar thing i.e. take readings of PWM output and measuring the pulse to know the distance. Again few lines of code in Pickbasicish will be better than instructions in plain english. Thanks
    Attached Images Attached Images

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Take a look at this page,

    http://www.parallax.com/dl/docs/prod...tance28015.pdf

    Regards
    Mark

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    You need to set the mode and distance threshold by sending the correct command serially to the sensor. You want 9600 baud, no parity, one stop bit. The SEROUT section of the PBP manual will tell you how to do this, and the PDF you provided in your OP will tell you what commands you need to send.

    The best way to get help here is to try it, and if what you try doesn't work, post the code here and i'm sure lots of help will be forthcoming.
    Last edited by Kamikaze47; - 20th June 2010 at 06:32.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  4. #4
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Here goes my code for 16F676. Used this IC because I can program it in PICKIT2 & also I have jumpered my sensor around it.
    Code:
    Include "modedefs.bas"
    DEFINE     NO_CLRWDT    1
    DEFINE          OSC     4       ' OSCCON defaults to 4MHz on reset
    DEFINE OSCCAL_1K 1
    
    @ __Config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN & _CP & _CPD
    
    PAUSE 50
    Tx Var PortA.1
    Rx Var PortA.0
    power var PortC.1
    	TRISA = %000001
    	TRISC = %000001
    	CMCON = 7
    	ANSEL = 0
          OPTION_REG = %10000111  ' RAPU = off, PS 1:256 to TMR0
    	PORTA=0
    	PORTC=0
    	' setup iZCD to interrupt on change
    	INTCON = 0
    
    ; PortC.0 = PWM Receive ; ; ; ; ; PortC.1 = Power_On ; ; ; ;  PortA.0 = Receive Pin ; ; ; ; PortA.1 = Transmit Pin ; ; ; ; PortA.2=Trigger Pin
    
    main:
    High power
    pause 100
    Serout Tx, N9600, ["0x44+0x00+160+ ???"]	;	To set lower threshold distance to appx 3 meters. What should be the SUM & how do you calculate it?
    Serout Tx, N9600, ["0x44+0x01+250+ ???"]	;	To set upper threshold distance to appx 5 meters
    Serout Tx, N9600, ["0x44+0x02+0xaa+ ???"] ; 	To set operation mode to autonomous mode
    
    	if PortA.2=0 then
    		High PortC.2 ; led 1
    	else
    		high PortC.5 ; led 2
    	endif
    goto main
    end
    1) How do you calculate the SUM? any example please
    Once I get to the next point, I will update here if I get stuck. Thanks

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    try something like this (taking your 1st serout line as an example):

    Serout Tx, N9600, [$44,$00,$A0,$E4]

    $A0 is just 160 in hex.

    To work out the sum: $44+$00+$A0=$E4

    If the sum is bigger than $FF then you take just the lower byte. e.g. if the sum came to $01A2, then you just use $A2.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Hi, I tried this:
    Code:
    main1:
    High power
    Pause 100
    Serout Tx, N9600, ["0x44+0x00+0xA0+0xE4"] ;To set lower threshold distance to appx 3 meters
    Pause 100
    Serout Tx, N9600, ["0x44+0x01+0xD1+0xFE"] ;To set upper threshold distance to appx 5 meters
    Pause 100
    Serout Tx, N9600, ["0x44+0x02+0xaa+0xF0"] ; To set operation mode to autonomous mode
    main:
    	if PortA.2=1 then
    		high PortC.2
    	else
    		 toggle PortC.5
    	endif
    pause 50
    goto main
    end
    And also this:
    Code:
    Serout Tx, N9600, [$44,$00,$A0,$E4] ;To set lower threshold distance to appx 3 meters
    Pause 100
    Serout Tx, N9600, [$44,$01,$D1,$FE] ;To set upper threshold distance to appx 5 meters
    Pause 100
    Serout Tx, N9600, [$44,$02,$aa,$F0] ;To set operation mode to autonomous mode
    but no luck - any advise?

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    The question is ...

    did you use a RS232 chip on YOUR board ??? ( like in the datasheet scheme )

    more ...

    Could you , please , attach YOUR scheme ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    The question is ...

    did you use a RS232 chip on YOUR board ??? ( like in the datasheet scheme )

    more ...

    Could you , please , attach YOUR scheme ???

    Alain
    Hi, here is my hardware photo, just my sensor jumpered to my PICKIT2 in the following configuration:
    ; PortC.0 = PWM Receive ; ; ; ; ; PortA.0 = Receive Pin ; ; ; ; PortA.1 = Transmit Pin ; ; ; ; PortA.2=Trigger Pin
    PWR_ON is directly connected to 5V on the PICKIT2
    Also I am using TTL I/O, I have arranged the jumpers accordingly.
    Attached Images Attached Images   

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    OMG ...

    I also think you are exceeding the Pickit sourcing authorized current ... Especially when " pinging " ...

    Alain
    Last edited by Acetronics2; - 22nd June 2010 at 16:47.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    OMG ...

    I also think you are exceeding the Pickit sourcing authorized current ... Especially when " pinging " ...

    Alain
    Oops! will try everything again with seperate supply and update here. May be this is the reason that it is not working.

  11. #11
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    OK, I tried with this approach and it works, but as you will see in the code, even though the object is under 5 meters away (for eg. 4meters), PORTC.2 goes high & does not toggle. Is there something wrong with my calculation. It is working fine upto 1 meter. LED connected to PortC.2 does alterate it's toggling speed depending upon how far is the object under 1 meter. My latest code goes like this:
    Code:
    ; Phase2: RA0=TRIG (OUTPUT) ;;;;RA1- PWM (INPUT)
    w1 var word
    
    pause 500
    main1:
    w1=0
    High PortA.0
    pause 1
    Low PortA.0
    pause 1
    High PortA.0
    
    main:
    Pulsin PortA.1,0,w1
    if w1>500 or w1=0 then 
    		high PortC.2
    	else
    		toggle PortC.2
    			pause w1
    	endif
    goto main1
    end

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Everything works as written ...

    W1 = 500 is a distance of 1 meter ...

    ...

    try 2500 instead ...

    and note no measure will send back ... 5000 Pulsin units ( 50 000 µs )

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  13. #13
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    Everything works as written ...

    W1 = 500 is a distance of 1 meter ...

    ...

    try 2500 instead ...

    and note no measure will send back ... 5000 Pulsin units ( 50 000 µs )

    Alain
    Hi, it works fine with 2500. Can you please explain how you calculated 2500? Thanks

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    500 multiply by 5 meters...
    Dave
    Always wear safety glasses while programming.

  15. #15
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Megahertz View Post
    Hi, it works fine with 2500. Can you please explain how you calculated 2500? Thanks
    Being Honest ...

    1) read Datasheet Page 3 ...
    4: PWM - PWM Output 025000USEvery 50US represent 1cm
    2) read PbP Manual page 133 ...

    The resolution of
    PULSIN is dependent upon the oscillator frequency. If
    a 4MHz oscillator is used, the pulse width is returned in 10us increments.

    3) The distance is 5 meters = 500 cm

    from 1) and 3) ...

    4) Pulse will be 500cm * 50µs/cm = 25 000 µs

    from 4) and 2) ...

    @4 Mhz Pulsin will return 25 000 / 10 = 2 500 units

    This is the mathematical explanation of zeu thing

    Alain

    PS@ Dave

    Don't you think I should add " Need Help " in my signature validity conditions ???
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  16. #16
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    PS@ Dave

    Don't you think I should add " Need Help " in my signature validity conditions ???
    Yup, need help understanding data sheet.

    I think Megahertz is doing fine, sometimes know where to look is a bit different than knowing what to look for.

    I just spent parts of two days trouble-shooting a machine. Tried this and that, read the manual, tried other stuff, back to the manual.....
    Then the great thought hit me... I wonder if that cable on the floor should be plugged into something
    Dave
    Always wear safety glasses while programming.

  17. #17
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Yup, need help understanding data sheet.

    I think Megahertz is doing fine, sometimes know where to look is a bit different than knowing what to look for.

    I just spent parts of two days trouble-shooting a machine. Tried this and that, read the manual, tried other stuff, back to the manual.....
    Then the great thought hit me... I wonder if that cable on the floor should be plugged into something
    Hi, Dave

    This episode of our tremendous adventures lits a little light in my head ...

    Would be a nice thing to get the Holy manual opened at the convenient page just clicking for "Help" in our IDEs ... ( say " mouseover " + a keystroke on KB )

    Always useful to have a look to function parameters ...

    Hope IDE Creators read that post ... ( thoughts to my overseas friends ...)

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  18. #18
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    At one time Jeremy and I was working on a code editor with some automated fill functions but we did not get very far before other things put it on the back burner. We was using a flavor of Basic.

    Then I sorta started a cross platform editor in Python but the same thing happened, got to busy to work on it.

    Normet has the "fineline" editor he working on.

    I wonder how many around here would be interested in an open source, cross platform, community project?
    Dave
    Always wear safety glasses while programming.

  19. #19
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    I wonder how many around here would be interested in an open source, cross platform, community project?
    I saw one nice working " IDE " ...

    Called PICabc ... limited to 16F84 and assembler ...

    http://www.abc-micro.co.rs/

    ftp://ftp.chatnfiles.com/Microforum/...ABC/PICABC.EXE ( demo ...)

    but the concept is rather interesting ...

    to be honest, I must say the MiKroElektronika IDE is really handy here ...

    I know one other IDE " on the rail " but was asked not to talk about it ...

    Alain
    Last edited by Acetronics2; - 26th June 2010 at 14:22.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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