BRIGHTNESS OF BI-COLOUR LEDS

Bi-colour LED

Two wire bi-colour LEDs are a useful means of being able to show different colours according to the status of a system, device, etc. I use them extensively in my ESP8266 sensors that have a single red/green LED that shows flashing green when it's connecting to WiFi, steady green when it's connected successfully, steady red if it cannot connect to WiFi and flashing red if it cannot find the I2C sensor.

However, if you've used a red/green bi-colour LED then you may have noticed that one colour is brighter than the other. To get both colours to be of about the same perceived brightness there are two solutions that I use.

Hardware Solution

As a hardware solution you can use an additional resistor, and a diode in parallel with it, to reduce the current for for the brighter colour. However, because the diode has a voltage drop across it, you'll need to replace the usual current limiting resistor with a lower value, perhaps as low as 82Ω. You'll need to experiment somewhat to find the best combination of resistors for your particular LED, its supply voltage and your perception of similar brightness.

Dual resistor LED circuit

Software Solution

If you don't wish to use additional components then you can use the PWM feature on the output pins to reduce the perceived LED brightness. The actual brightness is not reduced, just the time for which the LED is illuminated. Our persistence of vision approximates the brightness to a lower level. This solution has the benefit that you can experiment using software instead of trying different value resistors. Note that you still require one resistor in series with the LED of about 330Ω to 560Ω.

In your sketch, instead of using digitalWrite(pin,HIGH) use analogWrite(pin,pwm-value) instead. The value pwm-value is an integer between 0 and 1023 and specifies the duty cycle for the pin: 0 (always off) and 1023 (always on). Note that a mid-point value of 511 does not necessarily give half the brightness of a value of 1023 - it's all down to perception and the LED's characteristics.

All available pins on the ESP8266 have PWM capability.

The diagram below shows how the pulse width varies with the duty cycle value.

PWM diagram

The code below is an example of how you can use analogWrite() to obtain the effect you want. You could replace the digitalWrite(pin,LOW) with analogWrite(pin,0)

/*******************************************************************************

* ledRed() *

*******************************************************************************/

void ledRed(int pinOne, int pinTwo)

{

digitalWrite(pinOne,LOW);

analogWrite(pinTwo,64);

}

/*******************************************************************************

* ledGreen() *

*******************************************************************************/

void ledGreen(int pinOne, int pinTwo)

{

analogWrite(pinOne,64);

digitalWrite(pinTwo,LOW);

}