Friday, April 6, 2018

RGB-Strip Control over Wifi Part 2

This story is a sequel to earlier stories on this weblog. I advise you to read them first for some basic understanding on how this all came together.

First important story was about controlling a ledstrip. This story showed how to use power transistors to set the colors of a ledstrip.

The second story was based on the first story however in this story the ledstrip was controlled by an Android phone over bluetooth.

In the third important story in this sequel I introduced you ESP-Basic. A full blown Basic language for the ESP-8266 which makes all kinds of projects with Wifi incredibly easy. Read that story here:

The fourth story showed how easy it is to use sliders in ESP-Basic to control the colors of a neo-pixel strip.

And in this installment I am going to combine all previous techniques to control a full-blown ledstrip over Wifi using power transistors attached to an ESP-8266 and programmed in just a few lines of Basic.

So the objective is to attach a full-blown ledstrip to an ESP-8266 and control it over Wifi. Why should you do this instead of the previous version with bluetooth.
In the first place wifi has a bigger range as bluetooth so there is more ease of use. And secondly: you can do it from anywhere in the world. So you can set the colours of the ledstrip in the color of your choosing before you get home. Or you can use it even to notify people that you are on your way home.

Let's start.

The theory.

A ledstrip can produce thousands of colors when it is controlled in the right way. In the Bluetooth variation described in the previous stories I used several predifined colors. However I wanted more freedom.

So the leds had to be controlled by an analog signal. A digital signal just sets the led ON or OFF. An analog signal is able to control the intensity of the light.

Now I am using the ESP-8266 (NodeMCU or Wemos D1 mini version) and the ESP has no analog output capabilities. That is the same on an Arduino. So clever programmers have simulated an analog output by something called PWM (Pulse Width modulation).
Basically it works by setting the I/O pin in a high and then low state but in a very fast pace. By doing so the pin is pulsating. And if you do that fast enough you can dim the LED in steps.
Sounds complicated ??? Well it is. But we can use this by a very simple command.

Let's try it first with a simple led.

Build the breadboard like the picture shows you and program your ESP with the following Basic code:

 timer 500, [set]  
 slider pwmval, 0, 1024  
 wprint "<br/><br/>"  
 button "<h2>Off</h2>", [Off]  
 wprint "<br/>"  
 io(pwo, d5, pwmval)  
 io(po, d5, 0)  

This simple program puts a slider on your screen and an OFF button.
By moving the slider the led will be dimmer or brighter. Just what we need to control all the colors of the ledstrip.

As this works we have the fundament working. We can now expand it to a full RGB strip control.


Look at the breadboard. As you can see, just like in the blue-tooth ledstrip control, we are going to control the ledstrip with TIP120 transistors.

The Tip120 transistors can supply each a maximum current of 5 amperes.
5 Amperes is 5000 Mili Amperes. Each led of the RGB strip will consume 20 Ma.
So in theory each Tip120 can control 5000/20 = 250 leds in the strip. You will need to put cooling fins on the TIP's for this. But my advise is to not drive them to the max.

My ledstrip is 5 meter long and has 150 leds of each color (150 Red, 150 green and 150 Blue). So the Tip120's should supply enough current to control the complete ledstrip.

Just make shure that your power supply has the same specifications. It has to be a 12volt power supply that supplies enough current for the complete ledstrip.
So what I did was to look at the original power supply that was issued with my RGB strip. It was 12 volts at 2 amps. So not enough to supply the full strip !!!

Make sure that the 12Volt power is only attached at the TIP120 side. The NodeMCU needs its own 5 volt power supply.

I attached 3 TIP120 transistors with resistors to d5, d6 and d7 of the NodeMCU.
The schematics are straightforward and will give you enough information to build this yourself

Basic program

The Basic program is just an extension of the small program listed above. I made 3 sliders 1 for each color (R, G and B) and on button to put the ledstrip off.

 timer 500,[set]  
 wprint |<h1 style="text-align:center;">Luc Volders</br>RGB-STRIP</br>CONTROL|  
 wprint "<br/><br/>"  
 slider r, 0, 1023  
 cssid htmlid(),"background-color: red"  
 textbox r  
 wprint "<br/><br/>"  
 slider g, 0, 1023  
 cssid htmlid(),"background-color: green"  
 textbox g  
 wprint "<br/><br/>"  
 slider b, 0, 1023  
 cssid htmlid(),"background-color: blue"  
 textbox b  
 wprint "<br/><br/>"  
 button "<h2>Off</h2>", [Off]  
 wprint "<br/>"  
 r = 0  
 g = 0  
 b = 0  

Actually the Basic program is an alteration of the Basic program I introduced in the previous story in this line-up where we controlled a strip of Neopixels. Read that story by clicking here.

The alterations are that the RGB strip will react instantly at any alteration of the sliders. And the Neopixel command has been aleterd in these commands:


These commands will set the I/O pin in a PWM state defined by the r, g or b variable.

So there it is:  a complete Wifi controlled RGB strip in just 38 lines of code !!!

The pictures above show you how it functions in real life.
As you can see there are textboxes below the sliders. In these text-boxes you can fill in any figure you like (between 0 and 1024) for test purposes or for fine-tuning the slider setting.

The ESP8266 is really a marvel and ESP-Basic is fantastic !!

Till next time
Have fun

Luc Volders