Can I use one potientiometer to adjust the brightness of the 4 digit 7 segment display?

Hi guys, I am making a timer for my project. The project requires that the LEDs on the timer display must have 3 brightness levels. The brightness level will be selected by a potentiometer or switch. I am using a 4 digit seven-segment display as my LEDs. How does one potentiometer adjust the 3 level brightness of the 4 digit 7-segment display? I tried the code below. There is only one pin of the digit showing up.

const int potPin = A0; // Connect the potentiometer to analog input A0
const int segmentPins[] = {7, 6, 5, 11, 10, 8, 9}; // Connect the 7-segment display pins to digital pins 2-8
const int numSegments = 7; // Number of segments in the display
int brightness = 0; // Store the brightness value
int a=7; 
int b=6; 
int c=5; 
int d=11; 
int e=10; 
int f=8; 
int g=9; 
int dp=4; 

void setup() {

  for (int i = 0; i < numSegments; i++) {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], LOW); 
    
  }
}

void loop() {
  int potValue = analogRead(potPin); // Read the value of the potentiometer
  brightness = map(potValue, 0, 1023, 0, 255); // Map the potentiometer value to the brightness range
  for (int i = 0; i < numSegments; i++) {
  analogWrite(segmentPins[0], brightness); // Set the brightness of the first segment
  delay(2); // Wait a short amount of time
  digitalWrite(segmentPins[i], LOW); // Turn off the current segment
  }
}

Could anyone help with this? Is it possible to use one potentiometer to adjust 3 levels of the brightness of the 4 digit 7-segment display?

Thank you so much!

What display? How are you driving it?

Post a link to the display you are using.

Post code that displays numbers on that display, without worrying about the brightness.

There are a few different ways to make numbers show up on 7 segment displays, leading naturally to needing a few different ways to control brightness.

Tell us way too much about the whole thing!

a7

Your analogWrite() goes to segmentPins[0] seven times. Needs to be segmentPins[i]

Also, waiting 2 ms before turning it off isn't long enough for your eyes to even notice.

I guess this is a 12 pin display ( 4 digit pins and 8 segment pins including the decimal point). Presumably you are going to be multiplexing that display at some stage in the development of this project.
Another approach to dimming the display is then to regularly display a blank digit instead of the actual digit.

I think the solution is to turn on each digit for an adjustable amount of time:

const byte potPin = A0;                                // Connect the potentiometer to analog input A0
const byte segmentPins[] = { 7, 6, 5, 11, 10, 8, 9 };  // Connect the 7-segment display pins to digital pins 2-8
const int numSegments = 7;                             // Number of segments in the display
const unsigned long MinBrightness = 50;
const unsigned long MaxBrightness = 3000;


void setup()
{
  for (int i = 0; i < numSegments; i++)
  {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], LOW);
  }
}

void loop()
{
  int potValue = analogRead(potPin);                                                // Read the value of the potentiometer
  unsigned long brightness = map(potValue, 0, 1023, MinBrightness, MaxBrightness);  // Map the potentiometer value to the brightness range
 
  // Turn on all segments
  // In actual use, the segments for the current digit would be turned on
  for (int i = 0; i < numSegments; i++)
  {
    digitalWrite(segmentPins[i], HIGH);        
  }

  // The longer the segments are on, the brighter, but too long and the display flickers
  delayMicroseconds(brightness);  // Wait a short amount of time

  // Turn off all segments before selecting the next digit or you will get ghosting.
  for (int i = 0; i < numSegments; i++)
  {
    digitalWrite(segmentPins[i], LOW);  // Turn off the current segment
  }
}

Multiplexing can be a little difficult for a newbie to understand and multiplexing with brightness control even worse. Besides it consumes a lot of processor time. You would be better off buying an I2C display that does all that for you.

Can you recommend an I2C 4 digit 7-segment display with software brightness control?

You might be interested in this
Arduino Playground - MAX72XXHardware

You may try the following setup (Fig-1); where, the brightness of the display unit should change as the Rex1-Potentiometer is rotated.


Figure-1:

HT16K33 Displays.
Brightness is in 0..15.
Lot of 7 segment/ 4digits are available. Adafruit is just one vendor.

I would use my library

display.setBrightness(analogRead(inputPin)/64);

.

https://werner.rothschopf.net/201909_arduino_ht16k33.htm

I've used the DFRobot DFR0645-G (also comes in RED). It's the cheapest
SparkFun 7-Segment Serial Display but it's much more expensive.
Adafruit has quite a few, all different colors.

How do you do the brightness control?

Multiplexing may be non-trivial but the constraints listed in the first post are consistent with a school type project where the OP does not have the flexibility to use ready made driver chip solutions.

I am using a 4pin 4digit 7-segment display. There is a link direct to Amazon for the one I bought.

Could you please give me some suggestions?
So I need to display the numbers first, and after that, adjust the brightness?

Thank you!

Yes, this is the one I am using, which is a 4-pin 4-dight 7-segment display.
Is there any information you can give to adjust the brightness of it using a potentiometer?
Thank you.

based on the description its a TM1637.

-->You should use a library for a TM1637. You will find several in the IDE library manager.

Which of the eleven TM1637 libraries in Library Manager did you choose? If you have not chosen one yet, pick one that has a setBrightness() function.

Thank you. I will try the one you mentioned.