Interfacing PIC MCU with HC-SR04 ultrasonic sensor | MPLAB Projects

The HC-SR04 ultrasonic sensor module can measure distances form 2 cm to 400 cm (4 m) with an accuracy of 3 mm. The HC-SR04 module includes ultrasonic transmitter, ultrasonic receiver and control circuit.
This post shows how to make a simple distance meter using PIC16F887 microcontroller and HC-SR04 ultrasonic sensor.
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

The HC-SR04 ultrasonic sensor module has 4 pins as shown in the image below where:
VCC : Positive power supply (+5V)
Trig : Trigger input pin
Echo : Echo output pin
GND : Ground (0V)

HC-SR04 Ultrasonic sensor

HC-SR04 ultrasonic sensor timing diagram:
The timing diagram of the HC-SR04 ultrasonic sensor is shown below.
First we have to supply the sensor trigger pin with a pulse of 10µs and the sensor will automatically send 8 cycles burst of ultrasound at 40 kHz and raise its echo pin. The Echo is a distance object that is pulse width and the range in proportion. We can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or:
the range = high level time x sound velocity / 2  where the sound velocity = 340M/S.

HC-SR04 Ultrasonic sensor signals

Related Projects:
To see how to interface PIC microcontroller with LCD module using MPLAB XC8 compiler, read the following post:
Interfacing LCD with PIC microcontroller | MPLAB Projects

PIC16F887 and HC-SR04 ultrasonic sensor circuit

Hardware Required:

  • PIC16F887 microcontroller   —->  datasheet
  • HC-SR04 ultrasonic sensor
  • 16×2 LCD screen
  • 10k ohm variable resistor or potentiometer
  • 330 ohm resistor
  • 5V source
  • Breadboard
  • Jumper Wires

Interfacing PIC microcontroller with HC-SR04 ultrasonic sensor circuit:
Distance meter project circuit schematic diagram is shown below.

PIC16F887 with HC-SR04 ultrasonic sensor and 16x2 LCD - MPLAB XC8 HC-SR04 - mikroC HC-SR04

(All grounded terminals are connected together)

The HC-SR04 ultrasonic sensor board has 4 pins (from left to right): VCC (+5V), Trig (Trigger), Echo and GND (ground).
The Trigger pin is connected to pin RB1 (#34) and Echo pin is connected to pin RB0 (#33) of the PIC16F887 microcontroller.

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing PIC microcontroller with HC-SR04 ultrasonic sensor C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:
MPLAB XC8 LCD Library

after the download, add the library file (LCD_Lib.c) to project folder.

In this project I used Timer1 module (16-bit timer) to measure signal widths (logic 1 and logic 0 widths), it is configured to increment every 1 us (1 tick per microsecond). That means Timer1 module clock frequency is 1MHz.

Functions used in the code:
__bit wait_sensor(): this function waits for sensor’s response, it returns 1 if there has been a response and 0 if time out. Here the time out is 1000 ticks = 1000 us = 1 ms.

__bit get_distance(uint16_t *ticks): this function measures the duration of the pulse sent by the       HC-SR04 sensor (in microseconds). This duration is stored in the variable *ticks. This function returns 0 if OK and 1 if there is out of range or time out error. In this function Timer1 also is used to measure the pulse (high pulse) generated by the HC-SR04 sensor (Echo pulse).

Why 23200?
The actual distance (in cm) is equal to the width of the Echo pulse (in us) divided by 58. With the maximum distance that the sensor can measure is 400 cm we get a maximum Echo pulse (in us) of  400 x 58 = 23200

The connections between the microcontroller and the HC-SR04 sensor are defined as:

The microcontroller used in this example is PIC16F887, configuration words are:

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

The following video shows how a hardware circuit of the project should work (in the video PIC16F877A microcontroller is used):

3 thoughts on “Interfacing PIC MCU with HC-SR04 ultrasonic sensor | MPLAB Projects”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top