How to control addressable RGB lights with a ESP8266. Part 1 of 3.
Tags: micropython
Published: 2021-03-14 05:59:25

This will be a multi-part blog covering the steps needed to setup a micro web server on a ESP8266 microcontroller and use that to control a string of addressable RGB lights. The first part will cover the environment set-up, part two will be about controlling the RGB led lights, and part three will be implementing a basic web server to allow the lights to be controlled from a page served by the ESP8266.

If you wish to follow along, you will need the following items:

* EPS8266 - These can be ordered from several places but shouldn't be more than a few dollars. Amazon has unreasonably high prices for these and you should shop around if you can. If you want a name brand item for the consistency, I recommend Adafruit, although the Feather HUZZAH is the most expensive board. You get what you pay for, mostly. You can save a few dollars sometimes if you order the board without the headers soldered and do it yourself. In part two, we are going to be soldering sockets to the prototyping board to plug the modules into so you will need to order headers with your sockets if the board you choose does not include them.

* 3.3v-5v level shifter. This is not actually required since the leds will usually work correctly at 3.3v, but this will allow longer strings of lights to work without error. It's best to buy a few of these at a time since they are so cheap. You could also do a single transistor to convert the signal, but a level shifter on a pre-made board is easiest to implement.

* 5VDC power supply. This heavily depends on how many lights you want to run. For the purposes of this demo, I'm using a 1500mA power supply obtained from a wall transformer. These are very common and anything 1000mA or more should be fine. If you are only doing 1-3 lights, you can use the pin on your ESP8266 that connects to the USB plug and get your 5v there. This pin will not be connected if you are using an external power supply.

* Addressable RGB leds. These come in two main varieties and the main distinction is the order of the light's color message information. There are RGB and RBG and they are basically identical, except the order in which the messages are set, which will be covered later. Either one is fine, as long as they are 5 volt.

* Electronics prototyping supplies. I won't go into too much detail here, but the basics are some breadboard, sockets for the ESP8266 and level shifter (so they can be reused in another project), screw terminals, and a bit of 24ga wire. Some basic searching should put you on track to get everything you need pretty easy.

### Environment set-up

The first thing to do is get the required software to build micropython. This is needed over using a pre-built image because there is limited memory available on the ESP8266. In order to run more complex programs and not run out of memory, it is required to precompile your program when the firmware is built. This will place the your program in a different part of memory which will free up ram, which is very limited. I followed the guide available [here](https://learn.adafruit.com/building-and-running-micropython-on-the-esp8266/build-firmware) with one extra step. The dependencies were broken for me, so after running `vagrant ssh` for the first time, run `sudo apt -y install git autoconf gperf bison flex texinfo help2man libtool libncurses-dev build-essential python-dev` inside the vagrant machine before trying to compile the toolchain.

Once you have the toolchain built, continue to follow the guide until you are able to connect to the board over serial and you get a REPL. Once you know that you can connect and the firmware you flashed is working, then you are ready to start working on connecting to the leds, which is covered in the next blog.