Ultrasonic ranging

From Mech
Revision as of 22:34, 11 February 2009 by Ankur Bakshi (talk | contribs) (→‎Code)
Jump to navigationJump to search

Original Assignment

In this project you will demonstrate that you can measure distances using an ultrasonic transmitter and receiver, at distances up to a few meters. While you could build from components (see, for example, this website), in this project you will use this pre-packaged PING))) ultrasonic sensor. Your results can be displayed on an LCD screen (see, e.g., C Example: Serial LCD or C Example: Parallel Interfacing with LCDs) or on a PC using the USB/RS-232 cable (see PIC RS232).

Overview

The Ping Ultrasonic Distance Sensor by Parallax has three pins. These pins are +5V, I/O, and Ground. In order to activate the sensor, a 5 microsecond 5V signal must be sent to the I/0 pin. The sensor then waits 750 microseconds before sending an ultrasonic wave. The sensor sets the I/O pin to high (+5V). When the sensor receives the reflected ultrasonic wave, it sets the I/O pin to low. The time that the I/O pin is high represents twice the distance of the object, as the wave must travel to the object and be reflected back. The sensor is rated for 2cm to 3m, though it will measure distances as far as 3.47m. After the sensor has received a signal there is a 200 microsecond refractory period. Due to the delay and the refractory period, one must wait 19.455 milliseconds between successive measurements (18.5 milliseconds to measure 3m + 5 microseconds to active the sensor + 750 microsecond delay + 200 microsecond refractory period). Charts diagramming the effects of elevation and angle of the object relative to the sensors can be found in the product documentation ([1]).

Ultrasonic Ranging.JPG

Circuit

In the circuit, the black wire represents ground. Pin C5 was used as the output pin responsible for activating the sensor. The external triggers RB0 and RB1 were used to identify when the I/O pin of the sensor was set to high and low respectively. Pin C6 is used for RS-232 communication to the PC. In order to setup LCD output, see C Example: Parallel Interfacing with LCDs.

Ultrasonic Ranging Schematic.jpg

Code

#include <18f4520.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=40000000)
#use rs232(baud=19200, UART1) // hardware UART; uses  RC6/TX and RC7/RX  

int16 timer;
float distance;

#INT_EXT             // designates that this is the routine to call when pin RB0/INTO changes state
void INT0isr() {
   SET_TIMER1(0);    // initialize timer 1 to 0
}

#INT_EXT1             // designates that this is the routine to call when pin RB1/INT1 changes state
void INT1isr() {
   timer=GET_TIMER1(); // records timer 1 value 
   distance=timer*.0000004*343; //scale timer
   //.0000004 = .8 microsecond increment of the timer divided by 2
   //Dividing by 2 is necessary because the time the wave takes to return to the sensor is twice the distance
   //343 is an approximation for the speed of sound (in m/s) at Room Temperature, a temperature sensor could be 
   //used to estimate the speed of sound using the formula V=331.5+(.6*TCelsius)
   if(distance >.01) {
      printf("%f meters\n",distance); //print out the Value in meters
   }
}

void main() {
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_EXT1);
   enable_interrupts(GLOBAL);
   ext_int_edge(0, L_TO_H);      // interrupt on INT0/RB0 pin, low to high transition
   ext_int_edge(1, H_TO_L);      // interrupt on INT1/RB1 pin, high to low transition
   
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // start timer T1, which counts in .8 microsecond increments
   
   while(true) {
      output_high(PIN_C5); //send signal to Ultrasonic Sensor for 5 microseconds
      delay_us(5);         
      output_low(PIN_C5);
      input(PIN_C5); // set pin to input, since the sensor has one pin for I/O, pin c5 can not be outputting a signal
      delay_ms(200); //changeable delay
}