Go Down

### Topic: 4 digit 7 segment display (Read 4997 times)previous topic - next topic

#### in2tech

##### Nov 27, 2014, 07:08 amLast Edit: Nov 27, 2014, 07:12 am by in2tech
I just received my kit about 2 weeks ago and just got sent a new 4 digit 7 segment display. I am new so I am wondering if this code is just suppose to display solid 8's? It appears to be a cathode anode display according to the code and the number on it is SH5461AS. I thought this code would count or something. As you can see I am new to Arduino and all of this! Hey at least I figured out how to light up all the segments as they did not all work at first because of some wiring issues by me!

I hooked it up exactly like the tutorial image! All I have lit up is all solid 8's, on all 4 digit's!

Code: [Select]
`//set the cathode interfaceint a = 1;int b = 2;int c = 3;int d = 4;int e = 5;int f = 6;int g = 7;int p = 8;// set the anode interfaceint d4 = 9;int d3 = 10;int d2 = 11;int d1 = 12;//set variablelong n = 0;int x = 100;int del = 55;  //fine-tuning value for clockvoid setup(){  pinMode(d1, OUTPUT);  pinMode(d2, OUTPUT);  pinMode(d3, OUTPUT);  pinMode(d4, OUTPUT);  pinMode(a, OUTPUT);  pinMode(b, OUTPUT);  pinMode(c, OUTPUT);  pinMode(d, OUTPUT);  pinMode(e, OUTPUT);  pinMode(f, OUTPUT);  pinMode(g, OUTPUT);  pinMode(p, OUTPUT);} void loop(){  clearLEDs();  pickDigit(1);  pickNumber((n/x/1000)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(2);  pickNumber((n/x/100)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(3);  dispDec(3);  pickNumber((n/x/10)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(4);  pickNumber(n/x%10);  delayMicroseconds(del);   n++;   if (digitalRead(13) == HIGH)  {    n = 0;  }} void pickDigit(int x)  //define pickDigit(x),to open dx port{  digitalWrite(d1, LOW);  digitalWrite(d2, LOW);  digitalWrite(d3, LOW);  digitalWrite(d4, LOW);   switch(x)  {  case 1:     digitalWrite(d1, HIGH);     break;  case 2:     digitalWrite(d2, HIGH);     break;  case 3:     digitalWrite(d3, HIGH);     break;  default:     digitalWrite(d4, HIGH);     break;  }} void pickNumber(int x)   //define pickNumber(x)to display number x{  switch(x)  {  default:     zero();     break;  case 1:     one();     break;  case 2:     two();     break;  case 3:     three();     break;  case 4:     four();     break;  case 5:     five();     break;  case 6:     six();     break;  case 7:     seven();     break;  case 8:     eight();     break;  case 9:     nine();     break;  }} void dispDec(int x)  //set to start the decimal point{  digitalWrite(p, LOW);} void clearLEDs()  //clear contents{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);  digitalWrite(p, HIGH);} void zero()  //define 0 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, HIGH);} void one()  // define 1 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void two()  // define 2 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, LOW);} void three()  // define 3 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, LOW);} void four()  // define 4 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void five()  // define 5 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, HIGH);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void six()  // define 6 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void seven()  // define 7 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void eight()  // define 8 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void nine()  // define 9 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, HIGH);  digitalWrite(f, LOW);  digitalWrite(g, LOW);}`

#1
##### Nov 27, 2014, 07:15 am
You have 5643A? Common cathode.

5643B is the common anode.

May just need to swap the level the active anode uses - low vs high, and possibly the levels for the cathodes (lows to highs, and highs to lows).
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### in2tech

#2
##### Nov 27, 2014, 07:26 am
Sorry wasn't sure about the cathode and anode thing. You mean just change the code to where ever it says High to Low, or something more specific? Sorry brand new to this and the Arduino. Can you tell me exactly what code needs to be changed please?

#3
##### Nov 27, 2014, 07:28 am
Yes, High to Low, and Low to High. Any place that has to do with Anode pin or Cathode pin.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### in2tech

#4
##### Nov 27, 2014, 07:50 amLast Edit: Nov 27, 2014, 08:14 am by in2tech
Changed the High's to Low's and Low's to High's and no change. Still solid 8's and decimal points. Is the original code suppose to count down or something. Sorry, but brand new to this! Here is the Youtube video of the actual project:

Arduino 4 Digit 7 Segment Youtube!

