Controlling a Seven-Segment Display Using Arduino Part 2

* This is a multi-part post. Here are links to all parts:

Part 1: Overview
Part 2: Bill of Materials and sketch #1; this post
Part 3: Sketch #2, using a pushbutton
Part 4: Sketch #3, bit manipulation

This month’s project is a simple program to display the Arabic numbers using the Arduino and a 7-segment display.

Parts list:

The 7-segment display used in this example is a common anode display with pin connections as shown in the picture, and the Arduino sketches were written so as to light up a segment when the corresponding pin is LOW.

The first of a series of three simple sketches cycles through the numbers from 0 to 9, resets the display to show a “0” once it reaches “9” and repeats until the power is turned off on the Arduino. The code below accomplishes this in a very simplified manner, as each digit is formed by a separate function that sequentially lights up each of the necessary segments to display that number.

The third sketch in this sequence will use individual bits to represent each segment and will require less written code, a good thing to keep in mind when programming microcontrollers, as they have a small memory footprint (the Arduino Duemilanove has 32K bytes of program memory).

Sketch #1:

// www.TinkerHobby.com
// Natalia Fargasch Norman
// Seven-segment LED Display
// Common Anode pins 3 and 8

//   G F + A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D + C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F_SEG 7
#define G 8

// Common anode;
// on when pin is low
// and off when pin is high
#define ON LOW
#define OFF HIGH

int ms = 1000;

void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F_SEG, OUTPUT);
pinMode(G, OUTPUT);
}

void loop() {
zero();
one();
two();
three();
four();
five();
six();
seven();
eight();
nine();
}

// 0 => ABCDEF
void zero() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, ON);
digitalWrite(F_SEG, ON);
digitalWrite(G, OFF);
delay(ms);
}

// 1 => BC
void one() {
digitalWrite(A, OFF);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, OFF);
digitalWrite(E, OFF);
digitalWrite(F_SEG, OFF);
digitalWrite(G, OFF);
delay(ms);
}

// 2 => ABDEG
void two() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, OFF);
digitalWrite(D, ON);
digitalWrite(E, ON);
digitalWrite(F_SEG, OFF);
digitalWrite(G, ON);
delay(ms);
}

// 3 => ABCDG
void three() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, OFF);
digitalWrite(F_SEG, OFF);
digitalWrite(G, ON);
delay(ms);
}

// 4 => BCFG
void four() {
digitalWrite(A, OFF);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, OFF);
digitalWrite(E, OFF);
digitalWrite(F_SEG, ON);
digitalWrite(G, ON);
delay(ms);
}

// 5 => ACDFG
void five() {
digitalWrite(A, ON);
digitalWrite(B, OFF);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, OFF);
digitalWrite(F_SEG, ON);
digitalWrite(G, ON);
delay(ms);
}

// 6 => ACDEFG
void six() {
digitalWrite(A, ON);
digitalWrite(B, OFF);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, ON);
digitalWrite(F_SEG, ON);
digitalWrite(G, ON);
delay(ms);
}

// 7 => ABC
void seven() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, OFF);
digitalWrite(E, OFF);
digitalWrite(F_SEG, OFF);
digitalWrite(G, OFF);
delay(ms);
}

// 8 => ABCDEFG
void eight() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, ON);
digitalWrite(F_SEG, ON);
digitalWrite(G, ON);
delay(ms);
}

// 9 => ABCDFG
void nine() {
digitalWrite(A, ON);
digitalWrite(B, ON);
digitalWrite(C, ON);
digitalWrite(D, ON);
digitalWrite(E, OFF);
digitalWrite(F_SEG, ON);
digitalWrite(G, ON);
delay(ms);
}
• João Alexandre da Silveira

I write a new version of this sketch for a common catode dispay. The comments are in portuguese, I am from Brazil. Here it:

/*
Digitos0a9
Mostra a cada segundo os digitos 0 a 9 no mostrador de 7 segmentos
*/
#define pausa delay(1000) //define uma pausa de 1 segundo

