Interface Arduino with 7-segment display | 4-Digit counter example

This post shows how to interface Arduino UNO board with 7-segment display in order to build a simple 4-digit counter which counts from 0 to 9999. A push button connected to Arduino is used to increment the displayed number.

There are two types of the seven-segment displays: common anode and common cathode.
In the common anode type all the 7 LED anode terminals are connected together whereas in the common cathode all cathode terminals are connected together.

The common terminal is connected to +VCC (+5V, +3.3V …) or GND (0V) depending on the type of the 7-segment display (common anode or common cathode respectively).

Basically for each 7-segment digit there are 8 pins: one for the common terminal (anode or cathode) and 7 pins for the 7 segments (A, B, C, D, E, F and G). Another pin may be used for the decimal point (DP).

one-digit seven-segment display

In multi-digit 7-segment display (for example 4-digit) all pins of the same segment are connected together (segment A of digit 1 with segment A of digit 2 …), and each digit has its common pin alone. This is called multiplexing technique. This technique minimizes number of pins used.

So for a 4-digit display we’ll have 7 pins of the 7 segments, 4 pins of the 4 digits (common terminals) and 1 pin for the decimal point (DP) which means a total of 12 pins.

Hardware Required:

  • Arduino UNO board
  • 4-Digit common anode 7-segment display
  • 4 x PNP transistor (2SA10152S90152N3906 …)
  • 7 x 100 ohm resistor
  • 4 x 4.7k ohm resistor
  • Push button
  • Breadboard
  • Jumper wires

Interfacing Arduino with 7-segment display circuit:
Example circuit schematic diagram is shown below.

Arduino with common anode 7-segment display | Arduino 7 segment

The push button which is connected to Arduino A0 pin is used to increment the displayed number.
A common anode 7-segment display is used in this example.

In the circuit there are 4 transistors of the type PNP, the collector of each transistor is connected to common anode pin of 1 digit. That means each transistor supplies one digit segments.
The 4 transistors are used to supply the display LEDs with sufficient current because Arduino microcontroller (ATmega328P) may not be able to do that (maximum output current is 40mA).
Each transistor emitter pin is connected to +5V that comes from the Arduino board and each transistor base is connected to the Arduino through 4.7k resistor.

Each 100 ohm resistor is used for limiting the current that passes through the segment LED.

Interfacing Arduino with 7-segment display code:
Example Arduino code is below.

Since the 4 digits are multiplexed we need to refresh the display very quickly (display one digit at a time, others are off). For that I used Timer1 module interrupt with the following configuration:

With the above configuration Timer1 module overflows every 4096 microseconds (4096 = 65536/16) which is a good refresh period.

Note that Timer1 module is 16-bit timer, prescaler = 1 (TCCR1B = 1) and Arduino UNO clock = 16MHz.

The following video shows a simple protoboard circuit of the example:

and the video below shows Proteus simulation (simulation circuit is not the same as real hardware circuit, example circuit diagram is shown above):

Proteus simulation file download:
Arduino 7-segment display counter

Other Arduino projects where 7-segment display was used:
Print Arduino ADC values on 7-segment display
Arduino with rotary encoder and 7 segment display
Arduino with LM335 temperature sensor and seven-segment display
Interfacing Arduino with LM35 sensor and 7-segment display
7-Segment display with 74HC595 shift register | Arduino Projects

17 thoughts on “Interface Arduino with 7-segment display | 4-Digit counter example”

  1. Hi guys,

    I have tried to upload the code and this error code popped up. If anybody knows how to fix it that would be great.

    sketch_feb08a:82:17: error: expected ‘)’ before ‘:’ token

    if(count &gt: 9999)

    ‘gt’ was not declared in this scope

  2. This code can’t be verified as the disp_off(); hasn’t been defined. I’d offer up more information but I’m not entirely sure as to what it means.

    1. I have gotten past this now but I am struggling to get the button to increment the counter. I have adjusted the code for a common cathode display, with NPN and grounding instead of pulling up to 5V but I’m still missing something to get the button to have any effect.

  3. If you are using the example code from this project, and using serial.parseint() to supply the value for ‘count’, it should work. You should be able to read and display values from 0 to 9999. If you share your code, someone might be able to identify why it isn’t functioning as expected.

  4. Hi, can anyone tell me how to take any number input from the user and display that on the 7 segment 4 number display? I used serial.parseint(), but its not showing the digits in the tens, hundredths and the thousandths place.

  5. In this example to show 2056 on the display we need <= 2055 button presses.
    Can another switch be added to select the digits(ones tens hundreds and thousands and the increment the number on each digit. The number will increase upto 9 and roll over to 0 but will not go to next digit.

    1. Basically, choosing a transistor type of NPN or PNP depends on circuit design. In this project we’ve a common anode 7-segment display. This means each common pin has to be connected to +5V in order to turn on its related digit.
      The NPN transistor type allows current flows from collector to emitter when base-emitter voltage is greater than threshold voltage (about 0.7V).
      The PNP transistor allows current flows from collector to emitter when base-emitter voltage is lower than threshold voltage (about -0.7V).
      In this project if we use a NPN type then its collector will be connected to +5V and its emitter to the common pin of 7-segment display. Normally, the NPN transistor turns on when the Arduino outputs logic high (+5V). If there’s a 5V at base and 5V at emitter how will the transistor turn on?
      You can use NPN type but the 7-segment display will not be supplied correctly with 5V (about 4 Volts) which yields low brightness.
      If you have a common cathode 7-segment display then you should use NPN type.

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