Interfacing Seven Segment Displays
Seven segment displays are very common for electronic product to display numerical output. Many common devices like calculators, watches, electronic weighing scales,ovens etc use them. You must have also seen lifts display the current floor numbers in seven segment displays. So in this article I will show you how to use 7-Segment displays in your own projects.
Fundamentals
A seven-segment display is so named because it is divided into seven different segments that can be switched on or off. The different combination of these segments switched on produces different English numbers. The display also has a decimal point.
![]() |
Fig: Seven Segment Display |
![]() |
Fig: '4' |
The figure shows a seven segment display and the names of the various segments. For example if you want to display number 4 then segments that will be ‘on’ are {f,g,b,c} while rest are ‘off’.Basically the seven segments are just LEDs. The one common end of all the leds are connected while the rest are available. Depending on whether anode or cathode of all the leds are common they are of two types.
1) Common anode 2)Common cathode
![]() |
Fig: Common Cathode Type |
![]() |
Fig: Common Anode Type |
PIN Configuration
Now you know the basic of these displays, to use them you should know the pin configuration of the commercially available displays. As you must have guessed these displays should have nine pin( one for each segment + decimal point +common) but the available modules have two pins for common. They are internally connected. So they have total of 10 PINs.
![]() |
Fig: Pin Configuration |
![]() |
Fig: A seven segment display |
Interfacing with MCU
Interfacing these displays are same as interfacing LEDs with MCU. You need 7 MCU port pins to control them. If you also want to control the decimal point you need one extra pin. The connection is simple.
![]() |
Fig: Connection with MCU |
Here I have interfaced a common anode(+) 7 segment display with PORTD of AVR.If you have made the “home made avr dev board” then you can easily connect this to PORTD of the board by using 8PINconnectors. The segments will be "on" when levels on the PORT is low, that is 0.
Programming
These displays are very easy to program in C. I am giving here a function, which you may use to display digits in 7-segment display. The sample program uses the function to continuously display digits from 0-9 and then repeating the sequence.
/*
A program to demonstrate the use of seven segment displays.
Hardware:
A single seven segment display connected to PORTD as
a->PD7
b->PD6
c->PD5
d->PD4
e->PD3
f->PD2
g->PD1
DP->PD0
By:
Avinash Gupta
eXtremeElectronics.co.in
avinash@extremeelectronics.co.in
*/
#include <avr/io.h>
#include <util/delay.h>
//Configurations
//**************
// Here you may cange the port in which you have connected the display
#define SEVEN_SEGMENT_PORT PORTD
#define SEVEN_SEGMENT_DDR DDRD
void SevenSegment(uint8_t n,uint8_t dp)
{
/*
This function writes a digit given by n to the display
the decimal point is displayed if dp=1
Note:
n must be less than 9
*/
if(n<10)
{
switch (n)
{
case 0:
SEVEN_SEGMENT_PORT=0b00000011;
break;
case 1:
SEVEN_SEGMENT_PORT=0b10011111;
break;
case 2:
SEVEN_SEGMENT_PORT=0b00100101;
break;
case 3:
SEVEN_SEGMENT_PORT=0b00001101;
break;
case 4:
SEVEN_SEGMENT_PORT=0b10011001;
break;
case 5:
SEVEN_SEGMENT_PORT=0b01001001;
break;
case 6:
SEVEN_SEGMENT_PORT=0b01000001;
break;
case 7:
SEVEN_SEGMENT_PORT=0b00011111;
break;
case 8:
SEVEN_SEGMENT_PORT=0b00000001;
break;
case 9:
SEVEN_SEGMENT_PORT=0b00001001;
break;
}
if(dp)
{
//if decimal point should be displayed
//make 0th bit Low
SEVEN_SEGMENT_PORT&=0b11111110;
}
}
else
{
//This symbol on display tells that n was greater than 9
//so display can't handle it
SEVEN_SEGMENT_PORT=0b11111101;
}
}
void Wait()
{
// An approx one second delay for 12Mhz CPU clock
uint8_t i;
for(i=0;i<46;i++)
{
_delay_loop_2(0);
}
}
void main()
{
//Setup
SEVEN_SEGMENT_DDR=0xFF; //All output
SEVEN_SEGMENT_PORT=0xFF; //All segments off
uint8_t count=0;
while(1)
{
SevenSegment(count,0);
count++;
if(count==10)
{
count=0;
}
Wait();
}
}
Download Sample Code
Download PDF version | Get Adobe Reader Free !!!








