In this tutorial we are going to learn how to interface 7 segment displays (common cathode & common anode) with Arduino by using “SevSeg” library and the concept using multiplexing. We will learn how to interface one to four digit-7 segment displays with Arduino.
We will see:
- Understanding the Fundamentals.
- What is Multiplexing?
- How Multiplexing Works?
- Circuit Diagram.
- Program and Explanation.
- Digital Clock Using Arduino and 7 Segment Display.
Understanding the Fundamentals:
Seven segment displays come across our daily life with a finished product like: microwave oven, digital clocks, speedometers etc. Most of them are controlled by some sort of microcontroller; by understanding how it works we can implement it on our own projects in efficient way like reduced cost, power consumption and number of wires that connects to 7 segment displays.
All the commercial products which employees 7 segment display are multiplexed and are either common cathode or common anode type. Multiplexing helps in reducing the power consumption of any battery operated device significantly, with the power consumption of single 7 segment display we can power an array of 7 segment displays.
What are Common Cathode and Common Anode types?
A common cathode 7 segment display whose all the cathode terminals (-) are connected to ground and the anode terminals (+) are controlled individually by an IC or microcontroller. In common anode, all the anodes of the LEDs are connected to (+) 3V and cathode terminals are individually controlled by microcontroller.
Internal Diagram of 7 Segment Display:
What is Multiplexing of 7 Segment Display?
Multiplexing is a technique where different data signals are passed through a common path or a common group of wires (called bus) which are shared with different destinations.
For example: Let us assume we have devices A, B and C let these devices be any communication devices and A, B and C need 5 wires each for data communication. Now instead of wiring up 5 + 5 + 5 = 15 wires, we will connect only 5 wires for all the three devices. Now you may wonder since the entire devices share the same 5 wires how do we prevent the wrong data stream entering wrong devices? Good question, this problem is addressed with “select lines”.
“Select Lines” are the wires which enable or disable the communication to the individual devices, since we have 3 devices here, we will have 3 select lines. When device ‘A’ is selected the B and C device are disabled, we will send a data stream via 5 wires which will reach the destination device ‘A’ and B and C will ignore the data stream. Similarly when device ‘B’ is selected, now A and C are disabled and ‘A’ and ‘C’ will ignore the incoming data stream.
So we just need 5 (data) + 3 (select) = 7 wires instead of 15 wires.
The exact same concept is applied to 7 segment displays. Below is the circuit diagram of 4 digit-7 segment displays which are multiplexed:
Each 7 segment display has 8 data lines (A, B, C, D, E, F, G and one decimal point) which light up 8 individual LEDs, to display four digits we need 8 x 4 = 32 wires when not multiplexed.
Multiplexing of 7 segment display involve connecting each segments together as illustrated in the diagram. The select lines are the common terminals of a single display. Now the wire count reduced to 8 (data lines) + 4 (select lines) = 12 wires instead of 32 wires.
How Multiplexing on 7 segment display is done?
Let’s assume we want to display “9867” on a 4 digit display, to do that the microcontroller applies LOW signal (to common cathode terminal) to the first digit and apply +Ve signals to each segments to light up the LEDs to show digit “9”, after few milliseconds it stop applying LOW signal to the first digit and applies LOW signal to the second digit and also applies +Ve signals to each segments to show digit “8” similarly for 3rd to show digit “6” and 4th digit to show “7” and the cycle repeats.
One important thing to be noted is that at any given instant only one digit lights up and rest of the 3 digits stays OFF, but due to the “persistence of vision” we will perceive that all the four digits are lit up simultaneously as shown below:
Circuit Diagram for multiplexing 7 segment display with Arduino:
If you don’t want to connect 4 individual 7 segment display together with wires as illustrated above, you may purchase ready made 4 digit 7 segment display like shown below:
Now you know how a multiplexed 7 segment display works, now let’s see how to write code for Arduino.
Here we are using “SevSeg” library which will take care of multiplexing.
Download SevSeg Library: Click here
Program code for displaying static digits:
The above code will display a static number “9867” on a 4 digit-7 segment display.
Now let’s see the explanation for the code:
- To select the number of digits:
4 for four digits, 3 for three digits, 2 for two digits and 1 for single-7 segment display.
- Selecting pin numbers for select lines:
Here the select lines are common terminal of a seven segment display, if you are using only two digits we can write as:
These pins are user selectable and you can change the pin numbers as per your requirements.
- Selecting pin numbers for data lines, that is: A, B, C, D, E, F, G and DP:
- If you are using a common cathode display then,
- If you are using a common anode display then,
- To set display brightness:
100 is full bright and 0 is lowest brightness.
- The below line will print number to 7 segment display:
- If you want to print floating numbers then,
This will print as 9.876 on 4 digit-7 segment display.
- The below line updates the data on the 7 segment display:
Now let’s see how to make simple 7 segment display counter with 4 digits:
The circuit diagram is same here; we just need to upload a new code:
Program code for counter (0 to 9999):
The above code will count from 0 to 9999 and resets to zero and counts again, each count is incremented every 250 milliseconds.
we should not use Delay() function here, this will interrupt the multiplexing process and will display broken digits. Instead of using Delay(), learn how to use millis() function and how to generate delay for any process without actually pausing the loop.
Digital Clock Using Arduino and 7 Segment Display:
Using this library we can build any 7 segment display projects like this cute little digital clock. Click here for full project in detail.
If you have any question regarding this tutorial feel free to ask us in the comment section, you will get a guaranteed reply from us.