# Arduino with 7-segment display and potentiometer and display is flickering

**URL:** <https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834>\
**Category:** LEDs and Multiplexing\
**Created:** [November 2, 2024, 3:23pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834 "2024-11-02T15:23:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 2, 2024, 3:23pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/1 "2024-11-02T15:23:08Z")

</div>

so i have the following Arduino code that takes the input of 2 buttons A and B and uses a shift register to make shift the data into its output LEDs binary number but I made it in such a way that the output is divided into 2 4-bits to count both A and B which is working very fine on its own and then the count is also displayed on a 7 segment display in hexadecimal which is also working very fine but i wanted to add a method to multiplex through the 7 segments and also added a potentiometer to change the delay of the multiplexing and display how it works but when i added the potentiometer into this, the LEDs start flickering sometimes and also when i use button B, the count keeps switching between displaying count A or B each click instead of just staying at B... also this switching happens regardless if there's a potentiometer or not... so here's the code:

```arduino
int dataPin = 6;
int latchPin = 7;
int clockPin = 8;
int resetButtonPin = 11;
int clearPin = 2;
int extraButton = 12;

int buttonPinA = 9; 
int buttonPinB = 10; 

byte LEDsAllOff = 0x00;
byte LEDsAllOn = 0xFF;
byte LEDs2 = 0b10101010;
byte LEDs3 = 0b01010101;
byte LEDs4 = 0b11101111;

int buttonAState = LOW;
int lastButtonAState = LOW;

int buttonBState = LOW;
int lastButtonBState = LOW;

int resetButtonState = 0;
int lastResetButtonState = 0;

int countA = 0;
int countB = 0;
int maxCount = 15;
bool displayCountA = true; // Flag to alternate between countA and countB
int delayTime = 1000;

// 7-segment display pins
int segmentPins[] = {5, A1, A2, A3, A4, A5, 3}; // A1 to A5 for a to g, 3 for f, 5 for a
int decimalPointPin = 4; // Pin for the decimal point
int poten = A0;
int interval = 1;

// Segment patterns for digits 0-F
byte digitPatterns[] = {
  0b00000000, // 0
  0b00000110, // 1
  0b0111011, // 2
  0b00101111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00000111, // 7
  0b01111111, // 8
  0b01101111, // 9
  0b01110111, // A
  0b01111100, // b
  0b01011001, // C
  0b00111110, // d
  0b01111001, // E
  0b01110001 // F
};

void setup() {
  Serial.begin(9600);
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(resetButtonPin, INPUT_PULLUP);

  pinMode(buttonPinA, INPUT_PULLUP);
  pinMode(buttonPinB, INPUT_PULLUP);

  pinMode(clearPin, OUTPUT);
  digitalWrite(clearPin, HIGH); // Ensure SRCLR is high to enable shift register
  pinMode(decimalPointPin, OUTPUT); // Initialize pin for decimal point

  // Initialize 7-segment display pins
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
}

void updateShiftRegister(int valueA, int valueB) {
  byte combinedValue = (valueA << 4) | (valueB & 0x0F);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, combinedValue);
  digitalWrite(latchPin, HIGH);
}

void clearShiftRegister() {
  digitalWrite(clearPin, LOW);  
  delay(1);                     
  digitalWrite(clearPin, HIGH); 
  updateShiftRegister(0, 0);  
}

void displayDigit(int digit, bool decimalPoint) {
  byte pattern = digitPatterns[digit];
  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[i], (pattern >> i) & 0x01); // Turn on the current segment
    delay(interval); // Small delay to allow the segment to be visible
    digitalWrite(segmentPins[i], LOW); // Turn off all segments 
  }
}

void loop() {
  // int potValue = analogRead(poten);
  // // Map the potentiometer value to a suitable delay range (e.g., 1 to 100 milliseconds)
  // interval = map(potValue, 0, 1023, 1, 100);

    if (displayCountA) {
      displayDigit(countA, false);
    } else {
      displayDigit(countB, true);
    }

  buttonAState = digitalRead(buttonPinA);
  buttonBState = digitalRead(buttonPinB);
  resetButtonState = digitalRead(resetButtonPin);

  // Check if button A is pressed
  if (buttonAState == HIGH && lastButtonAState == LOW && countA + countB < maxCount) {

    displayCountA = displayCountA;
    countA++;
    updateShiftRegister(countA, countB);
    delay(200); // Debounce delay
  
  }
  lastButtonAState = buttonAState;

  //check if button B is pressed
  if (buttonBState == HIGH && lastButtonBState == LOW && countA + countB < maxCount) {

    displayCountA = !displayCountA;
    countB++;
    updateShiftRegister(countA, countB);
    delay(200);
    }
  lastButtonBState = buttonBState;

  // Check if the reset button is pressed
  if (resetButtonState == HIGH && lastResetButtonState == LOW) {
    countA = 0;
    countB = 0;
    clearShiftRegister();
    displayDigit(0 ,false); // Display 0 on reset
    delay(delayTime);
  }
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 2, 2024, 4:21pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/2 "2024-11-02T16:21:05Z")

</div>

what's this for ??

> [@abdelrahman666](#):
>
> ```arduino
> displayCountA = displayCountA;
> 
> ```

should you use

```arduino
    displayCountA = true;

```

and

```arduino
    displayCountA = false;

```

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [November 2, 2024, 4:22pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/3 "2024-11-02T16:22:35Z")

</div>

[Using a potentiometer with 7-segment display causes flickering - Using Arduino / LEDs and Multiplexing - Arduino Forum](https://forum.arduino.cc/t/using-a-potentiometer-with-7-segment-display-causes-flickering/1317842)

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 2, 2024, 4:24pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/4 "2024-11-02T16:24:21Z")

</div>

@anon85221860 flag one of the topics as cross-posting.

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [November 2, 2024, 4:24pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/5 "2024-11-02T16:24:48Z")

</div>

Thanks.  
They are too busy trolling.

---

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 2, 2024, 3:51pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/6 "2024-11-02T15:51:32Z")

</div>

so i have the following Arduino code that takes the input of 2 buttons A and B and uses a shift register to make shift the data into its output LEDs binary number but I made it in such a way that the output is divided into 2 4-bits to count both A and B which is working very fine on its own and then the count is also displayed on a 7 segment display in hexadecimal which is also working very fine but i wanted to add a method to multiplex through the 7 segments and also added a potentiometer to change the delay of the multiplexing and display how it works but when i added the potentiometer into this, the LEDs start flickering sometimes and also when i use button B, the count keeps switching between displaying count A or B each click instead of just staying at B... also this switching happens regardless if there's a potentiometer or not... so here's the code:

```arduino
int dataPin = 6;
int latchPin = 7;
int clockPin = 8;
int resetButtonPin = 11;
int clearPin = 2;
int extraButton = 12;

int buttonPinA = 9; 
int buttonPinB = 10; 

byte LEDsAllOff = 0x00;
byte LEDsAllOn = 0xFF;
byte LEDs2 = 0b10101010;
byte LEDs3 = 0b01010101;
byte LEDs4 = 0b11101111;

int buttonAState = LOW;
int lastButtonAState = LOW;

int buttonBState = LOW;
int lastButtonBState = LOW;

int resetButtonState = 0;
int lastResetButtonState = 0;

int countA = 0;
int countB = 0;
int maxCount = 15;
bool displayCountA = true; // Flag to alternate between countA and countB
int delayTime = 1000;

// 7-segment display pins
int segmentPins[] = {5, A1, A2, A3, A4, A5, 3}; // A1 to A5 for a to g, 3 for f, 5 for a
int decimalPointPin = 4; // Pin for the decimal point
int poten = A0;
int interval = 1;

// Segment patterns for digits 0-F
byte digitPatterns[] = {
  0b00000000, // 0
  0b00000110, // 1
  0b0111011, // 2
  0b00101111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00000111, // 7
  0b01111111, // 8
  0b01101111, // 9
  0b01110111, // A
  0b01111100, // b
  0b01011001, // C
  0b00111110, // d
  0b01111001, // E
  0b01110001 // F
};

void setup() {
  Serial.begin(9600);
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(resetButtonPin, INPUT_PULLUP);

  pinMode(buttonPinA, INPUT_PULLUP);
  pinMode(buttonPinB, INPUT_PULLUP);

  pinMode(clearPin, OUTPUT);
  digitalWrite(clearPin, HIGH); // Ensure SRCLR is high to enable shift register
  pinMode(decimalPointPin, OUTPUT); // Initialize pin for decimal point

  // Initialize 7-segment display pins
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
}

void updateShiftRegister(int valueA, int valueB) {
  byte combinedValue = (valueA << 4) | (valueB & 0x0F);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, combinedValue);
  digitalWrite(latchPin, HIGH);
}

