Tag Archives: LED

Seven segment display interface with Arduino (Without library)

A seven-segment display is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays. In this tutorial you will learn about seven segment display interface with Arduino, its possibilities & Arduino code. Seven-segment displays may use a liquid crystal display (LCD), a light-emitting diode (LED) for each segment, an electrochromic display, or other light-generating or controlling techniques such as cold cathode gas discharge (Panaplex), vacuum fluorescent (VFD), incandescent filaments (Numitron), and others.

In a simple LED package, typically all of the cathodes (negative terminals) or all of the anodes (positive terminals) of the segment LEDs are connected and brought out to a common pin; this is referred to as a “common cathode” or “common anode” device. Hence a 7 segment plus decimal point package will only require nine pins, though commercial products typically contain more pins, and/or spaces where pins would go, in order to match standard IC sockets.

Characters on seven segment display

The segments of a 7-segment display are referred to by the letters A to G, where the optional decimal point (an “eighth segment”, referred to as DP) is used for the display of non-integer numbers.

You can display 0-9 decimals on seven segment display. The most common patterns used for each of these is:

To display hexadecimals, you have to turn on segments to represent A, B, C & D. Same can be displayed as;

So, using seven segment display you can easily display hexadecimal numbers from 0 to F. To represent same, you can refer following table:

Digit Display gfedcba abcdefg a b c d e f g
0 0 0x3F 0x7E on on on on on on  
1 1 0x06 0x30   on on        
2 2 0x5B 0x6D on on   on on   on
3 3 0x4F 0x79 on on on on     on
4 4 0x66 0x33   on on     on on
5 5 0x6D 0x5B on   on on   on on
6 6 0x7D 0x5F on   on on on on on
7 7 0x07 0x70 on on on        
8 8 0x7F 0x7F on on on on on on on
9 9 0x6F 0x7B on on on on   on on
A A 0x77 0x77 on on on   on on on
b b 0x7C 0x1F     on on on on on
C C 0x39 0x4E on     on on on  
d d 0x5E 0x3D   on on on on   on
E E 0x79 0x4F on     on on on on
F F 0x71 0x47 on       on on on

Apart from hexadecimals, there are few characters which are being used to display on seven segment display which are as below;

Case A B C D E F G H I J K L M N O P Q R S T U V W X Y
Upper A   C   E F G H I J   L     O P     S   U        
Lower   b c d       h i         n o   q r   t u       y

Few short messages giving some status information (e.g. “no dISC” on a CD player) are also usually represented on seven segment displays. In the case of such messages it is not necessary for every letter to be unambiguous, merely for the words as a whole to be readable. For example:

There are few decoder ICs available in market (like CD4511) which has BCD as input (4 pins) & seven segment pins as output. First we will see how to connect a seven segment display with Arduino without any external IC.

Seven segment display pinout

7 segment pinout

As you can see, all segment LEDs are connected with common pin & separate pinout pins for each LED. Above diagram is common cathode seven segment display, if it is common anode then LED polarity will be reversed compared to common cathode.

Seven segment display interface with Arduino

Seven segment display interface with Arduino

In above connection diagram, common cathode type seven segment is used to interface with Arduino. If you have common anode type seven segment display, you have to connect 

Usually, the display may run without any current limiting resistors. So it is advised to use for protection against high current & display segments burnout. Usually for a commonly used red color seven segment display, each LED segment can draw around 15 mA to illuminated properly, so for 5 volt digital logic circuit, the value of the current limiting resistor would be about 200Ω (5v – 2v) / 15mA, or 220Ω to the nearest preferred value. SO resistor value may vary as per seven segment display technical specifications. You can test each segment LED of your seven segment by applying +1.5V to 5V on each pin and common pin to ground.

Arduino code for seven segment display test without library

You can download above code from here.

Above code is for basic understanding seven segment display’s working operation by Arduino. Basically, it is just particular segment pin HIGH & LOW, but it may be more complex if you have more than one displays (that is different topic of seven segment display multiplexing and same will be posted on #beatyourbit in future). The code is to display 0-9 counts at interval of 1 second. The output will look like,

Thus, It may be little bit long but also without any library you can simply use seven segment display. Similar code you can write by directly writing on GPIO ports (Pure AVR method). Find below code with similar output as above:

You can download above code from here.

Understanding above code:

ATmega328p has total 3 No. of GPIO ports namely Port B, Port C & Port D. Port C is an analog port as it has total six pins which are usually used as A0 to A6 in Arduino. Port B & Port D are digital ports & each port has 7 pins hence total 14 digital pins (D0 to D13). here in above code we have used port addressing, the basic method of AVR programming.

I hope you will learn to use single seven segment display by this tutorial & it will be helpful to display some counts or characters in your DIY stuff. In future you can use external seven segment decoders ICs to run such seven segment displays using minimum pins and also multiplexing of multiple seven segment displays; such tutorials will be uploaded soon on #BeatYourBit website. If you like this tutorial, share it with your friends & any Arduino newbie you know. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Like/Follow @beatyourbit on FacebookTwitterInstagram & Telegram for latest updates.