Code: [Select]
`//set the cathode interfaceint a = 1;int b = 2;int c = 3;int d = 4;int e = 5;int f = 6;int g = 7;int p = 8;// set the anode interfaceint d4 = 9;int d3 = 10;int d2 = 11;int d1 = 12;//set variablelong n = 0;int x = 100;int del = 55;  //fine-tuning value for clockvoid setup(){  pinMode(d1, OUTPUT);  pinMode(d2, OUTPUT);  pinMode(d3, OUTPUT);  pinMode(d4, OUTPUT);  pinMode(a, OUTPUT);  pinMode(b, OUTPUT);  pinMode(c, OUTPUT);  pinMode(d, OUTPUT);  pinMode(e, OUTPUT);  pinMode(f, OUTPUT);  pinMode(g, OUTPUT);  pinMode(p, OUTPUT);} void loop(){  clearLEDs();  pickDigit(1);  pickNumber((n/x/1000)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(2);  pickNumber((n/x/100)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(3);  dispDec(3);  pickNumber((n/x/10)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(4);  pickNumber(n/x%10);  delayMicroseconds(del);   n++;   if (digitalRead(13) == HIGH)  {    n = 0;  }} void pickDigit(int x)  //define pickDigit(x),to open dx port{  digitalWrite(d1, LOW);  digitalWrite(d2, LOW);  digitalWrite(d3, LOW);  digitalWrite(d4, LOW);   switch(x)  {  case 1:     digitalWrite(d1, HIGH);     break;  case 2:     digitalWrite(d2, HIGH);     break;  case 3:     digitalWrite(d3, HIGH);     break;  default:     digitalWrite(d4, HIGH);     break;  }} void pickNumber(int x)   //define pickNumber(x)to display number x{  switch(x)  {  default:     zero();     break;  case 1:     one();     break;  case 2:     two();     break;  case 3:     three();     break;  case 4:     four();     break;  case 5:     five();     break;  case 6:     six();     break;  case 7:     seven();     break;  case 8:     eight();     break;  case 9:     nine();     break;  }} void dispDec(int x)  //set to start the decimal point{  digitalWrite(p, LOW);} void clearLEDs()  //clear contents{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);  digitalWrite(p, HIGH);} void zero()  //define 0 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void one()  // define 1 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void two()  // define 2 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, LOW);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, LOW);  digitalWrite(g, HIGH);} void three()  // define 3 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, HIGH);} void four()  // define 4 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void five()  // define 5 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void six()  // define 6 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void seven()  // define 7 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void eight()  // define 8 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void nine()  // define 9 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);}`

#### PaulRB

#5
##### Nov 27, 2014, 08:16 am
Hi, you have changed the cathodes from HIGH to LOW & LOW to HIGH, but not the anodes.

Paul

#### in2tech

#6
##### Nov 27, 2014, 05:01 pm
Which ones are the anodes? Guess I just have to start over again and change all HIGH's and LOW's around. Hopefully I'll figure this out at some point! Is this the anode part I need to change?

Code: [Select]
` n++;   if (digitalRead(13) == HIGH)  {    n = 0;  }} void pickDigit(int x)  //define pickDigit(x),to open dx port{  digitalWrite(d1, LOW);  digitalWrite(d2, LOW);  digitalWrite(d3, LOW);  digitalWrite(d4, LOW);   switch(x)  {  case 1:     digitalWrite(d1, HIGH);     break;  case 2:     digitalWrite(d2, HIGH);     break;  case 3:     digitalWrite(d3, HIGH);     break;  default:     digitalWrite(d4, HIGH);     break;  }}`

#### in2tech

#7
##### Nov 27, 2014, 05:27 pmLast Edit: Nov 27, 2014, 06:08 pm by in2tech
Success....got it working and it is counting to 999.9 I guess.

Thanks everyone! Here is the working code for that particular 4 digit 7 segment LED if anyone else needs it.

4 Digit 7 Segment Working YouTube!

