7 Segment Display Interfacing With PIC Microcontroller (PIC16F877A)
7 Segment Display Interfacing With PIC Microcontroller (PIC16F877A)
This is our 8th tutorial of Learning PIC microcontrollers using MPLAB and XC8. We have
come up all the way from installing MPLABX (https://circuitdigest.com/microcontroller-
projects/getting-started-with-pic-microcontroller) to using a LCD with PIC MCU. If you are
new here, then look at previous tutorials where you can learn timers
(http://circuitdigest.com/microcontroller-projects/pic-microcontroller-timer-tutorial),
blinking LED (http://circuitdigest.com/microcontroller-projects/led-blinking-with-pic-
microcontroller), interfacing LCD (https://circuitdigest.com/microcontroller-
projects/16x2-lcd-interfacing-with-pic-microcontroller) etc.. You can find all our PIC
Tutorials here (http://circuitdigest.com/pic-microcontroller-projects). In our last tutorial we
saw how we can generate Custom characters with our 16*2 LCD display
(https://circuitdigest.com/microcontroller-projects/custom-characters-on-lcd-using-
pic16f877a), now let us equip our self with another type of display module called the 7-
segment display and interface it with PIC Microcontroller.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 1/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
drawback of having low character size and will be overkill for your project if you are just
planning to display some numeric values. 7-segments also have the advantage against poor
lighting condition and can be viewed from lager angles than a normal LCD screen. So, let us
start knowing it.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 2/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 3/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Now we know how to display our desired numeric character on a single 7-segment display.
But, it is pretty evident that we would need more than one 7-segment display to convey any
information that is more than one digit. So, in this tutorial we will be using a 4-digit 7-
Segment Display Module as shown below.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 4/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
As we can see there are Four Seven Segment Displays connected together. We know that
each 7-segment module will have 10 pins and for 4 seven segment displays there would be
40 pins in total and it would be hectic for anyone to solder them on a dot board, so I would
highly recommend anyone to buy a module or make your own PCB for using a 4-digit 7-
segment display. The connection schematic for the same is shown below:
To understand how 4-digit seven segment module works we have to look into the above
schematics, as shown the A pins of all four display is connected to gather as one A and the
same for B,C.... upto DP. So, basically if trigger A on, then all four A's should go high right?
But, that does not happen. We have additional four pins from D0 to D3 (D0, D1, D2 and D3)
which can be used to control which display out of the four should go high. For example: If I
need my output to be present only on the second display then only D1 should be made high
while keeping other pins (D0, D2, and D3) as low. Simply we can select which display has to
go active using the pins from D0 to D3 and what character to be display using the pins from
A to DP.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 5/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
(/fullimage?i=circuitdiagram_mic/interfacing-7-segment-with-pic-microcontroller-
circuit.png)
We have 12 output pins from the module out of which 8 is used to display the characters
and four is used to select one display out of four. Hence all the 8 character pins are
assigned to PORTD and the display selection pins are assigned to first four pins of PORTC.
Note: Ground pin of the module should also be connected to the ground of the MCU which
is not shown here.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 6/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
As usual we use the set configuration bits window to set these bits. If you are not sure what
they mean then visit the LED blinking tutorial here
(https://circuitdigest.com/microcontroller-projects/led-blinking-with-pic-microcontroller).
Next let us define the output pins for toggling between each digit of the display.
Here the pins RC0, RC1, RC2 and RC3 are used for selecting between the four digits of our
7-segment display module. These pins are defined as s1, s2, s3 and s4 respectively.
Next let us jump into void main(), inside which we have the following variable declaration:
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 7/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Here the variables i and flag are used for storing the values to be displayed and creating a
delay respectively. The unsigned integer variables a to h are used to break the four digit
numbers into single digits and store them (which will be explained later here).
One key thing to note here is the "seg[]" array declaration. In this program we are using a
new data type called Array. Array is nothing but a collection of similar data-type values.
Here, we have used this array to store all the equivalent hex values for displaying a number
from 0 to 9.
The address of the array always starts from zero. So this array will have the hex value of a
numeric number (0-9) stored in the address which is same as that of the number as shown
below
Variable: seg[0] seg[1] seg[2] seg[3] seg[4] seg[5] seg[6] seg[7] seg[8
Hex
0X3F 0X06 0X5B 0X4F 0X66 0X6D 0X7C 0X07 0X7F
Code:
Eq.
Numeric 0 1 2 3 4 5 6 7 8
number:
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 8/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
So simply, if you want to display the number 0 on your 7-segment you can call seg[0],
likewise if you want to display the number 6 you just have to use seg[6].
To understand how the HEX value was actually obtained let us look into the below table.
The equivalent HEX value for each decimal number is stored in the array so that it can be
called to display one particular number.
Now, let us move on to the next part of the code which is the I/O configuration:
//*****I/O Configuration****//
TRISC=0X00;
PORTC=0X00;
TRISD=0x00;
PORTD=0X00;
//***End of I/O configuration**///
I/ O configuration is simple because all the pins on our 7-segment are output pins, and the
connections are shown in the circuit diagram above, so simply declare them as outputs and
initialize them to zero.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 9/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Now let us jump into our infinite loop (while(1)). Here we have to split the value of "i" into
four digits and display them on the 7-segment. First let us start by splitting the value on "i"
By using simple modulus and division operation the 4 digit number (i) is separated into
individual numbers. In our case let us take an example where the value of "i" is 4578. Then
at the end of this process the variable g=4, e=5, c=7, and a=8. So now it will be easy to
display each digit by simply using that variable.
This is the actual place where the MCU talks with the 7-segment. As we know we can
display only one digit at a time, but we have four digits to be displayed and only if all the
four digits are On the complete four digit number will be visible for the user.
Lucky for us our MCU is very much faster than a human eye, so what we actually do: we
display one digit at a time but we do it very fast as shown above.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 10/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
We select one digit display it wait for 5ms so that the MCU and 7-segment can process it
and then turn off that digit and move on to the next digit and do the same till we reach the
last digit. This delay of 5ms cannot be observed by a human eye and all the four digits
appeared to be On at the same time.
That is it, finally we just increment the value of displayed digit using a delay as shown below
The delay is used so that the time taken for changing from one number to another is long
enough for us to notice the change.
The complete code is given below and the process is also explained in the Video at the end.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 11/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
This project does not have any complicated hardware setup, we are again using the same
PIC Microcontroller board which we have created in LED blinking Tutorial
(http://circuitdigest.com/microcontroller-projects/led-blinking-with-pic-microcontroller).
Simply connect the 7-segment Module with your PIC Microcontroller board according the
connection diagram. Once you are done with the connections, simply dump the code using
your PicKit 3 programmer and that is it enjoy your output.
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 12/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 13/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Code
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 14/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection
// #pragma config statements should precede project file includes.
#define s1 RC0
#define s2 RC1
#define s3 RC2
#define s4 RC3
//***End of definition**////
void main()
{
unsigned int a,b,c,d,e,f,g,h; //just variables
int i = 0; //the 4-digit value that is to be displayed
int flag =0; //for creating delay
unsigned int seg[]={0X3F, //Hex value to display the number 0
0X06, //Hex value to display the number 1
Video
Tags
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 15/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Comments
Submitted by eengine on Sat, 07/01/2017 - 19:32
Permalink (/comment/15717#comment-15717)
Permalink (/comment/15720#comment-15720)
Permalink (/comment/16021#comment-16021)
Permalink (/comment/16030#comment-16030)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 16/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Voltmeter (/comment/16030#comment-16030)
I have a voltmeter which I made with an lcd, but I want to change it to 4 7-segment display.
How can I do this?
Permalink (/comment/16062#comment-16062)
Hi Godwin, (/comment/16062#comment-16062)
Hi Godwin,
If you have made it with LCD then its quite straight forward to replace it with an 7-segment
Display. For example if you have saved the voltage value in the variable "Voltage" simply use
the above program and replace the variable i with "Voltage". Also prevent it from
incrementing.
This is the dirtiest way to make it work. But, once you get this understand the program and re-
write your complete code.
Permalink (/comment/16967#comment-16967)
Permalink (/comment/16972#comment-16972)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 17/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Permalink (/comment/18193#comment-18193)
Permalink (/comment/18215#comment-18215)
Permalink (/comment/23727#comment-23727)
J
Submitted by Jo (/users/jo) on Sun, 01/06/2019 - 00:30
Permalink (/comment/29550#comment-29550)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 18/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Permalink (/comment/29678#comment-29678)
S
Submitted by Sam (/users/sam-6) on Mon, 07/01/2019 - 14:04
Permalink (/comment/30364#comment-30364)
J
Submitted by Joseph Tannenbaum (/users/joseph-tannenbaum) on Thu, 08/29/2019 - 02:53
Permalink (/comment/30561#comment-30561)
J
Submitted by Joseph Tannenbaum (/users/joseph-tannenbaum) on Thu, 08/29/2019 - 03:22
Permalink (/comment/30562#comment-30562)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 19/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Permalink (/comment/31636#comment-31636)
v
Submitted by vijayan (/users/vijayan) on Mon, 10/05/2020 - 19:34
Permalink (/comment/32686#comment-32686)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 20/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
Semicon Media is a unique collection of online media, focused purely on the Electronics
Community across the globe. With a perfectly blended team of Engineers and
Journalists, we demystify electronics and its related technologies by providing high
value content to our readers.
(https://www.facebook.com/circuitdigest/) (https://twitter.com/CircuitDigest)
(https://www.youtube.com/channel/UCy3CUAIYgZdAOG9k3IPdLmw)
(https://www.linkedin.com/company/circuit-digest/)
COMPANY
Privacy Policy (/privacy-policy) Cookie Policy (/cookie-policy)
Advertise (/advertise)
PROJECT
555 Timer Circuits (/555-timer-circuits) Op-amp Circuits (/op-amp-circuits)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 21/22
1/8/25, 10:55 PM 7 Segment Display Interfacing with PIC Microcontroller (PIC16F877A)
AVR Projects (/avr-microcontroller-projects) 8051 Projects (/8051-microcontroller-
projects)
OUR NETWORK
(https://components101.com)
(https://circuitdigest.com) (https://iotdesignpro.com)
https://circuitdigest.com/microcontroller-projects/7-segment-display-interfacing-with-pic16f877a 22/22