A Cheap Ultrasonic Range Finder




HOW DOES IT WORK & CIRCUIT SCHEMATIC

Everybody knows the speed of the sound in the dry air is around 340 m/s. Send a short ultrasonic pulse at 40 Khz in the air, and try to listen to the echo. Of course you won’t hear anything, but with an ultrasonic sensor the back pulse can be detected. If you know the time of the forth & back travel of the ultrasonic wave, you know the distance, divide the distance by two and you know the range from the ultrasonic sensor to the first obstacle in front of it.

A Cheap Ultrasonic Range Finder

Here we use an ultrasonic piezzo transmitter with its receiver, they are very efficient, easy to find and quite cheap.

First, we have to send the pulse : it is easy to get a 40 Khz pulse from a PIC PWM output. You can drive an ultrasonic transmitter directly from the PIC output, but the sense range will not exceed 50 cm. Using a transistor and a resonator circuit, the ultrasonic transmitter will get around 20 volts and the sense range will be extended up to 200 cm.

Second we have to sense the echo : the piezzo receiver can provide a few dozens of millivolt, this will be enough for a PIC ADC with 4 mV resolution without extra hardware.

Click on schematic to enlarge

C1 is a decoupling capacitor

The PWM pulse from the RC2 pin of the PIC drives the T1 transistor base through R1 resistor

A 330 µH inductor is added in parallel to the piezzo ultrasonic transceiver, to form a LC resonnator, the D1 diode protects T1 from reverse voltage.

The ultrasonic receiver is directly connected to the RA1 pin of the PIC (ADC channel number 1), with R3 in parallel as impedance adaptator.

A Cheap Ultrasonic Range Finder schemetic

The prototype board

<- Component side

Solder side ->

Take care to align as best as possibe the transmitter with the receiver




SOURCE CODE

Here is the mikroC source code :

/*
* file         : sonar.c
* project      : Simple UltraSonic Range Finder
* author       : Bruno Gavand
* compiler     : mikroC V6.2
* date         : september 30, 2006
*
* description  :
*      This is a basic ultrasonic range finder, from 30 to 200 centimeters
*
* target device :
*      PIC16F877A with 8 Mhz crystal
*      or any PIC with at least one ADC and PWM channel
*
* configuration bits :
*      HS clock
*      no watchdog
*      no power up timer
*      no brown out
*      LVP disabled
*      data EE protect disabled
*      ICD disabled
*
* see more details and schematic on http://www.micro-examples.com/
*//********************
* MACRO DEFINITIONS
********************/
/*
* ultra sonic pulse length in microseconds
*/
#define PULSELEN        300

/*
* circular buffer size for samples averaging
*/
#define BUFSIZE 10

/*
* LCD PORT
* EasyPic2, EasyPic3 : PORTB
* EaspyPic4 : PORTD
*/
#define LCDPORT PORTD
#define LCDTRIS TRISD

/*******************
* GLOBAL VARIABLES
*******************/
unsigned char   outOfRange ;            // out of range flag : set when no echo is detected

unsigned int    buf[BUFSIZE] ;          // samples buffer
unsigned char   idx = 0 ;               // index of current sample in buffer

/*****************************************
* INTERRUPT SERVICE ROUTINE
* This ISR handles TIMER1 overflow only
*****************************************/
void    interrupt(void)
{
if(PIR1.TMR1IF)                                       // timer1 overflow ?
{
outOfRange = 1 ;                              // set out of range flag
PIR1.TMR1IF = 0 ;                             // clear interrupt flag
}
}

/************
* MAIN LOOP
************/
void    main()
{
ADCON1 = 0 ;            // enables ADC

TRISA = 0xff ;          // PORTA as inputs
PORTA = 0 ;

TRISC = 0 ;             // PORTC as outputs
PORTC = 0 ;

// TIMER1 settings
T1CON = 0b00001100 ;    // prescaler 1:1, osc. enabled, not sync, internal clk, stopped

#ifdef   LCDPORT
// init LCD
Lcd_Init(&LCDPORT) ;            // use EP2/3/4 settings
Lcd_Cmd(Lcd_CLEAR) ;            // clear display
Lcd_Cmd(Lcd_CURSOR_OFF) ;       // cursor off

Lcd_Out(1, 1, “UltraSonicRanger”) ;
Lcd_Out(2, 5, “cm”) ;
#endif

 

For more detail: A Cheap Ultrasonic Range Finder 




Leave a Comment

*
= 3 + 4

Read previous post:
A Programmable Optical Slave Flash Trigger for Digital Cameras with Processor PIC 12F675

The synchronizer ensures a contact-free, optical synchronization of external photographic flashguns. The triggering of the external flash is derived from...

Close
Scroll to top