7 segment display and Arduino


I experienced a bit with the persistence of vision effect(POV), using a 7 segment LED display. The one I used is a single digit numeric display, Kingbright SC56-11EWA. Essentially I wanted to see the effect going from slow to fast “live”, so I can better understand it. For this I used a potentiometer to slowly change the delay between turning on and turning off each of the segments. When the delay is long enough, it simply looks like turning on and turning off the segments. As I make it shorter, the light seems to chase around the display. Finally, when the delay has passed a limit, it seems like the whole display is lit. You can see the effect in the animated gif at the right( unfortunately the low framerate of the gif does not display the effect in its whole beauty).

I don’t think it’s necessary to present you a schematic( ok, I’m lazy), since it’s not complicated to hookup the display to Arduino, it’s just that there are a lot of connections to make. I used a 100? resistor for each segment. The Arduino code is also really simple… I basically set the potentiometer value as the delay value( it goes from 0 to 1024), and then set a segment to HIGH, delay, set the segment to LOW and proceed with the next segment in the same manner.

It seems that this effect is massively used in a LED matrix.

Then, just out of boredom I wanted to simply animate it using its original purpose: displaying numbers. There’s a lot of copy-pasting involved as you have to create the pattern for each number. For this reason, I’m going to put the code in this article for anybody who wants to adapt it to his/her own project. Keep in mind that the segments were connected in their alphabetical order( a through g) to their coresponding pins from 2 to 8.

/* this noob friendly way of displaying alphanumeric codes 
on the 7segment uses functions for each character
 a more efficient way would be using a byte array,
 there's a link at the bottom of the page */
void zero(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);}

void one(){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);}

void two(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);}

void three(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);}

void four(){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);}

void five(){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);}

void six(){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);}

void seven(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);}

void eight(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);}

void nine(){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);}

int delaytime= 650;

void setup(){
pinMode(2, OUTPUT); 
pinMode(3, OUTPUT);
pinMode(4, OUTPUT); 
pinMode(5, OUTPUT);
pinMode(6, OUTPUT); 
pinMode(7, OUTPUT); 
pinMode(8, OUTPUT); }

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

I realize that this is the lazy or easier way to understand version of how this can work. Here’s a more thorough tutorial on how to do this at hacktronics.

Advertisements

3 thoughts on “7 segment display and Arduino

  1. About the displaying number on 7 segments, maybe it’s easier if you declare an unsigned array with the hex values of configurations from 0 to 9 and then write those configurations on microcontroller’s port. Also, you can reuse the array of configurations if you wire another display. For example if your display is common cathode, then you have to command it with 1 and in common anode with 0. If you have the hex values, you just have to invert them ( logical not).

    1. I don’ think it’s easier, I think it’s more practical. I agree that the method I used here is extremely lazy, but also very clear in what it’s doing. It’s also consuming a lot of memory, so yes, again, I wouldn’t use this in a real project.

      I see you moved your blog to a new address. I was wondering why the old link didn’t work. Seems like you’ve been really active this year :).

      Reformatted the code btw. The old one looked ugly and inconsistent with the new theme.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s