[go: nahoru, domu]

Wifi-control for a 5050 LED strip with ESP32

The 5050-type LED strips are pretty cheap, and can (sometimes) be controlled by ESP32’s fairly easily. The rough idea is to connect the individual color pins to a NPN transistor and to control them with an ESP32 PWM output pin. 5050 LED strips are always set to the same color, you can’t control them individually. There are different voltages of strips available - we’re using 5V since they seem to be pretty cheap, and they can just be powered by a USB cable. 

Parts needed:

  • A 5050 LED strip for 5V – You probably have this otherwise you wouldn’t be reading this. Or not? Here’s one (when searching, use [5050 led 5v usb] to make sure that it’s 5v / USB-powerable; the 12V strips are more complex to hook up since the ESP32 doesn’t take 12V directly).

  • A random ESP32 dev-board – these go for $4-$20 depending on where you get them. The attributes of the board that you need are: USB port (most have them) and 5V pin (sometimes called “V USB” since that’s where the 5V comes from). You don’t need any special kind, most will just work. Here’s one. (Annoying side-note: most ESP32 boards are 28mm or wider, and don’t fit easily on a breadboard without bending all the legs; the one linked above is a slightly older model

  • which is fine for us - and it’s 25mm wide, and fits well on a breadboard.)
  • 3x NPN transistor that works for 5V & ca 800 mA. Example: BC337. Tons of these exist, you might have 3 already. Get them locally, or get a kit of lots of them.

  • 3x 1k resistor. Cheap! Free sometimes! The color code is “brown black red (gold / silver or something else)” – this stands for “1 (brown) 0 (black) x 100 Ohm (red)”. The gold / silver is for accuracy measurements, which don’t matter that much here (see [resistor color codes]). Here’s a set with a bunch of resistors.

  • Breadboard of some sort + some wires. Here’s one to get started (you don’t need the power part for this project, but it’s handy to have a set of board + wires). 

Hardware

The basic idea is to use an output pin to control a signal, which will let more or less electricity flow. The electricity will flow through the LED strip and light a color appropriately. Do this for 3 colors and you have everything covered. 

On the ESP32 you don’t change the voltage of the pin directly (which would be an analog output – though some ESPs have that), but rather you change how often it’s turned on & off, effectively making it appear to be a different level. Technically you don’t change how often, but rather which percent of the time the signal is on (& appropriately which percent it’s off). This is called pulse-width-modulation (PWM). Most of the smaller microcontrollers have some form of this available by design, you can also do it manually with code that turns the output on & off, but it’s not really needed. 

The ESP32 outputs can’t send enough electricity to directly power the LEDs, so we use the output to control a transistor, which does it for us. It looks roughly like this, for each color:

Let’s walk through what happens here. The LED box on the right is where you connect the LED strip. The strip takes a +5V input, runs it through the LEDs, and returns it for Red, Green, Blue separately. If you let the electricity flow to ground, the LED lights. If you don’t let it flow, it stays off. The NPN transistor lets the flow of the electricity to ground, if there is some signal on the “base” (the middle line). With the ESP output, through a 1k Ohm resistor, we give a signal to the “base” of the transistor, letting the electricity flow. The NPN transistor type lets it flow when the signal is there; a PNP type would let it flow except when a signal is there. (A PNP would theoretically work too, but it would result in the LEDs being lit when your device is turned off, which is kinda weird, and you’d have to unplug it to turn it all off.)

To control the LEDs, we’ll use WLED, which is a cool wifi-based LED controller. It’s made for individually controllable LEDs, but it works for 5050 LEDs too. Because we’re using WLED, we’re going to have to use specific pins on the ESP32, namely: outputs 5, 12, & 15. If you’re making your own software, you can use pretty much any pin that you like. If you want to modify WLED, you can change the pins too. 

Here’s what the full circuit would look like with these pins:

A simple breadboard version of this could look like this:

If you plan on keeping this, obviously make it tidier :-). 

WLED setup

Build a modified WLED

WLED is an awesome project to control strips of LED lights via wifi. The initial setup is kinda tricky, but once you have it on your ESP, you’re ready to rock & roll. 

To use 5050 LED strips, you’re going to have to compile WLED yourself. That sounds trickier than it is, hopefully. You should be familiar with the command line & code editors to do this. You don’t really need to know how to code.

  1. Get a local copy of the WLED project.

Open a terminal window and use “git” to clone it to your local drive. 

git clone https://github.com/Aircoookie/WLED.git

  1. Get Microsoft Visual Studio Code & platform.io set up.

There’s a brief guide for this in the WLED wiki. 

  1. Open Visual Studio Code & make 3 edits

… in platformio.ini add a semicolon to comment out the existing “default_envs” line, as mentioned in the WLED wiki.  (This lets us select a build environment)

… in the same platformio.ini file, remove the semicolon from the “default_envs” line that mentions “esp32dev”. (This selects the generic ESP32 setup)

… in wled00/NpbWrapper.h remove the “//” from the beginning of the line with “//#define WLED_USE_ANALOG_LEDS” (this lets us use the 5050 LED strip)

  1. Plug your ESP32 in with a USB cable :)

  2. Build the code & upload it (in the bottom of the editor window, click the “->” button). It should compile without issue, and then slowly upload it to the ESP32. If you have other terminal / serial windows open (eg, from Arduino), close those beforehand. 

Hurray, it’s built. 

Initial WLED setup

Now you can connect to your device and try it out. To do so:

  1. Use your phone to connect to the ESP32 wifi, or scan the barcode to do it automatically. Usually it’s called “WLED-AP” and has a password of “wled1234”. 

  2. Your phone might guide you to the login page, or otherwise you’ll have to go there manually. It’s http://4.3.2.1/ . 

  3. Try a color! It should just work. If the colors are wrong, you mixed up the connections for Red Green Blue somewhere; this is easy to fix by swapping the cables. 

  4. Add your local wifi connection, so that you can access it easily. 

Click on “Config” (top menu), “WiFi setup”, and add the network name & password there. Make note of the name mentioned in the mDNS section, that’s the name you can use in a browser. Mine is “http://wled-fa3124.local” for example. 

  1. Scroll down and click “Save & connect” to reboot. 

  2. Let it connect to your local wifi, reconnect your phone to that wifi (probably happens automatically), and access the mDNS URL in a browser. 

  3. Done! Party mode?

Updates

2022-09-25 - Thank you to Dempsey for flagging that I’m a doofus and have the transistor wired backwards in my schematic. Woops. Fixed :-)

Comments / questions

There's currently no commenting functionality here. If you'd like to comment, please use Mastodon and mention me ( @hi@johnmu.com ) there. Thanks!

Related pages