Code: [Select]
`//set the cathode interfaceint a = 1;int b = 2;int c = 3;int d = 4;int e = 5;int f = 6;int g = 7;int p = 8;// set the anode interfaceint d4 = 9;int d3 = 10;int d2 = 11;int d1 = 12;//set variablelong n = 0;int x = 100;int del = 55;  //fine-tuning value for clockvoid setup(){  pinMode(d1, OUTPUT);  pinMode(d2, OUTPUT);  pinMode(d3, OUTPUT);  pinMode(d4, OUTPUT);  pinMode(a, OUTPUT);  pinMode(b, OUTPUT);  pinMode(c, OUTPUT);  pinMode(d, OUTPUT);  pinMode(e, OUTPUT);  pinMode(f, OUTPUT);  pinMode(g, OUTPUT);  pinMode(p, OUTPUT);} void loop(){  clearLEDs();  pickDigit(1);  pickNumber((n/x/1000)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(2);  pickNumber((n/x/100)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(3);  dispDec(3);  pickNumber((n/x/10)%10);  delayMicroseconds(del);   clearLEDs();  pickDigit(4);  pickNumber(n/x%10);  delayMicroseconds(del);   n++;   if (digitalRead(13) == LOW)  {    n = 0;  }} void pickDigit(int x)  //define pickDigit(x),to open dx port{  digitalWrite(d1, HIGH);  digitalWrite(d2, HIGH);  digitalWrite(d3, HIGH);  digitalWrite(d4, HIGH);   switch(x)  {  case 1:     digitalWrite(d1, LOW);     break;  case 2:     digitalWrite(d2, LOW);     break;  case 3:     digitalWrite(d3, LOW);     break;  default:     digitalWrite(d4, LOW);     break;  }} void pickNumber(int x)   //define pickNumber(x)to display number x{  switch(x)  {  default:     zero();     break;  case 1:     one();     break;  case 2:     two();     break;  case 3:     three();     break;  case 4:     four();     break;  case 5:     five();     break;  case 6:     six();     break;  case 7:     seven();     break;  case 8:     eight();     break;  case 9:     nine();     break;  }} void dispDec(int x)  //set to start the decimal point{  digitalWrite(p, HIGH);} void clearLEDs()  //clear contents{  digitalWrite(a, LOW);  digitalWrite(b, LOW);  digitalWrite(c, LOW);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);  digitalWrite(p, LOW);} void zero()  //define 0 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, LOW);} void one()  // define 1 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void two()  // define 2 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, LOW);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, LOW);  digitalWrite(g, HIGH);} void three()  // define 3 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, HIGH);} void four()  // define 4 as cathode pin switch{  digitalWrite(a, LOW);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void five()  // define 5 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void six()  // define 6 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, LOW);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void seven()  // define 7 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, LOW);  digitalWrite(e, LOW);  digitalWrite(f, LOW);  digitalWrite(g, LOW);} void eight()  // define 8 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, HIGH);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);} void nine()  // define 9 as cathode pin switch{  digitalWrite(a, HIGH);  digitalWrite(b, HIGH);  digitalWrite(c, HIGH);  digitalWrite(d, HIGH);  digitalWrite(e, LOW);  digitalWrite(f, HIGH);  digitalWrite(g, HIGH);}`

#### PaulRB

#8
##### Nov 27, 2014, 09:07 pm
Well done! Your first success in the world of Arduino.

Now the bad news, and you could not be expected to know...

I strongly suspect that circuit could shorten the Arduino's life. The sketch isn't very well written either.

I'll explain if you're interested. What value are those resistors?

#### Nick_Pyner

#9
##### Nov 28, 2014, 01:32 am
And, in the light of that, and since you have a common cathode display, you might try using the MAX 7221 multiplexer.

http://playground.arduino.cc/Main/MAX72XXHardware#.UxPXAc76U1I
http://forum.arduino.cc//index.php?topic=172192.msg1279382#msg1279382

#### in2tech

#10
##### Nov 28, 2014, 03:45 am
Well done! Your first success in the world of Arduino.

Now the bad news, and you could not be expected to know...

I strongly suspect that circuit could shorten the Arduino's life. The sketch isn't very well written either.

I'll explain if you're interested. What value are those resistors?
Sure I am interested to know why it would shorten the life of the Arduino. I am new and need any and all info. Chances are it won't be hooked up very long at all. Just wanted to see the 4 digit 7 segment work actually. Resistors are 220 ohm, just like the example and circuit in the first post. Followed it exactly as I can only follow a Fritzing  diagram at this point because I am so new to this

#### Mcz15

#11
##### Oct 05, 2015, 04:45 am
hi by any chance would it be possible change the code so it will countdown (like a timer)

thx

#### PaulRB

#12
##### Oct 05, 2015, 07:31 amLast Edit: Oct 05, 2015, 07:34 am by PaulRB
hi by any chance would it be possible change the code so it will countdown (like a timer)
Hi, this thread is nearly a year old, you should start a new thread if you have a question. Its ok to give a link to an old thread that is relavent.

Yes, it would be possible to change the code to count down. But as i said in an earlier post, this not a good quality piece of code. I would look for a better example to use as a starting point. Start that new thread if you need help with it.

Paul

Go Up

Please enter a valid email to subscribe