How To Use a 4-Digit 7-Segment Display in Arduino Project
A seven-segment display is one of the simplest and cheapest electronic display units. It is used to display numerical output that has few place values or decimal places. 7-segment indicators exist in different forms. Some of the common types are one-digit, 2-digits, 3-digits, and 4-digits display units. Of those that have more than one place value, there are those that have decimal points and colons. The seven-segment display units with decimal points are ideal for displaying numerical output with few decimal places while those that have colons are mostly used to display time.
Though simple in design, a 7-segment display unit can be difficult to interface with a microcontroller, especially because of the many connection pins. It is even more difficult to interface multiple-digit units because they have more pins. In this article, I will teach you how to interface a 4-digit seven-segment indicator with Arduino microcontroller. We will then use it to display the temperature measured from the LM35 sensor.
Parts
- 4-digit seven-segment display unit
- LM35 temperature sensor
- Arduino
- Wire jumpers
- Breadboard
Circuit diagram
Code
/**
* Author : Julioceaseless
* Date : September 2019
* Description : Display temperature from LM35 temperature sensor
* and display it on a 4-Digit Seven Segment Display
* Website : https://nandgeek.com
**/
// Pin 2-8 is connected to the 7 segments of the display.
#define ADCPIN 0
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
//common pins
int D1 = 9;
int D2 = 10;
int D3 = 11;
int D4 = 12;
//decimal pin
int Dot = 13;
double Temperature = 0;
int temp = 0;
int placeValuesofTemp[4];
int tempRefresh = 1000; //refresh temperature every 1 second
int sevSegRefresh = 5;
unsigned long time_now = 0;
// the setup routine runs once when you press reset:
void setup() {
analogReference(INTERNAL);
// initialize the digital pins as outputs.
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(Dot, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read temperature every second
if(millis() > time_now + tempRefresh ){
time_now = millis();
//read from temperature sensor
Temperature = analogRead(ADCPIN);
Temperature = Temperature*1100/(1024*10);
temp = Temperature*100;
//put temp digits by their place value
placeValuesofTemp[3] = ((temp)/1)%10;
placeValuesofTemp[2] = ((temp)/10)%10;
placeValuesofTemp[1] = ((temp)/100)%10;
placeValuesofTemp[0] = ((temp)/1000)%10;
} //end millis() if statement
//seven segment 0 - 0 digits
int number [10][7] = {
{1, 1, 1, 1, 1, 1, 0}, //0
{0, 0, 0, 0, 1, 1, 0}, //1
{1, 1, 0, 1, 1, 0, 1}, //2
{1, 1, 1, 1, 0, 0, 1}, //3
{0, 1, 1, 0, 0, 1, 1}, //4
{1, 0, 1, 1, 0, 1, 1}, //5
{1, 0, 1, 1, 1, 1, 1}, //6
{1, 1, 1, 0, 0, 0, 0}, //7
{1, 1, 1, 1, 1, 1, 1}, //8
{1, 1, 1, 0, 0, 1, 1}, //9
};
//
int commonPin [5][4]= {
{0, 1, 1, 1},//enable digit 1
{1, 0, 1, 1},//enable digit 2
{1, 1, 0, 1},//enable digit 3
{1, 1, 1, 0},//enable digit 4
{0, 0, 0, 0},//enable all
};
for (int n = 0; n<4; n++){
for (int x = 0; x < 4; x++ ){
digitalWrite(x+9, commonPin[n][x]);
(n==1)?digitalWrite(Dot, 1):digitalWrite(Dot, 0);//display decimal after second digit
}//end for loop for x
//display 4 digits
for (int j = 0; j < 7; j++){//loop seven segments
digitalWrite(j+2,number[placeValuesofTemp[n]][j] );
}
delay(sevSegRefresh);//seven segment refresh rate
}//end n for loop
}
Jim Martino
May 6, 2020the muliplexing part is not working…..numbers are flashing and making no sense
can you rewrite a code using the commonly available SevSeg library?
Thanks
Julioceaseless
Post author June 18, 2020I will try to use SevSeg library also. However, you can adjust the delay() at the end of the program to see if the blinking stops. Probably reducing the number will make the display a bit more steady
Ricardo
November 3, 2020Did you have any luck fixing those flashings? It is happening the same thing to me.