void clearShiftRegister() {
  digitalWrite(clearPin, LOW);  
  delay(1);                     
  digitalWrite(clearPin, HIGH); 
  updateShiftRegister(0, 0);  
}

void displayDigit(int digit, bool decimalPoint) {
  byte pattern = digitPatterns[digit];
  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[i], (pattern >> i) & 0x01); // Turn on the current segment
    delay(interval); // Small delay to allow the segment to be visible
    digitalWrite(segmentPins[i], LOW); // Turn off all segments 
  }
}

void loop() {
  // int potValue = analogRead(poten);
  // // Map the potentiometer value to a suitable delay range (e.g., 1 to 100 milliseconds)
  // interval = map(potValue, 0, 1023, 1, 100);

    if (displayCountA) {
      displayDigit(countA, false);
    } else {
      displayDigit(countB, true);
    }

  buttonAState = digitalRead(buttonPinA);
  buttonBState = digitalRead(buttonPinB);
  resetButtonState = digitalRead(resetButtonPin);

  // Check if button A is pressed
  if (buttonAState == HIGH && lastButtonAState == LOW && countA + countB < maxCount) {

    displayCountA = displayCountA;
    countA++;
    updateShiftRegister(countA, countB);
    delay(200); // Debounce delay
  
  }
  lastButtonAState = buttonAState;

  //check if button B is pressed
  if (buttonBState == HIGH && lastButtonBState == LOW && countA + countB < maxCount) {

    displayCountA = !displayCountA;
    countB++;
    updateShiftRegister(countA, countB);
    delay(200);
    }
  lastButtonBState = buttonBState;

  // Check if the reset button is pressed
  if (resetButtonState == HIGH && lastResetButtonState == LOW) {
    countA = 0;
    countB = 0;
    clearShiftRegister();
    displayDigit(0 ,false); // Display 0 on reset
    delay(delayTime);
  }
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 2, 2024, 4:18pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/7 "2024-11-02T16:18:41Z")

