Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller
HC-SR04 Ultrasonic Distance Sensor is a popular and low cost solution for non-contact distance measurement function. It is able to measure distances from 2cm to 400cm with an accuracy of about 3mm. This module includes ultrasonic transmitter, ultrasonic receiver and its control circuit.
HC-SR04 module has 4 pins :
- VCC – 5V, +ive of the power supply
- TRIG – Trigger Pin
- ECHO – Echo Pin
- GND – -ive of the power supply
HC-SR04 Ultrasonic Module Working
- Provide TRIGGER signal, atleast 10μS High Level (5V) pulse.
- The module will automatically transmit eight 40KHz ultrasonic burst.
- If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.
- If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about 150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.
Interfacing with PIC Microcontroller
PIC 16F877A is the heart of this circuit. VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively which will provide necessary power for its operation. A 8MHz crystal is connected to OSC1 and OSC2 pins of PIC, to provide clock for its operation. 22pF capacitors connected along with the crystal will stabilize the oscillations generated by the crystal. 16×2 LCD is connected to PORTD which is interfaced using 4 bit mode communication. 10KΩ preset is used to adjust the contrast of the LCD. A 100Ω resistor is used to limit current through the LCD back-light LED.
TRIGGER pin of HC-SR04 sensor is connected to RB0 (pin 33) of PIC which is to be configured as an Output PIN (TRIS bit is 0) and ECHO pin is connected to RB4 (pin 37) which is to be configured as an Input PIN (TRIS bit is 1).
Basic Steps :
- Provide TRIGGER to ultrasonic module
- Listen for Echo
- Start Timer when ECHO HIGH is received
- Stop Timer when ECHO goes LOW
- Read Timer Value
- Convert it to Distance
- Display it
Timer1 Module can be used as a 16 bit counter or timer. It consists of two 8 bit registers TMR1H and TMR1L which are readable and writable. The register pair, TMR1H:TMR1L increments from 0000H to FFFFH and rolls over to 0000H. If enabled Timer1 Overflow Interrupt is generated during rolls over to 0000H. Here we will use this module as a 16 bit Timer.
Since we are using Timer1 module as a Timer, we should use internal clock (Fosc/4), ie TMR1CS = 0. Prescaler is used to divide the internal clock (Fosc/4). Here we can set Prescaler as 2, ie T1CKPS1 = 0 & T1CKPS0 = 1. T1SYNC bit is ignored when TMR1CS = 0. As we are using internal clock (Fosc/4) we can disable oscillator, ie T1OSEN = 0. TMR1ON bit can be used to ON or OFF timer as per our requirements.
- Thus we can initialize timer as : T1CON = 0x10
- To TURN ON the Timer : T1CON.F0 = 1 or TMR1ON = 1
- To TURN OFF the Timer : T1CON.F0 = 0 or TMR1ON = 0
Fosc is the oscillator frequency, here we are using 8MHz crystal hence Fosc = 8MHz.
Time = (TMR1H:TMR1L)*(1/Internal Clock)*Prescaler
Internal Clock = Fosc/4 = 8MHz/4 = 2MHz
Therefore, Time = (TMR1H:TMR1L)*2/(2000000) = (TMR1H:TMR1L)/1000000
- Distance = Speed * Time
- Let d be the distance between Ultrasonic Sensor and Target
- Total distance traveled by the ultrasonic burst : 2d (forward and backward)
- Speed of Sound in Air : 340 m/s = 34000 cm/s
- Thus, d = (34000*Time)/2, where Time = (TMR1H:TMR1L)/(1000000)
- Therefore, d = (TMR1H:TMR1L)/58.82 cm
- TMR1H:TMR1L = TMR1L | (TMR1H<<8)
Simple Method – MikroC & MPLAB XC8 Program
For more detail: Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller
For All Your PCB Need, One-stop PCB Solution Services Appear:
EasyEDA: free PCB design tool, 200,000+ engineers are using
PCBs: 10pcs 2-layer 10x10cm only $8.21, on-time global shipping
Components: save 50% on cost, save on freight if shipped with ordered PCBs
Current Project / Post can also be found using:
- hc-sr04 ultrasonic sensor microcontroller code
- picaxe HC-SR04 interface
- ultrasonic sensor interfacing with pic16f877a
- interefacing hc sr04