DesignSpark Electrical Logolinkedin
Menu Ricerca
Ask a Question

2 Oct 2018, 14:56

How far is it?

This project has been developed as part of a final degree project of the MsC in Industrial Engineering (speciality in electronics) at the Electrical, Electronics and Communications Department of the Public University of Navarra. The aim of this project is to build a digital meter; when the pushbutton is pressed the distance to the object in front will appear in the LCD. An ultrasonic sensor (HC-SR04) will be used for the measurement of the distance.

The development of the project will be divided into two different parts: the electronic circuit design (using DesignSpark PCB) and the programming of the microcontroller.

1. Electronic circuit design


The control circuit is based on the PIC16F876A microcontroller, which works at 5V. Furthermore, an external oscillator working at 20 MHz (0.2µs per instruction cycle) will be used. In-circuit Serial Programming (ICSP) connector will be used In order to connect the PC with the microcontroller and enable direct programming/debugging of the robot.

Linear voltage regulator

To power up the PCB an 8.4V rechargeable battery will be employed. Since the operating voltage of the microcontroller is of 5V, a linear voltage regulator (7805) will be necessary to decrease the voltage from 8.4V to 5V.

Ultrasonic Ranging Module

This sensor is composed by 4 pins: 5V and GND; the trigger pin, which will receive a 10µs pulse from the microcontroller to start the ranging; and the echo pin, which will send a pulse to the microcontroller, the width of the pulse is proportional to the distance of the object in front. You can calculate the range through the time interval between sending trigger signal and receiving echo signal. Formula: µs / 58 = centimeters.


LCD screen

For the control of the LCD screen pins RS and Enable will be used, pin R/W will be connected to GND, in that way it will always be in write mode. Besides, the data bus will be of 4 bits (DB4:DB7) and will be connected to the pins RB0:RB3 of the microcontroller.


The components needed for the development of the project are detailed below:

    • PIC16F876A.
    • 20MHz crystal oscillator.
    • 5V linear voltage regulator.
    • Ultrasonic Range Module (HC-SR04).
    • Pushbutton.
    • LCD screen.
    • Several discrete components, such as connectors, NPN transistors, resistors and capacitors (detailed in the BOM list attached at the end of the article).


2. PCB design

The design of the PCB has been done with the software DesignSpark PCB 8.0. The components have been placed in order to save as much space as possible and the package of the components has been selected accordingly (SMD) so it will help to save more space. To reduce the number of tracks and vias to the maximum the bottom layer is used as GND plane. The following figures show the 3D representation of the PCB prototype:

3. Programming environment


This section describes the functions of the digital meter and the number of pins of the microcontroller that are used:




Ultrasonic Range Module

4 pins


6 pins


1 pin


11 pins


Adding the 5V and GND pins, the external oscillator pins and the modular connection pins, it makes a total of 18 pins out of the 28 pins available in the microcontroller used for this project (PIC16F876A). 

The microcontroller has been programmed using the Integrated Development Environment (IDE) MPLAB 8.92 and the ICD2 debugger as a depuration tool. The programming language used for this project is C by means of the HI-tech compilation tool. All the code has been attached at the end of the article but some of the main microcontroller integrated hardware used in the project is described below.

Sleep mode

The microcontroller will be in sleep mode until the button is pressed, in this way it will save power.

CCP2 module

The Echo pin has been connected to the pin RC1 of the microcontroller, attached to the CCP2 module, which will work in capture mode. It will be necessary to measure the width of the pulse that is received from the ultrasonic sensor to know the distance to the object in front. When the module detects the first rising edge the Timer 1 will start counting the time until the first falling edge arrives. 

Timer 2

The Timer 2 will be used to control the frequency that the 10µs pulse is sent to the Trigger Pin. A pulse will be sent 2 times per second. The timer will be configured to interrupt every 10ms, so it will be necessary to use an auxiliary counter. 


The interruptions of CCP2 (for the measurement of the range) and Timer 2 (to control the frequency at which the pulse is sent) will be needed for the correct operation of the digital meter. Furthermore, to wake up the microcontroller from the sleep mode the interruptions of change in RB4:RB7 will be needed since the pushbutton is connected to RB4. 






julenmc has not written a bio yet…

2 Oct 2018, 14:56