# Driving RGB Leds from ESP8266

I want to create a schematic for the following behaviour:

I want an ESP-12e (ESP8266) microcontroller similar to an arduino, to be able to control 10 RGB leds , changing their colours from blue to green to red and all shades available in between those colours.

I have designed the following schematic to achieve this behaviour having researched options:

The question I have as per the schematic note is - which 9V regulator would I use , given I need around 600mA from it I am finding it hard to find one within the schematic libraries and additionally, finding an actual regulator to buy appears difficult.

I have a question about the design and that is as follows: As I understand it, I can control the amount of brightness of Red, Green and Blue from my LED by controlling the amount of voltage sent to those pins, however, the 2N2222 is on, or off - with no space in between, so does that mean in this case as my GPIO pins fluctuate to control shades of colours, the LEDS in this case will be Red ON or OFF, Green ON or OFF and Blue ON or OFF - or have I misunderstood how this bit works.

Many thanks!

• This schematic is poorly drawn and lots of things are wrong. What makes you not want to look for a regulator by yourself? You have the constraints, now use the web to narrow down your choices. The second part is not clear... no you're not controlling LEDs with voltage but current sank into the transistors. Usually PWMing is the way to go to control the brightness of each color, does the ESP has PWM capabilities on those GPIO pins? – eeintech Mar 10 '20 at 22:10
• @cisco25 , the ESP can do PWM, as per mattman944's answer below, so in that case, will my circuit do the job? As for suggesting the schematic is poorly drawn and "lots of things are wrong" - it would be more helpful to answer properly with a set of advice rather than opinion oriented statements. – RenegadeAndy Mar 10 '20 at 22:27
• How to design clean schematics: electronics.stackexchange.com/questions/28251/… -- I'm sure you can find more guidelines online, start by orienting your parts the proper way, adding a proper ground reference and spacing/connecting wires so it is legible to the reviewer. Where are you decoupling caps? Read about low-dropout regulator design guidelines. Transistor's answer for PWMing will get you started then. – eeintech Mar 10 '20 at 23:33

The question I have as per the schematic note is - which 9V regulator would I use , given I need around 600mA from it ...

You don't really need a regulator! LEDs are very sensitive to voltage but with the current limiting resistors you have correctly applied to each LED the variation in current and brightness will be quite acceptable for a reasonable variation in voltage. You can use an unregulated power supply (transformer, rectifier and smooting capacitors).

... I can control the amount of brightness of Red, Green and Blue from my LED by controlling the amount of voltage sent to those pins, however, the 2N2222 is on, or off - with no space in between, ...

Yes. You are describing pulse-width modulation. With your transistor half-way on (to vary the voltage) it will get rather warm as the power dissipated in it will be the product of the voltage across it (the transistor, not the LED) and the current through it: $$\ P = V_t I \$$. We generate much less heat if we switch the transistor fully on (V is very low) or fully off (I is zero). We just need to do this quickly enough to fool the eye that the light is dimming.

Figure 1. PWM signal transitioning from high pulse width (75%) to low (25%) and back again. Note amplitude remains constant. This will result in a control input of 7.5 V and 2.5 V respectively giving 75% and 25% of rated current. Source: LEDnique.com.

Figure 2. Figure 2. By cross-fading the colours an RGB LED can generate a wide color spectrum by mixing light. Source: RGB LED.

Each colour can be generated by varying the relative pulse-widths of the RGB LEDs. All on will give white(ish).

• Excellent answer thank you. Does that mean, as my schema is drawn, everything will work as I expect, so long as I use PWM in the software side of the esp8266 firmware? You say I I don't need a voltage regulator, but I still need to supply 9V and if my power source doesn't supply 9V i'll need a regulator to drop whatever my supply is down to that level, correct? – RenegadeAndy Mar 10 '20 at 22:25
• It actually appears therefore that my original post's understanding is correct - the 2n2222 means each of the colours is on or off, nothing in between, but the PWM in the software is what controls how frequently the individual colours are on and off, and therefore what colour our eyes perceive! – RenegadeAndy Mar 10 '20 at 22:34
• (1) Your schematic looks OK but I haven't studied it in detail. (2) Yes PWM in the software. (3) Yes, you need around 9 V but 12 V would work too but you might want to recalculate the LED resistance values. (4) Yes, the 2N2222s are either fully on or fully off. (5) PWM controls the "duty cycle" (% on-time) rather than the frequency which will be fixed. Your eyes have three cones for short, medium and long wavelengths corresponding roughly to blue, green and red. By blending the power of your R, G and B channels you stimulate the cones proportionally giving the perception of colour. – Transistor Mar 10 '20 at 23:48

To control your LED brightness, you should pulse-width (PWM) modulate your LEDs at a rate faster than the human eye can see. Modern MCUs often have built-in PWM circuits. If not, they will always have timers, and you can use timers and interrupts to do it with minimal software.

https://randomnerdtutorials.com/esp8266-pwm-arduino-ide/

Many different sizes of power adapters are available from amazon, add some margin, don't run near their limit. I would use 1A minimum.

• Great - so I use PWM on my esp8266 to control how often the transistors are switched on and that will allow me to make all the colours of the rainbow on the circuit I posted, right? – RenegadeAndy Mar 10 '20 at 22:30
• Yes, PWM on RGB LEDs can produce all colors. A quick search leads me to believe that any GPIO pin can be used for PWM on the ESP8266 (not the case for all MCUs). – Mattman944 Mar 10 '20 at 23:45
• If you are willing to start over, WS2812 LEDs as referenced by hacktastical are another option. They have built-in PWM circuits, you access them via a 800 kHz serial bit interface. – Mattman944 Mar 10 '20 at 23:48

That’s the hard way, and it will have flicker artifacts if the PWM rate is too low.

The easy way is to use WS2812 -interfaced RGB LEDs which come singly or in strips. WS2812 protocol allows daisy-chain connection of multiple LEDs.