Drive a 7-Segment LED with an Arduino

Last week I took a day off work and started experimenting with Arduino. Arduino is an open-source electronics prototyping platform that enables anyone to create interactive objects. During the week I started doing basic stuff using simple components such as blinking LEDs and playing tunes with a buzzer, and yesterday I experimented with multiple LEDs. After that I wanted to try and do something with a 7-Segment LED display so I found this tutorial that enabled me to make the circuit and learn the code that should drive the display.

Unfortunately the provided code did not work and I had to modify a lot of parts to finally get the display to work. I had to invert the “HIGH” and “LOW” params in all the function calls and change the way the constants are defined in the beginning.

I can’t really explain to you why there is such a difference between my working code and the code provided in the article I based myself on since I’m still a beginner. I might be using a different display and a different version of the IDE. When I’m advanced enough to figure that out I’ll make sure to update the article. If you can explain the reason to me please do so in the comments.

Here’s the updated code, I also created an additional function that plays a little “snake” animation on the display.

//7-segment LED by eliedh.com
const int A = 8;
const int B = 9;
const int C = 2;
const int D = 3;
const int E = 4;
const int F = 5;
const int G = 6;
void clr()
{
//Clears the LED
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void drawLoop()
{
digitalWrite(G, HIGH);
delay(100); 
digitalWrite(C, LOW);
delay(100);
digitalWrite(A, HIGH);
delay(100);
digitalWrite(G, LOW);
delay(100); 
digitalWrite(B, HIGH);
delay(100);
digitalWrite(A, LOW);
delay(100);
digitalWrite(E, HIGH);
delay(100);
digitalWrite(B, LOW);
delay(100);
digitalWrite(D, HIGH);
delay(100);
digitalWrite(E, LOW);
delay(100);
digitalWrite(C, HIGH);
delay(100);
digitalWrite(D, LOW);
delay(100);
}
void char_A()
{
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void char_B()
{
//Displays B
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_C()
{
//Displays C
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_D()
{
//Displays D
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void char_E()
{
//Displays E
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_F()
{
//Displays F
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_G()
{
//Displays C
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_H()
{
//Displays H
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void char_I()
{
//Displays I
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
void char_J()
{
//Displays J
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void char_L()
{
//Displays L
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void char_R()
{
//Displays R
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void one()
{
//Displays 1
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void two()
{
//Displays 2
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void three()
{
//Displays 3
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
void four()
{
//Displays 4
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
void five()
{
//Displays 5
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
}
void six()
{
//Displays 6
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
}
void seven()
{
//Displays 7
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
void eight()
{
//Displays 8
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void nine()
{
//Displays 9
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
}
void zero()
{
//Displays 0
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
}
void LoopDisplay()
{
//Start with a small "animation"
drawLoop();
//Loop through all Chars and Numbers
char_A();
delay(1000);
char_B();
delay(1000);
char_C();
delay(1000);
char_D();
delay(1000);
char_E();
delay(1000);
char_F();
delay(1000);
char_G();
delay(1000);
char_H();
delay(1000);
char_I();
delay(1000);
char_J();
delay(1000);
char_L();
delay(1000);
char_R();
delay(1000);
one();
delay(1000);
two();
delay(1000);
three();
delay(1000);
four();
delay(1000);
five();
delay(1000);
six();
delay(1000);
seven();
delay(1000);
eight();
delay(1000);
nine();
delay(1000);
zero();
delay(1000);
}
void setup()
{
//Setup our pins
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
Serial.begin(9600);  //Begin serial communcation
}
void loop()
{
Serial.println("Starting\n");
LoopDisplay();
}

In case you’re wondering, I got the Arduino board, the 7-segment display and a lot of other cool components in this dx.com Arduino starter kit. (I’m not sure if this code will only work with 7-segment displays bought from dealextreme).