Measure Distance with UltraSonic Distance Sensor (PWM O/P)
The “ECHO”(PWM) ,Ultrasonic Distance Sensor from Rhydolabz is an amazing product that provides very short (2 cm) to long-range (400 cm) detection and ranging. The sensor provides precise,Stable non-contact distance measurements from about 2 cm to 400 cm with very high accuracy. Its compact size, higher range and easy usability make it a handy sensor for distance measurement and mapping. The board can easily be interfaced to microcontrollers where the triggering and measurement can be done using Single I/O pin. The sensor transmits an ultrasonic wave and produces an output pulse that corresponds to the time required for the burst echo to return to the sensor. By measuring the echo pulse width, the distance to target can easily be calculated.
This UltraSonic Distance Sensor is perfect for any number of applications that require you to perform measurements between moving or stationary objects. Naturally, robotics applications are very popular but you’ll also find this product to be useful in security systems or as an infrared replacement if so desired.This is extreamily suitable for Roboic Application, since it need only one I/O pin and very fast. The “ECHO”(PWM) does not require any ADC or USART to meassure the distance. Since it is very stable ,the “ECHO”(PWM) Ultrasonic sensor module can be used for Micromouse application instead of IR sensor
Features:
- Professional EMI/RFI Complaint PCB Layout Design for Noise Reduction
- Range: 2 cm to 400 cm
- Accurate and Stable range data
- Data loss in Error zone eliminated
- Modulation at 40 Khz
- Mounting holes provided on the circuit board
- Triggered externally by supplying a pulse to the signal pin
- 5V DC Supply voltage
- Bidirectional TTL pulse interface on a single I/O pin can communicate with 5 V TTL or 3.3V CMOS microcontrollers
- Comparable to Parallax PING.
- Echo pulse: positive TTL pulse, 87 µs minimum to 30 ms maximum(PWM)
- On Board Burst LED Indicator shows measurement in progress
- 3-pin header makes it easy to connect using a servo extension cable, no soldering required
- UltraSonic Distance Sensor (PWM O/P) PIN descriptions are as below
COMMUNICATION PROTOCOL:Communication protocol based on ,Ultrasonic Distance Sensor(PWM) as shown below
Under control of a host microcontroller (trigger pulse), the ECHO(PWM) sensor emits a short 40 kHz (ultrasonic) wave. This burst travels through the air, hits an object and then bounces back to the sensor.The ECHO (PWM) sensor provides an output pulse,to the host (through its signal pin) when the echo is detected; hence the distance to the target can be measured from the width of this pulse. Timing of sensor parameters are shown below.
PRACTICAL CONSIDERATION FOR USE
Object Positioning :
The ECHO(PWM) sensor cannot accurately measure the distance to an object that:
- That has its reflective surface at a shallow angle so that sound will not be reflected back towards the sensor (Angle θ < 90°) or is more than 4 meters away,that is shown below.
- Is too small to reflect enough sound back to the sensor.
In addition, if your ECHO(PWM) sensor is mounted low on your device, you may detect sound reflecting off the floor.Positioning of Ultrasonic Distance Sensor(PWM) as shown below
Calculations to be performed by your Host microcontroller:
- Speed of ultrasonic wave is 347 m/s equivalent to 0.0347cm/µsec(Temperature dependent)Timer count multiplied with 200nsec (0.2µsec ), internal clock period gives the echo time (say, Et).
- As per the eqn: Speed = distance/time => echo distance (Ed) = echo speed(Ev) *echo time(Et) ie, distance (Ed) = 0.0347cm per µsec (Ev) * Et µsec
The obtained distance will be twice the actual distance since it gives the to and fro distance of the object as per the to and fro time equated to the equation: (ie, Et stands for 2Et).
Thus the obtained distance divided by 2 gives actual distance of the obstacle.
ie, Actual distance = Ed/2.As per the above illustration your equation is,
Ed = Ev *(Et/2) implies Et = 2 * Ed /Ev equivalent to Et = (2/0.0347) *Ed
Implies Et = 58 *Ed equivalent to Ed (in cm) = Et(in µsec)/58
Output waveform:
- The width of the wave increases with increase in distance between ultrasonic sensor and obstacle.Above graph shows the width of wave i.e 11.617ms.Using this graph we can calculate the distance of object from the sensor.Using above equation the resulting distance of the wave is 200cm.
ie, Ed (in cm) = Et(in µsec)/58 ,
Echo time(Et)=11617 us
Ed=116170/58
Ed=200.29cm
- Above graph shows wave with width of 1.336ms(distance between sensor and obstacle) and the calculated distance is 23 cm.
ie, Ed (in cm) = Et(in µsec)/58 ,
Echo time(Et)=1336 us
Ed =1336/58
Ed=23cm
- Above graph shows wave with width of .271 ms(distance between sensor and obstacle) and the calculated distance is 4.67cm.
ie, Ed (in cm) = Et(in µsec)/58 ,
Echo time(Et)=271 us
Ed=271/58
Ed= 4.67cm
How to Test:1. Test UltraSonic Distance Sensor ( PWM O/P) with Arduino
- Connection of UltraSonic Distance Sensor (PWM O/P)with Arduino board is as follows and connection is shown below: “5V”(Red jumper wire) pin of sensor is directly connected 5V of the Arduino board. “GND” (Black jumper wire) pin of sensor is connected to the “GND” pin of the Arduino board.”SIG”(signal,Blue jumper wire) pin of sensor is directly connected “8” pin of Arduino board.
- In the above figure, the “SIG”(signal I/O) pin of the ECHO(PWM) sensor is connected to PIN8 of the Arduino board. PIN8 of your host controller has to be configured to both input and output accordingly to the function. It is your host controller that has to trigger the functioning of an ECHO(PWM) senor. At this time the pin PIN8 of the host controller has to be configured as output so that it can give (o/p) a trigger signal to the sensor. The trigger pulse should be of range 10µsec – 100µsec, typical value is 10µsec.
- After triggering the sensor, pin PIN8 has to be configured as input so that your host controller switches to the receiving mode. Any pin configured as input should be provided with either pull-up or pull-down voltages so that it isn’t affected by small static field variations. But here no external pull-ups or pull-downs are required since, the signal I/O pin of the ECHO(PWM) sensor which is connected to pin PIN8 has been provided with pull-down on-board.
- In the receiving mode, the host controller has to wait for the Low to High transition in the SIG pin. When a transition is detected, the host controller should start counting the time. The host controller can use a timer or any other programming logic to calculate the time. The counting should terminate when the next High to Low transition is detected. The time gap between transitions will vary (PWM) with respect to the distance to the target. The obtained value (pulse-width) in µSeconds represents the echo time and further calibrations of this data gives you the obstacle distance. ie, the echo time in micro-seconds divided by 58 (at 30° C) gives you the obstacle distance in centimeters and the same divided by 148 (at 30° C) gives the distance in inches. i.e Echo time in µsec /58 = distance in cm and Echo time in µsec /148= distance in inches. The calculated distance can be transmitted to your PC.
Testing with Arduino board, sample program is shown below.
- Triggering PIN 8 pin of Arduino board gives pulse which travels through the air, hits an object and then bounces back to the sensor. Hence the distance to the target can be measured from the width of this pulse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/******************************************************************************************* Program for UltraSonic Distance Sensor (PWM O/p) BOARD : Arduino Duemilanove *******************************************************************************************/ long echo = 0; int signal = 8; double distance = 0; void setup() { Serial.begin(9600); pinMode(signal,OUTPUT); } /******************************************************************************************* Funjction :Triger Description:This function is used to control Ultrasonic sensor *******************************************************************************************/ void Triger() { digitalWrite(signal, LOW); //signal pin is set as Low pinMode(signal, OUTPUT); //signal pin is set as output delayMicroseconds(2); //generate smallest delay digitalWrite(signal, HIGH); // signal pin is set as High delayMicroseconds(5); digitalWrite(signal, LOW); // signal pin is set as low pinMode(signal, INPUT); //signal pin is set as input echo = pulseIn(signal, HIGH); distance = (echo / 58.138); //Calculate distance } /******************************************************************************************* Funjction :loop Description:This function is used to send continusly the distance value *******************************************************************************************/ void loop() { Triger(); Serial.print("Distance:"); Serial.print(distance); Serial.println("cm"); delay(250); //wait for next next pulse delay(250); //wait for start next pulse } |
- When Arduino board connected with ultrasonic sensor(PWM),the distance value is displayed on serial monitor is as shown below
Connection details of UltraSonic Distance Sensor ( PWM O/P) with PIC16F877A is shown below
- In the above figure, the “SIG”(signal I/O) pin of the ECHO(PWM) sensor is connected to RB2 (Pin<35>) of the HOST controller. Pin RB2 of your host controller has to be configured to both input and output accordingly to the function. It is your host controller that has to trigger the functioning of an ECHO(PWM) senor. At this time the pin RB2 of the host controller has to be configured as output so that it can give (o/p) a trigger signal to the sensor. The trigger pulse should be of range 10µsec – 100µsec, typical value is 10µsec.
- After triggering the sensor, pin RB2 has to be configured as input so that your host controller switches to the receiving mode. Any pin configured as input should be provided with either pull-up or pull-down voltages so that it isn’t affected by small static field variations. But here no external pull-ups or pull-downs are required since, the signal I/O pin of the ECHO(PWM) sensor which is connected to pin RB2 has been provided with pull-down on-board.
- In the receiving mode, the host controller has to wait for the Low to High transition in the SIG pin. When a transition is detected, the host controller should start counting the time. The host controller can use a timer or any other programming logic to calculate the time. The counting should terminate when the next High to Low transition is detected. The time gap between transitions will vary (PWM) with respect to the distance to the target. The obtained value (pulse-width) in µSeconds represents the echo time and further calibrations of this data gives you the obstacle distance. ie, the echo time in micro-seconds divided by 58 (at 30° C) gives you the obstacle distance in centimeters and the same divided by 148 (at 30°C) gives the distance in inches. i.e Echo time in µsec /58 = distance in cm and Echo time in µsec /148= distance in inches. The calculated distance can be transmitted to your PC.
Testing with PIC16F877A,sample program is shown below.
- Triggering RB2 pin of PIC16F877A gives pulse which travels through the air, hits an object and then bounces back to the sensor. Hence the distance to the target can be measured from the width of this pulse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
/* ********************************************************************************** UltraSonic-PWM Output-Sample Program with PIC16F877A ********************************************************************************** */ #include<pic.h> #define START_PULSE_TIMER() T0CS=0 //echo wait PULSEIN timer start bit #define STOP_PULSE_TIMER() T0CS=1 //echo wait PULSEIN timer stop bit #define PORT_CONFIG() TRISC=0;TRISD=0 #define INTERRUPT_ENABLE() GIE=1; PEIE=1; T0IF=0; T0IE=1 double PulseInTime=0; //variable calculating the echo travel time double cmDistance; //variable calculating the distance within limits unsigned int tDistance; //temperary variable for holding the distance value unsigned int TimeCnt=0; //timer0 overflow count char DArr[8]; // array holding the calculated distance for display /******************************************************************************************* Function : Delay10Us Description : Delay function for 10 micro-sec *******************************************************************************************/ void Delay10Us() { int dCnt; for(dCnt=0;dCnt<3;dCnt++); } /******************************************************************************************* Function : Delay_100MicroSec Description :Delay function for 100 micro-sec *******************************************************************************************/ void Delay_100MicroSec() { int dCnt; // delay count for(dCnt=1;dCnt<35;dCnt++); // 100Us approx } /******************************************************************************************* Function : DelayMs Description : Delay function for n milli-sec, n=1,2,3.... *******************************************************************************************/ void DelayMs(unsigned char Cnt) { int dCnt; while(Cnt>0) { for(dCnt=0;dCnt<10;dCnt++) Delay_100MicroSec(); Cnt--; } } /******************************************************************************************* Function : UART_DATA Description : function used for send data *******************************************************************************************/ void UART_DATA(char udata) { TXREG=udata; while(TRMT!=1); } /******************************************************************************************* Function : UART_STRING Description : function used for send string *******************************************************************************************/ void UART_STRING(const char *udata) { while(*udata!='\0') { UART_DATA(*udata); udata++; } } /******************************************************************************************* Function : TIMER0_INIT Description :Timer0 initialization *******************************************************************************************/ void TIMER0_INIT() { PSA =1; // set timer0 minimum prescale TMR0=0; // timer0 count register T0CS=1; // timer0 start bit } /******************************************************************************************* Function : UART_INIT Description :uart0 initialization *******************************************************************************************/ void UART_INIT() { SYNC=0; TXEN=1; BRGH=1; SPEN=1; SPBRG=129; } /******************************************************************************************* Function : Conv2DispFormat Description :BCD conversion *******************************************************************************************/ void Conv2DispFormat(unsigned int value) { int i,cnt=1; while(value>=10) // BCD conversion of cm distance calculated { DArr[cnt]=(value%10)+'0'; value=value/10; cnt++; } DArr[cnt]=value+'0'; for(i=cnt;i>=1;i--) { UART_DATA(DArr[i]); } } /******************************************************************************************* Function : TriggerPulse Description :Generating pulse for sensor *******************************************************************************************/ void TriggerPulse() { RB2=0; TRISB2=0; // pulse signal line made O/P for trigger Delay10Us(); RB2=1; // H_to_L transition. ie, //Trigger Pulse to the Ultrasonic range finder module Delay10Us(); RB2=0; TimeCnt=0; // clear timer overflow count PulseInTime=0; TMR0=0; } /******************************************************************************************* Function : PulseInMode Description :Calculating & displaying distance *******************************************************************************************/ void PulseInMode() { RB2=0; // clear I/O pin TRISB2=1; // pulse signal line made I/P for echo PulseIn while(!RB2); // wait for PulseIn signal to go HIGH START_PULSE_TIMER(); // start echo PulseIn timer T0CS=0 while(RB2); // wait for PulseIn signal to go LOW STOP_PULSE_TIMER(); // stop echo PulseIn timer T0CS=1 RB2 =0; // clear I/O pin TRISB2=0; // pulse signal line made O/P PulseInTime= TMR0; // timer calculations [return time in Us] PulseInTime= (double)( ( (PulseInTime* 0.2) + (TimeCnt * 51.2)) ); cmDistance = (double) PulseInTime/58; // timer value in Us // divided by 58 gives the cm distance tDistance =(unsigned int)cmDistance; // display of the calculated distance UART_STRING("Distance="); Conv2DispFormat(tDistance); UART_DATA('.'); Conv2DispFormat((unsigned int)((cmDistance-tDistance)*10)); UART_STRING("cm "); UART_DATA(0X0D); UART_DATA(0X0A); } void main() { PORT_CONFIG(); // PORT configurations TIMER0_INIT(); // Timer initialisation INTERRUPT_ENABLE(); // Interrupt enable UART_INIT(); while(1) { TriggerPulse(); // Trigger mode PulseInMode(); // PulseIn mode DelayMs(250); //wait before next cycle DelayMs(250); //wait before next cycle } } /******************************************************************************************* Function : interrupt isr Description :interrupt service *******************************************************************************************/ void interrupt isr() { if(T0IF==1) // timer overflow interrupt { T0IF=0; // clear timer interrupt flag TimeCnt++; // timer overflow count increment } } |
- When PIC16F877A connected with ultrasonic sensor(PWM),the distance value is displayed on serial monitor as shown below
How to Buy:Click here to buy rhydoLABZ UltraSonic Distance Sensor (PWM O/P)
Click here to buy rhydoLABZ USB to Serial Converter (5V/3V3)
Click here to buy rhydoLABZ UltraSonic Distance Sensor (Serial Ascii O/P)
Support:Please share your ideas with us, visit our forum for discussion
Frequently Asked Questions(FAQ):Q.Which type of target object material will effect this sensor?
Ans.In addition, objects that absorb sound or have a soft or irregular surface, such as a stuffed animal, may not reflect enough sound to be detected accurately. The ECHO sensor will detect the surface of water; however it is not rated for outdoor use or continual use in a wet environment. Condensation on its transducers may affect performance and lifespan of the device.
Q.How this sensor effect Air Temperature?
Ans.Temperature has an effect on the speed of sound in air that is measured by the ECHO sensor. If the temperature (degree Celsius) is known, the formula is
C_air = 331.5 + (0.6 * Tc)m/s
The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the magnitude of 11 to 12 percent. The use of conversion constants to account for air temperature may be incorporated into your program.
Q.What are the measurable distance of UltraSonic Distance Sensor (PWM O/P)?
Ans.The sensor provides precise,Stable non-contact distance measurements from about 2 cm to 400 cm with very high accuracy
Leave a Reply
You must be logged in to post a comment.