|
Circuit 146 - Ultrasonic Distance Meter with PIC16F877A
Ultrasonic Sensors act as the eyes and ears for all embedded applications, they help the digital Microcontroller to understand what is actually happening in this real Analog world. This article shows how to Interface the Ultrasonic Sensor HC-SR04 with a PIC microcontroller. The HC-SR04 is an ultrasonic sensor which can be used to measure distances anywhere between 2cm to 450cm (theoretically). This sensor has worked well in many projects which involve obstacle detection, distance measuring, environment mapping etc. To better understand its operation, an LCD display has been installed to display the correct distance.
How does an Ultrasonic Sensor work? Before we get any further, we should know how an Ultrasonic sensor works. The ultrasonic sensor used in this project is shown in Figure below.
As you can see it has two circular eyes like projections and four pins coming out of it. The two eye-like projections are the Ultrasonic wave (hereafter referred as US wave) Transmitter and receiver. The transmitter emits a US wave at a frequency of 40Hz, this wave travels through the air and gets reflected back when it senses an object. The returning waves are observed by the receiver. Now we know the time taken for this wave to get reflected and come back and the speed of the US wave is also universal (3400cm/s). Using this information and the below high school formulae we can calculate the distance covered (Distance = Speed × Time). Now that we know how an US sensor works, let us how it can be interfaced with any MCU/CPU using the four pins. These four pins are Vcc, Trigger, Echo and Ground respectively. The module works on +5V and hence the Vcc and ground pin is used to power the module. The other two pins are the I/O pins using which we communicate to our MCU. The trigger pin should be declared as an output pin and made high for a 10uS, this will transmit the US wave into the air as 8 cycle sonic burst. Once the wave is observed the Echo pin will go high for the exact interval of time which was taken by the US wave to return back to the sensor module. Hence this Echo pin will be declared as input and a timer will be used to measure how long the pin was high. For more details see diagram below Example: We know that the speed of sound is 343 m/s under conditions of 20 °C temperature, 50% humidity, and sea-level atmospheric pressure. Converting units results in:
That is, sound takes 29.2 microseconds to travel one centimetre. Therefore, we can obtain the distance from the time between the emission and reception of the pulse using the following equation.
The reason for dividing the time by two (in addition to the speed of sound in the appropriate units, which we calculated before) is that we have measured the time it takes for the pulse to go and come back, so the distance traveled by the pulse is twice the distance we want to measure.
As shown in Figures above, the circuit involves nothing more than a LCD display and the Ultrasonic sensor itself. The US sensor can be powered by +5V and hence it is directly powered by the any 7805 voltage regulator. The sensor has one output pin (Trigger pin) which is connected to pin 20 (RD1) and the input pin (Echo pin) is connected to pin 21 (RD2). The complete pin connection is illustrated in the table below.
Programming your PIC Microcontroller Inside, the main function we start with initializing the IO pins and other registers as usual. We define the IO pins for LCD and US sensor and also initiate the Timer 1 register by setting it to work on 1:4 pre-scalar and to use internal clock (Fosc/4) processor 16f877A ; indicates the processor type org 0x00 ;start Program bsf STATUS,RP0 ; Active bank 1 to configure the PORTs movlw b'00000001' ;Prescaler=1:1(65535) TMR1=Internal (fosc/4) TMR1=ON movwf T1CON ; configure T1CON The Timer 1 is a 16-bit timer used in PIC16F877A, the T1CON register control the parameters of the timer module and the result will be stored in TMR1H and TMR1L since it a 16-bit result the first 8 will be stored in TMR1H and the next 8 in TMR1L. This timer can be turned on or off using TMR1ON=0 and TMR1ON=1 respectively. Now, the timer is ready to use, but we have to send the US waves out of the sensor, to do this we have to keep the Trigger pin high for 40KHz, this is done by the following code. ;*************** Send 40KHz **************** ;*** Check if there is a pulse detected msg 0x0b,0x0b,0x0b ; - - - error detection ;*** Sends 0.5 msec pulse As shown in the timing diagram above, the Echo pin will stay low till the wave returns back and will then go high and stay high for the exact time taken for the waves to return back. This time has to be measured by the Timer 1 module. Once the time is measured the resulting value will be saved in the registers TMR1H and TMR1L, these registers have to be clubbed to gather to get the 16-bit value. This is done by using the line below ;16-bit/8-bit division division1 ;( R > 0 ) division2 ;( R = 0 ) division3 ;( R < 0 ) division4 Once the time is measured the resulting value will be saved in the registers TMR1H and TMR1L, these registers have to be clubbed to gather to get the 16-bit value. Next we have to find how to calculate the distance. As we know distance = speed * time. But here the result should be divided by 2 since the wave is covering both the transmitting distance and receiving distance. The speed of us wave (sound) is 34000cm/s. digito_dec ;*** Centenas digit_c1 ;( R > 0 ) digit_c2 ;( R = 0 ) digit_c3 ;( R < 0 ) digit_c4 ;***dozens digit_d0 digit_d1 ;( R >= 0 ) digit_d2 ;( R < 0 ) ;*** Units After this, all that remains is to pass the output result to the LCD where the reading takes place. The program is written entirely in assembly and can be downloaded from here
|
We offer the best and economical solutions, backed by 27+ years of experience and international standards knowledge, echnological changes, and industrial systems.