The 4 digit 7 segment display in an intermediate level project. This project is mainly focused on how to connect the 4 digit 7 segment display with the Arduino. Once you have learnt the connection and the logic of working. Nobody can stop you from reprogramming the 4 digit 7 segment display.
To get started we will need an Arduino (Uno or whichever one you have that would not make a difference). But ideally as the number of pins increase on our Arduino boards the capability is higher and better projects can be built.
Don’t know Arduino yet? Click here to learn about the Pins and other ports available on it.
Things Needed
- 4 digit 7 segment display
- Arduino Uno
- Breadboard
- Jumper cables
- Resistors x 4 (200Ω)
Circuit Diagram
Code
Please download the SevSeg.h library before running the code. Click here!
#include <SevSeg.h>
int pinA = 11;int pinB = 7;int pinC = 4;int pinD = 2;
int pinE = 1;int pinF = 10;int pinG = 5;
int pinDP = 3;int D1 = 12;int D2 = 9;int D3 = 8; int D4 = 6;
void setup() {//set all segments & digits as outputs
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
}
void loop() {
digit1();zero();delay(500);
digit2();one();delay(500);
digit3();two();delay(500);
digit4();three();delay(500);
digit3();four();delay(500);
digit2();five();delay(500);
digit1();six();delay(500);
digit2();seven();delay(500);
digit3();eight();delay(500);
digit4();nine();delay(500);
all4Digits();
allNumbers();
}
//functions representing numbers 0-9
void zero(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}
void one(){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void two(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
}
void three(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
}
void four(){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void five(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void six(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void seven(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void eight(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void nine(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void allNumbers(){
one();delay(500);
two();delay(500);
three();delay(500);
four();delay(500);
five();delay(500);
six();delay(500);
seven();delay(500);
eight();delay(500);
nine();delay(500);
}
void turnOffAllSegments(){
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
//functions to select individual digits on the display
void digit1(){
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
}
void digit2(){
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
}
void digit3(){
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
}
void digit4(){
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
}
void all4Digits(){
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
}
It says SevSeg.h no such file or directory. that what it all says.
Above the code the header file is linked. Please add the header files before using the code.
How to add? I`m beginner. Please help with more info as 12 years old like me.
Resolved, thanks for the help.
How do You make the 7-segment countdown at least three digits?
How do You make the 7-segment countdown at least three digits?
I ended up having a 16-pin 7-segment display. Will the code above work on it?
Actually the same code won’t work right away.
Thank you for your help anyway. I have looked at the pinout diagram of my display and rearranged the cables accordingly so that the numbers are displayed correctly.
One more question – how do I get a number with 2 digits or more (e.g., 16) to show on the display without having to alternate between each digit?
Thank you for your help anyway. I have looked at the pinout diagram of my display and rearranged the cables accordingly so that the numbers are displayed correctly.
One more question – how do I get a number with 2 digits or more (e.g., 16) to show on the display without having to alternate between each digit?
Sorry if I clicked on the Post Comment button twice – it was an accident.
You need to create separate functions to display two digits.
I got around the issue by implementing a very small delay, such as this:
void loop() {
digit4();
one();
delay(4);
digit3();
six();
delay(4);
}
It’s a bit of a drawback that if you try to display all four different numbers, the LEDs get dimmer, but with only two digits you shouldn’t notice too much of a difference. Hope this helps 🙂
Does anyone know a code for this setup to create a working clock with it?
I wanted to do the same thing and wrote the following, but I had some problems with the source code. I guess it’s because I’m using a different 7-segment display, but I had to change all the HIGHs to LOWs and LOWs to HIGHs, so if the original code works with your display, you would probably need to change that back again. Also, the E-Segment – the one that’s connected to the first pin – didn’t turn off at all, so I plugged it into the 13th pin. Nevertheless, now it works : )
#include
int pinA = 11;
int pinB = 7;
int pinC = 4;
int pinD = 2;
int pinE = 13; // I changed this pin from 1 to 13, since my first pin was always on HIGH – no idea why
int pinF = 10;
int pinG = 5;
int pinDP = 3;
int D1 = 12;
int D2 = 9;
int D3 = 8;
int D4 = 6;
// Variables
int minCount;
int tenMinCount;
int hourCount;
int tenHourCount;
int timer;
void setup()
{
Serial.begin(9600);
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
minCount = 0; // Here you can input where the minute-segment should start (0-9)
tenMinCount = 0; // Here you can input where the ten-minute-segment should start (0-5)
hourCount = 0; // Here you can input where the hour-segment should start (0-9 if tenHourCount (249*60)) // I tested the clock by displaying seconds instead of minutes, so if you want to test it too, just delete the *60
{
ClockCounter();
timer = 0;
}
}
void ClockCounter()
{
minCount++;
if (minCount > 9) {
minCount = 0;
tenMinCount++;
if (tenMinCount > 5)
{
tenMinCount = 0;
hourCount++;
if (hourCount > 9 && tenHourCount != 2)
{
hourCount = 0;
tenHourCount++;
}
else if (hourCount > 3 && tenHourCount == 2)
{
hourCount = 0;
tenHourCount = 0;
}
}
}
Serial.println(minCount);
Serial.println(tenMinCount);
Serial.println(hourCount);
Serial.println(tenHourCount);
}
void minDisplay()
{
digit4();
if (minCount == 0)
{
zero();
}
else if (minCount == 1)
{
one();
}
else if (minCount == 2)
{
two();
}
else if (minCount == 3)
{
three();
}
else if (minCount == 4)
{
four();
}
else if (minCount == 5)
{
five();
}
else if (minCount == 6)
{
six();
}
else if (minCount == 7)
{
seven();
}
else if (minCount == 8)
{
eight();
}
else if (minCount == 9)
{
nine();
}
}
void tenMinDisplay()
{
digit3();
if (tenMinCount == 0)
{
zero();
}
else if (tenMinCount == 1)
{
one();
}
else if (tenMinCount == 2)
{
two();
}
else if (tenMinCount == 3)
{
three();
}
else if (tenMinCount == 4)
{
four();
}
else if (tenMinCount == 5)
{
five();
}
}
void hourDisplay()
{
digit2();
digitalWrite(pinDP, HIGH);
if (hourCount == 0)
{
zero();
}
else if (hourCount == 1)
{
one();
}
else if (hourCount == 2)
{
two();
}
else if (hourCount == 3)
{
three();
}
else if (hourCount == 4)
{
four();
}
else if (hourCount == 5)
{
five();
}
else if (hourCount == 6)
{
six();
}
else if (hourCount == 7)
{
seven();
}
else if (hourCount == 8)
{
eight();
}
else if (hourCount == 9)
{
nine();
}
}
void tenHourDisplay()
{
digit1();
digitalWrite(pinDP, LOW);
if (tenHourCount == 0)
{
zero();
}
else if (tenHourCount == 1)
{
one();
}
else if (tenHourCount == 2)
{
two();
}
}
//functions representing numbers 0-9
void zero() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
}
void one() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void two() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}
void three() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
}
void four() {
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void five() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void six() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void seven() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
void eight() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void nine() {
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
}
void digit1() {
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
}
void digit2() {
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
}
void digit3() {
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
}
void digit4() {
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
}
Oh, wow, sorry for this wall of text. I didn’t realize it would be this long.
Using your code im getting a compilation error: exit status 1. Any idea how to fix? thanks 🙂
Using your code im getting a compilation error: exit status 1. Any idea how to fix? thanks 🙂
adding an empty Loop at the end had gotten rid of the error but the clock still doesn’t work
I need help even I installed the SevSeg.h library and added to the libraries of arduino it still says.
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: “Arduino Uno”
try_arduino:1:10: fatal error: SevSeg.h: No such file or directory
#include
^~~~~~~~~~
compilation terminated.
exit status 1
SevSeg.h: No such file or directory
you go to the side of arduino ide and install the library SevSeg