r/ArduinoHelp • u/salty_boi_1 • Oct 07 '25
Why is my 7 segment display flickering like this?
Hello everyone hope you're having a great day.today i was trying to work on a project using this example code but every time i use my display flickers like this what's the problem?
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
//Pin connected to DS of 74HC595
int dataPin = 11;
// Digit 1 active pin
int digit1 = 2;
// Digit 2 active pin
int digit2 = 3;
// Digit 3 active pin
int digit3 = 4;
// Digit 4 active pin
int digit4 = 5;
unsigned long previousChange = 0;
String text = "----";
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);
pinMode(digit3, OUTPUT);
pinMode(digit4, OUTPUT);
digitalWrite(digit1, !LOW);
digitalWrite(digit2, !LOW);
digitalWrite(digit3, !LOW);
digitalWrite(digit4, !LOW);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousChange > 1000) {
previousChange = currentMillis;
text = String(random(-999,999));
}
display4(text);
}
void display4(String str) {
byte data;
while (str.length() < 4) {
str = ' ' + str;
}
for (int i = 0; i < 4; i++) {
switch (str[i]) {
case '0':
data = B00111111;
break;
case '1':
data = B00000110;
break;
case '2':
data = B01011011;
break;
case '3':
data = B01001111;
break;
case '4':
data = B01100110;
break;
case '5':
data = B01101101;
break;
case '6':
data = B01111101;
break;
case '7':
data = B00000111;
break;
case '8':
data = B01111111;
break;
case '9':
data = B01101111;
break;
case '-':
data = B01000000;
break;
default:
data = B00000000;
break;
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);
switch (i) {
case 0:
renderDigit(digit1);
break;
case 1:
renderDigit(digit2);
break;
case 2:
renderDigit(digit3);
break;
case 3:
renderDigit(digit4);
break;
}
delay(5);
}
}
void renderDigit(int digit) {
digitalWrite(digit1, HIGH);
digitalWrite(digit2, HIGH);
digitalWrite(digit3, HIGH);
digitalWrite(digit4, HIGH);
delayMicroseconds(100);
digitalWrite(digit1, digit1 == digit ? LOW : HIGH);
digitalWrite(digit2, digit2 == digit ? LOW : HIGH);
digitalWrite(digit3, digit3 == digit ? LOW : HIGH);
digitalWrite(digit4, digit4 == digit ? LOW : HIGH);
delayMicroseconds(100);
}
12
Upvotes
3
u/NoAttention2620 Oct 07 '25
The way it's flickering it seems to me to be a power issue(?) maybe. The code looks fine. Are you powering a lot of things with your Arduinos 5v??
1
u/salty_boi_1 Oct 07 '25
I found it and i hate how annoying it was all i had to do was connect the 10th pin in the 74hc595 to 5 volt
1
u/herocoding Oct 07 '25
I'm used to use the binary-inverse of the databits for the seven-segments (common anode, common cathode?).
What is the block with those `digitalWrite(digit1, digit1 == digit ? LOW : HIGH);` meant to do?
Can you comment-out this block for a quick test?
1
u/nihilianth Oct 07 '25
To troubleshoot this, I suggest to increase all timers and delays by a factor of 10-100 so you can visually track what's happening. With the process slowed down you could also add some Serial.println to check what it's doing. Also for testing you could try to display a fixed number first (without changing it each second). Are you using something to limit the current (e.g. resistors for each segment)? It's possible that the arduino is resetting when you try to draw too much current.
One more obvious thing that I noticed is that you shift out the data while a segment is active. You probably want to do that inside the renderDigit instead (after turning off other segments)
1
u/j_wizlo Oct 07 '25 edited Oct 07 '25
Add more to those delays so you can see it in action. It might too fast for your hardware as well.
If I’m reading this correctly the new data is shifted in while the previous digit is still lit. I don’t think that’s where the issue lies but I wager that won’t give as clean a look than having all digits off when new data is shifted in.
1
u/Worldly-Device-8414 Oct 10 '25
Where are the current limiting resistors between the LED displays & the driver chip? You're probably pulling so much current when segments do light that you're pulling the supply down & resetting the CPU
1
u/Frogxdd Oct 07 '25
Look at the protoboard and see if there is a resistor or wire with bad contact (it has occurred to me)
1
u/W0CBF Oct 07 '25
I believe that the problem is in your contacts
1
u/who_you_are Oct 07 '25
Delay aren't your friend in such application
1
u/PurrfectMistake Oct 07 '25
Power or data issue. Likely power.
1
u/gm310509 Oct 08 '25
You need to ditch all of the delays.
Ideally you would write the image using a single low level IO to a port output register, when you do, you need to activate that digit immediately. You can do it via the shift register if you output the digit to the storage register then latch that data to the output register and immediately select the digit.
Don't write all the other digits to be disabled, then delay for an eternity before rewriting them all a second time to select the active one and delay for another eternity.
Then, independently of your random number generation loop update the strobe of the display to render the next digit.
If you are interested in a rock solid method (plus an optional variant that is pretty good but not rock solid), have a look at my Event Countdown Clock (Covid Clock - V2.0). Specifically the strobing algorithm in the clockDisplay code.
1
u/Difficult-Trade-5116 Oct 11 '25
looks like low voltage issue