[...] have discussed the basics of seven segment displays on our tutorial “Using Seven Segment Displays with AVR MCUs”. So you should be familiar with them. In this tutorial we will discuss about multiplexing of seven [...]
October 4th, 2008 at 2:13 pm[...] Using Seven Segment Displays. [...]
October 12th, 2008 at 4:28 amhi avinash,
February 26th, 2009 at 11:01 pmcud u plz tell me about unit8_t.i ve not come across such a datatype..
thanks in advance..
Hello Dhanya,
As a beginner you have used int & char. But in any professional c / c++ programming they are not used directly because the size depends on OS/Platform and implementation of C/C++. So every where in real programming senario you have “typedef” for data types. For example development of Windows application with SDK uses its own typedefs. Almost every tool/library provide typedefs.
So when you are in real c/c++ development you always come across new data types, so don’t be afraid.
Now your question-> What is uint8_t?
Ans: It is a 8 bit unsigned integer. Similarly there are uint16_t and int8_t and int16_t. The prefix u tells that it is unsigned i.e. it can hold only positive value.
See
February 27th, 2009 at 8:58 amhttp://www.nongnu.org/avr-libc/user-manual/stdint_8h.html
So
every where in your program where you are specifying uint16_t you are EXACTLY demanding that you need a int with 16bits and which is UNSIGNED. When you compile it under AVR with GCC it will automatically get translated to “unsigned int” (which is a basic C data type). Since int under avr-gcc is 16bit wide.
And suppose you move some code (say bubble sort) from AVR to ARM platform (or even Windows) then you if you had written “unsigned int” they would all have become 32 bit wide !!!
So now you just need to change a singe defination of uint16_t and every place of your code that used uint16_t will get correctly defined !
Hope you should have got it.
February 27th, 2009 at 9:06 amSo
February 27th, 2009 at 9:09 amuint8_t is not a new data type that you may be thinking that you missed while learing C. Its just a method for writing a more manageable code, the professional way.
Hello Sir,
March 13th, 2009 at 6:41 pmMay you please explain the following
for(i=0;i<46;i++)
{
_delay_loop_2(0);
}
Atleast I understood that 46 refers to 1 second and 23 refers to 0.5 second but how does that work or I mean to say What is the meaning of the expression inside the loop.
Hello Prafull,
The _delay_loop_2(0) is a 16bit delay counter that means it can count up to 2^16 i.e. upto 65536. And in one loop it burns 4 cpu cycle. We have pass 0 to count to maximum i.e. 65536
Since we are using 12MHz CPU the time period for
1 cycle = 0.000000083 sec
4 cycle = 0.00000033 sec
4x 65536 = 0.22sec
so 46 x 0.022 sec = 1.012 sec
that is approx 1 sec.
March 14th, 2009 at 8:05 amdear sir,
July 7th, 2009 at 5:21 pmcould you pls provide information how to inteface a loadcell with atmega8 and sample code in c my aim is to display the weight in 16×2 lcd if possibl pls provide circut
Hey, Just a quick question. How would you change the delay? The way you have the delay set up is a bit different from what I’m use to. When I change the _delay_loop_2(0); It is much longer than one second. About 5 seconds. Please share your thoughts.
August 2nd, 2009 at 10:03 amBTW your tuts are amazing! I wish I saw them earlier.
@David
Please note TIMING depends on CPU speed. I clearly said it is for 12Mhz external crystal.
In this example timing didn’t matter much but in complex examples what you can get is black LCD screens (instead of an intended message) or no response to remote control signals, no resonse to data transfer. Because timming realy matters in those case!!!
August 2nd, 2009 at 10:20 am[...] numerical, textual and graphical data to user. For very simple numerical display we can use 7 segment displays. If the requirement is little more than that, like displaying some alphanumeric text, we can use [...]
October 11th, 2009 at 9:21 amDesign a seven segment display which displays “INDIA IS GREAT “with certain delay (say 5 sec) between successive letter displays in continuously……….
October 21st, 2009 at 6:00 pm………..
HELP ME PLEASE ITS URGENT
Thank you so much. This article helped me a lot.
October 24th, 2009 at 5:12 amthanks for your commitment.I appreciate your job to publish these informations about pic with 7 seg.wish u all the best.
May 6th, 2010 at 9:53 amthank you sir .the inormation in this article helped me a lot.
May 6th, 2010 at 4:08 pmthank you sir,this article has helped me a lot
May 19th, 2010 at 12:06 amWell, I couldn’t understand the followings lines:
SEVEN_SEGMENT_DDR=0xFF; //All output
SEVEN_SEGMENT_PORT=0xFF; //All segments off
I mean, we can use DDRD or PORTD, but what’s the logic behind using SEVEN_SEGMENT before them?? Is this some kind of macro?
Thanks…
June 14th, 2010 at 1:42 amMayank Prasad
hi,
June 23rd, 2010 at 10:30 amcould you please help me in coding the program to interface 4-digit 7 segment display using pic16f877a using mplab IDE software…
@Mayan
So that user can change the port assignment for displays easily !!!
If some one wants the disp on PORTA, he/she just need to change that in one place.
This process is called configuration.
If a program is really bit and complex, we have a separate config.h file.
So the user just need to open up the config.h file change the required options and Voilla the program is now ported for the new platform !!!
If the program is really really big. Then we supply a windows GUI software for editing the configuration file. So now the user just need to fire the GUI find a selection area for display, use the drop down box to simply select the seven segment port of his/her choise !!! If you have changed windows wallpaper or changed the rigtone of your mobile you must be know what ease these GUI provide!
June 23rd, 2010 at 11:38 amweahhhhh
August 7th, 2010 at 2:16 pm