Changing Brightness of 2 digit 7 segment LED

Hi all,

I have a common anode 2 digit 7 segment LED set up like this with PNP transistors.

Is it necessary to have the resistors? How would I change the brightness of the LEDs? Is there some sort of code that I can insert into Arduino?

Hi, yes, the resistors are needed. It may seem to work ok without them, but doing so could be shortening the life of the arduino and/or the displays.

You can control the brightness by using the analogWrite function to control the common anodes. Note that analogWrite can only be used on certain outputs (see the reference page), so you may need to juggle the pins around a bit.

Paul

Hey Paul,

Thanks for the response. I've tried that method by hooking up the transistors to the pins that support analogWrite but it doesn't seem to work? Maybe I'm writing it wrong, I honestly have no clue how I would code it and I don't have a potentiometer handy. Do I hook up the individual led segments to the specified pins? If so there aren't enough that support changing the PWM. Here's part of the code that I've written to control the leds.

void lightDigit1(byte number) {
    digitalWrite(CA1, LOW);
    digitalWrite(CA2, HIGH);
    analogWrite(11,0) \\does this go here? the PNP transistors are connected to pins 10 and 11
    analogWrite(10,0) \\does this go here? 
    lightSegments(number);
}

void lightDigit2(byte number) {
    digitalWrite(CA1, HIGH);
    digitalWrite(CA2, LOW);
    lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

Hi Jonny, you just need 2 pwm outputs to control the 2 common anodes.

Your code is confusing because you have not posted the latest version in full. You should be changing the symbols CA1 and CA2 to be ouputs 10 and 11 if that is what you are using. If you have changed the ouputs driving the segments, change those symbols also.

Because the displays are common anode, pnp transistors are used. You switch on a pnp transistor with a low output from the arduino to its base (as opposed to a high output with npn). Its the digitalWrite (CA1, LOW) command that switches digit 1 on. Replace this command with analogWrite (CA1, x) where x is the brightness level. 0 will give maximum brightness and 255 miminum brightness (actually no brightness at all!). You can still use digitalWrite (CA1, HIGH) to switch that digit off again.

I apologize, here's everything that I have. I added the analogWrite to where you suggested but as the number goes up, the display starts flickering really badly. Is there a solution for that?

#define A A0
#define B A1
#define C A2
#define D A3
#define E A4
#define pinF A5
#define G 3
#define CA1 11
#define CA2 10

int counter=10;
int digit1=0;
int digit2=0;
const int segs[7]= {A,B,C,D,E,pinF,G};
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {  
  digit1=(counter/10)%10;
  digit2=counter%10;

  if(digitalRead(BTN1) == HIGH) {
    if (start < 99) {
      counter++;
      delay(200);
    }
  }
  if(digitalRead(BTN2) == HIGH) {
    if (start > 1) {
      counter--;
      delay(200);
    }
  }
  for(int x=0;x<50;x++) {
    lightDigit1(numbers[digit1]);
  }
  for(int y=0;y<50;y++) {
    lightDigit2(numbers[digit2]);
  }

void lightDigit1(byte number) {
    analogWrite(CA1, 250);
    digitalWrite(CA2, HIGH);
    lightSegments(number);
}

void lightDigit2(byte number) {
    digitalWrite(CA1, HIGH);
    analogWrite(CA2, 250);
    lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

You have a complex issue, having two (different) common anodes sharing one set of cathodes.

How would I fix that? Do I hook everything up to a single PNP transistor?

A) When you have the left anode (the left digit) enabled then you apply the left digit data to the cathodes.
B) When you have the right anode (the right digit) enabled then you apply the right digit data to the cathodes.

By rapidly toggling the A & B operations with the left and the right enabled for equal periods, the two digits appear to be on together and have equal brightness.
For one to be brighter/dimmer compared to the other will require having one enabled longer/shorter than the other.

Here is an experiment to illustrate my point.
It uses D2 and D3 as outputs, connect an LED with a series resistor to each.

pin ---A_K---220?---Gnd

Here's the sketch:

const byte brt = 9;
const byte med = 3;
const byte dim = 1;

const byte left = 2;
const byte right = 3;

void setup ()
{
  pinMode(left,OUTPUT);
  digitalWrite(left,LOW);
  pinMode(right,OUTPUT);
  digitalWrite(right,LOW);
}

void loop ()
{
  for(byte cycle = 0; cycle < 200; cycle++)
  {
    digitalWrite(left,HIGH);
    delay(brt);
    digitalWrite(left,LOW);
    delay(10-brt);
    digitalWrite(right,HIGH);
    delay(brt);
    digitalWrite(right,LOW);
    delay(10-brt);    
  }
  
  for(byte cycle = 0; cycle < 200; cycle++)
  {
    digitalWrite(left,HIGH);
    delay(med);
    digitalWrite(left,LOW);
    delay(10-med);
    digitalWrite(right,HIGH);
    delay(med);
    digitalWrite(right,LOW);
    delay(10-med);
  }  
  
  for(byte cycle = 0; cycle < 200; cycle++)
  {  
    digitalWrite(left,HIGH);
    delay(dim);
    digitalWrite(left,LOW);
    delay(10-dim);
    digitalWrite(right,HIGH);
    delay(brt);
    digitalWrite(right,LOW);
    delay(10-brt);  
  }    
  
  for(byte cycle = 0; cycle < 200; cycle++)
  {
    digitalWrite(left,HIGH);
    delay(brt);
    digitalWrite(left,LOW);
    delay(10-brt);
    digitalWrite(right,HIGH);
    delay(brt);
    digitalWrite(right,LOW);
    delay(10-brt);    
  }
  
  for(byte cycle = 0; cycle < 200; cycle++)
  {
    digitalWrite(left,HIGH);
    delay(brt);
    digitalWrite(left,LOW);
    delay(10-brt);
    digitalWrite(right,HIGH);
    delay(med);
    digitalWrite(right,LOW);
    delay(10-med);
  }  
  
  for(byte cycle = 0; cycle < 200; cycle++)
  {  
    digitalWrite(left,HIGH);
    delay(brt);
    digitalWrite(left,LOW);
    delay(10-brt);
    digitalWrite(right,HIGH);
    delay(dim);
    digitalWrite(right,LOW);
    delay(10-dim);  
  } 
}

It should be written in non-blocking code ("blink without delay"), but it gets the point across.

Ah, I got it!

Thanks so much for all of the help!

johnnyshepard:
Ah, I got it!

I don't... what was the problem and is it now fixed?