</div>

- **Always** show us a good schematic of your proposed circuit.  
Show us good images of **your** ‘actual’ wiring.  
Give links to components.

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [November 2, 2024, 4:22pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/8 "2024-11-02T16:22:07Z")

</div>

[Arduino with 7-segment display and potentiometer and display is flickering - Using Arduino / LEDs and Multiplexing - Arduino Forum](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 2, 2024, 4:36pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/9 "2024-11-02T16:36:38Z")

</div>

> [@PaulRB](#):
>
> @runaway\_pancake flag one of the topics as cross-posting.

I merged both threads

* * *

@abdelrahman666 Your two topics on the same or similar subject have been merged.

Cross-posting is against the Arduino forum [rules](https://forum.arduino.cc/faq#keep-tidy). The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to [Learn How To Use The Forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). It will help you get the best out of the forum in the future.

Thank you.

---

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 2, 2024, 4:57pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/10 "2024-11-02T16:57:31Z")

</div>

apologies for not following the rules, i will make sure to not do this next time... should i delete the two posts and post a new one with everything correct and schematics + components used? or edit one of them? or delete one and edit other?

---

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 2, 2024, 4:59pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/11 "2024-11-02T16:59:35Z")

</div>

it wasn't there in the beginning i only added this thinking it might solve the switching problem

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 2, 2024, 5:46pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/12 "2024-11-02T17:46:26Z")

