Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Ultrasonic Range Finder. Is this idea feasible?

Status
Not open for further replies.
Hell no! The ADC is far too slow! There are much faster solutions...

You will need some sort of amp and filter circuit on the receiver, so the moment it get a signal, it will go low, or go high (depends on the circuit) - but this is crucial for generating accurate samples!

Now that's out of the way, creating a 40Khz signal, and waiting for the response is not hard at all with a little thought, Consider this;

Code:
Device = 16F876
XTAL = 20

Symbol Distance_Per_Instruction = (1 / (XTAL / 4)) * 340.29          ' This is the distance per instruction in micro meters (1 instruction = 68.058uM @ 20Mhz XTAL)

Symbol Timer1 = TMR1L.WORD                ' A special way of addressing both TMR1L and TMR1H with one register.
Symbol TMR1_Overflow = PIR1.0             ' TMR1 overflow flag.
Symbol TMR1_Enabled = T1CON.0             ' Enables TMR1 to start incrementing.

Symbol Input_Pin = PORTA.0                ' Defines the receivers Pin.

Dim Loops As Byte
Dim Insructions_Taken As Float
Dim Distance As Float

ALL_DIGITAL = True                        ' Make all pins digital I/O's.

Low PORTB.0                               ' Make both outputs to the transmitter
Low PORTB.1                               '  outputs, and set them low.
Input Input_Pin                           ' Make the input pin an input.

TMR1_Enabled = 0

T1CON.1 = 0                               ' 0 = TMR1 Increments on Internal clock. 
T1CON.4 = 0                               ' TMR1 Prescale Bits, 00 = 1:1.
T1CON.5 = 0                               '  

Main_Loop:
	
	Timer1 = 0                            ' Reset TMR1.
	Insructions_Taken = 0                 ' Reset the Cycles_Taken register.
	
    Gosub Create_Ping                     ' Create a 40Khz Signal.

    TMR1_Enabled = 1                      ' Allow TMR1 to begin incrementing.

    Repeat
	    If Input_Pin = 0 Then             ' Capture the input going Low (echo has been recieved).
	        TMR1_Enabled = 0              ' Disable TMR1.
                Insructions_Taken = Timer1    ' Read the current contents of TMR1.
		Break                         ' Leave the Repeat Until loop.
	    EndIf
    Until TMR1_Overflow = 1               ' If TMR1 rolls over from 65535 to 0, then exit
		
	TMR1_Enabled = 0

	If Insructions_Taken = 0 Then         ' Check if Insructions_Taken has been updated.
	   Print At 1, 1, "Out Of Range    "     ' If not then a time out occurred - echo is out of range.
	Else
	   Distance = Insructions_Taken * Distance_Per_Instruction / 1000      ' Calculate the distance in micro meters, then scale it to mm.
	   Print At 1, 1, "Dist = ", DEC1 Distance, " mm   "                      ' Display on the LCD.
        EndIf
	
	DelaymS 100                           ' 100mS between checks
	
	Goto Main_Loop


Create_Ping:

        Loops = 8                         ' Number of 40Khz cycles to produce.
		
		Repeat                             
                PortB = %00000001         ' First half of cycle.
                DelayuS 10                ' Delay of 10uS.
                PortB = %00000010         ' Second half of cycle.
                DelayuS 10                ' Delay of 10uS.
				
                Dec Loops                 ' That one whole cycle, dec the counter.
        Until Loops = 0                   ' And repeat until its finished all cycles.

		Return


Written in Proton PIC Basic

I wrote that based on the Project from the Proton User site found **broken link removed**.

Within that file is a great receiver circuit, and a gold mine of ultrasonic fundamentals
 
Last edited:
I should point out that the above program is accurate from 0.06 to 4.4 Meters.

To improve the response time so that you can measure objects closer than 60mm (6cm) than just reduce the number of cycles produced in the 40Khz signal routine. (Its set to 8 at the moment, a value of 4 would allow detection of objects ~30mm away)
 
Over 30 years ago the Polaroid instant camera used ultrasonics for its auto-focus.
 
audioguru said:
Over 30 years ago the Polaroid instant camera used ultrasonics for its auto-focus.

yes, and you can find them in thrift shops for a couple of dollars. there are several web sites that show how to use the guts for ranging.

on the shortest distance, you are going to be limited by several factors: ping length and the amount of ringing that the transmitter does after you stop driving it. I was seeing 4-6 cycles before it decayed by 10db or so (yeah, I eyeballed it on the scope). You need to turn off the receiver while transmitting and for some of the decay as well otherwise you will false on the transmit. Speed of sound is around 340 m/S. At 40khz, each cycle is 1.7cm of distance (round trip). If you use 6 cycles for your ping and 4 cycles for the ring, you will not be able to detect an echo in less than 10 cycles from the start of your ping. or 8.5cm

there may well be some decent techniques for canceling the transmit signal at the receiver and for damping the ringing but frankly, for that short a distance, I'd just use IR.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top