int digito0[7]={1,1,1,1,1,1,0}; //matriz dos segmentos do digito 0
int digito1[7]={0,1,1,0,0,0,0}; //digito 1
int digito2[7]={1,1,0,1,1,0,1}; //digito 2
int digito3[7]={1,1,1,1,0,0,1}; //digito 3
int digito4[7]={0,1,1,0,0,1,1}; //digito 4
int digito5[7]={1,0,1,1,0,1,1}; //digito 5
int digito6[7]={1,0,1,1,1,1,1}; //digito 6
int digito7[7]={1,1,1,0,0,0,0}; //digito 7
int digito8[7]={1,1,1,1,1,1,1}; //digito 8
int digito9[7]={1,1,1,1,0,1,1}; //digito 9

int i=0; //contador para a estrutura ‘for’

void setup(){
for(i=0; i<7; i++){
}
}

void loop(){
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
for (i=0; i<7; i++){
}
pausa;
}

• I’m originally from Brazil, too, Joao. Thanks for your input. Also, check out the final version I wrote where the segment array for each digit is bit packed and is unpacked using the bitRead function to light each segment. It just makes for shorter code and uses less memory. You can check it out here:
https://www.tinkerhobby.com/controlling-a-seven-segment-display-using-arduino-part-4-of-4/

• João Alexandre da Silveira

Here is another sketch to show the position of a 10k potentiometer in the Arduino analog pin 0 in 0-9 digits in a 7-segments display.

/*
potDigital
Mostra a posição de um potenciometro no mostrador de 7 segmentos
by João Alexandre da Silveira
http://www.OrdemNatural.Com
16 junho 2010
*/
#define pausa delay(1000) //define uma pausa de 1 segundo

int digito0[7]={1,1,1,1,1,1,0}; //matriz dos segmentos do digito 0
int digito1[7]={0,1,1,0,0,0,0}; //digito 1
int digito2[7]={1,1,0,1,1,0,1}; //digito 2
int digito3[7]={1,1,1,1,0,0,1}; //digito 3
int digito4[7]={0,1,1,0,0,1,1}; //digito 4
int digito5[7]={1,0,1,1,0,1,1}; //digito 5
int digito6[7]={1,0,1,1,1,1,1}; //digito 6
int digito7[7]={1,1,1,0,0,0,0}; //digito 7
int digito8[7]={1,1,1,1,1,1,1}; //digito 8
int digito9[7]={1,1,1,1,0,1,1}; //digito 9

int i=0; //contador para a estrutura ‘for’
int potPin=0; //potenciometro no pino 0
int conversor; //

void setup(){
for(i=0; i<7; i++){
}
}

void loop(){

switch (conversor) {
case 0:
for (i=0; i<7; i++){
}
break;

case 1:
for (i=0; i<7; i++){
}
break;

case 2:
for (i=0; i<7; i++){
}
break;

case 3:
for (i=0; i<7; i++){
}
break;

case 4:
for (i=0; i<7; i++){
}
break;

case 5:
for (i=0; i<7; i++){
}
break;

case 6:
for (i=0; i<7; i++){
}
break;

case 7:
for (i=0; i<7; i++){
}
break;

case 8:
for (i=0; i<7; i++){
}
break;

case 9:
for (i=0; i<7; i++){
}
break;
}
}

• Not to beat a dead horse, guys, but I find the use of delay in software servicing displays of any kind to be a disadvantage, really. The 5-10ms refresh delay needed for creating a stable non-flickering image on a multi-digit 7-segment display means that you’re forcing the processor to stay put for the time it would have taken it to execute a thousand or so more commands. In other words, the processor pauses exactly where it would have actually done a lot of useful work had you not paused it for the display’s sake.

Anyway, some time ago I’ve posted an Arduino sketch with a non-stopping display refresh:
https://elabz.com/arduino-nano-and-hp5082-7433-vintage-7-segment-led-display/
It can be used in projects where the display function is secondary to something else Arduino is supposed to be doing. Like I said, a lot of useful work can be done during the time otherwise taken by the display pause.

Keep up great work!

Cheers!

• Thanks for stopping by, and I appreciate your input. You are right about the delays. I guess what happens is that beginner hobbyists are not yet creating projects that need to maximize use of those processor cycles. At this point all I do is overkill and inefficient, but I still have fun! Your vintage display sure looks cool, and I’ve bookmarked the sketch. Thanks for sharing!

• melisha

ant digram have