</div>

No, the topics are merged now, post your schematics etc here.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 2, 2024, 6:09pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/13 "2024-11-02T18:09:02Z")

</div>

> [@abdelrahman666](#):
>
> ```arduino
> void clearShiftRegister() {
> digitalWrite(clearPin, LOW);  
> delay(1);                     
> digitalWrite(clearPin, HIGH); 
> updateShiftRegister(0, 0);  
> }
> 
> ```

I don't think this function is necessary. You clear all the outputs with with a pulse to the clear pin, but then you immediately shift out 8 bits which are all zero, which has the same effect. Suggest deleting this function, connecting the clear pin of the shift register to 5V and freeing up that Arduino pin.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 2, 2024, 6:14pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/14 "2024-11-02T18:14:06Z")

</div>

> [@abdelrahman666](#):
>
> ```arduino
> void displayDigit(int digit, bool decimalPoint) {
> byte pattern = digitPatterns[digit];
> for (int i = 0; i < 7; i++) {
> digitalWrite(segmentPins[i], (pattern >> i) & 0x01); // Turn on the current segment
> delay(interval); // Small delay to allow the segment to be visible
> digitalWrite(segmentPins[i], LOW); // Turn off all segments 
> }
> }
> 
> ```

I guess this is the cause of the flickering.

You are only lighting one segment at a time, then switching the segments off again. Why not switch  
on all that you need, and leave them until you need to change them?

---

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 5, 2024, 9:00pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/15 "2024-11-05T21:00:02Z")

</div>

the purpose of this is to implement multiplexing to display the number on the led... before that yes it directly switched on all the led's at once but i made it this way to multiplex but for some reason the multiplexing causes the problems in the post to happen

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 5, 2024, 9:53pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/16 "2024-11-05T21:53:05Z")

</div>

> [@abdelrahman666](#):
>
> the purpose of this is to implement multiplexing

The purpose of multiplexing is to reduce the number of pins/wires required to illuminate all your display segments. You have 7 segments and 7 pins. No multiplexing is necessary and would give no advantage.

---

<div class="post-metadata">

**Author:** ![abdelrahman666](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/abdelrahman666/32/1117395_2.png) [@abdelrahman666](https://forum.arduino.cc/u/abdelrahman666)\
**Post date:** [November 5, 2024, 10:15pm UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/17 "2024-11-05T22:15:56Z")

</div>

I know in this case multiplexing is practically useless, and that multiplexing is usually done in 7 segment displays by having multiples segments wired to the same data lines and only one is allowed to work at a time, but in this project I wanted to showcase how multiplexing works by tricking the eye in lighting one segment at a time on one display. but if this is hard or troublesome to implement I won't pursue this further. thank you...

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 6, 2024, 8:24am UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/18 "2024-11-06T08:24:51Z")

</div>

> [@abdelrahman666](#):
>
> `if (resetButtonState == HIGH && lastResetButtonState == LOW) {`

Where is lastResetButtonState updated?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [November 6, 2024, 8:26am UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/19 "2024-11-06T08:26:42Z")

</div>

> [@abdelrahman666](#):
>
> `displayCountA = displayCountA;`

Did you miss a ! here?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2025, 8:27am UTC](https://forum.arduino.cc/t/arduino-with-7-segment-display-and-potentiometer-and-display-is-flickering/1317834/20 "2025-05-05T08:27:24Z")

</div>

Closed on May 5, 2025, 8:27am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
