Introduction: NODEMCU LUA WeMos ESP8266 With SD Card, Relay & WS2812

Before I start, I am assuming you know how to build custom firmware and flash it to your ESP8266. You will need to do this for the SD card and WS2812 shields.

My set up of the WeMos ESP8266 is on a dual base with the dev kit plugged into it. The SD card shield is plugged into dev kit and the WS2812 is plugged into the top position. It does not have to be done this way as you could use the shields side by side or whatever way you choose.

As the WeMos has a built in CH340G UART, you don't need a separate one as with some other systems.

I give the dual board an extra 5v and Gnd from a breadboard supply. It does work without this extra supply, but puts less drain on the USB supply from the PC.

I have included an init.lua file which you might find useful for when you are setting up your system.


Step 1: Wemos and ESP8266 Pin Outs

I have included the pin out diagram for the Wemos dev kit as well as the pin identification and index of ESP8226.

TX and RX can also be used as I/O lines after you have flashed the device, you will need to disconnect them during flashing.

In total there are 9 or 11 I/O lines and 1 ADC input, so there is ample to work with.

Step 2: WeMos Relay Shield

Nothing too complicated about this one. Plug your WeMos relay shield into the dev kit and load the code.

Before you run the code you need to enter your router's SSID and password into the program. You may also want to change the IP address.

When you have done this enter the IP address into a browser and you can control the relay wirelessly.

The WeMos shield has the relay connected to D1 (GPIO5).

On the relay the centre connection is common (i.e. connect +12v), NO (Normally Open) and NC (Normally Closed) are the outputs. You could connect a 12v or mains electric light to the relay. I am not going into how to do this here. Remember mains electric can kill you!

The lua code is what I have cobbled together from various sources on the web.

Step 3: WeMos WS2812 Shield

To use this shield, you will need to install the WS2812 module.

Instructions for this can be found here:

When you have selected your modules, they will be emailed to you and you can flash your firmware with them.

You will probably need to replace your esp_init as well. You will know this if your ESP8266 does not boot up after your have flashed your new firmware. i.e. the blue LED just flashes when you try to power it up.

I have included an example of the .bin files (nodemncu-master and esp_init) that you also need to flash your firmware. Of course you can select as few or as many modules as you like to build your firmware. If you are going to use the SD card you need to include FatFS support for reading from SD Cards in your build.

When you have flashed the ESP8266 you should get a start up as above.

Step 4: Using the WS2812 Shield

This shield and the module have a problem when you get them. The module uses pin D2 (GPIO4), but the shield uses D4 (GPIO2) pin instead. I fixed this by clipping off the D2 pin (underneath) and soldering a wire form D4 to D2 as in photo.

If you have got this far, then I have included some lua code to give a basic demo of how it works.

For some reason the module uses GRB instead of the usual RGB.

I have used 2 different methods to show how it can be done.

See NodeMCU Documentation for full details of the module.

Step 5: WeMos Micro SD Card Shield

The micro SD shield is similar to other SD shields in that it uses an SPI interface to read and write to the card. If you are familiar with the Arduino version then this works like it. You need to build your module with SPI and the file system for the code to work. See section above to see how to do this.

When you reset the WeMos for the first time after flashing the module, there is a short delay while the FAT filing system is formatted. You need to have the baud rate set to 115200 on Esplora or other IDE or you will only see garbage while this is happening.

I am using an 8Gb micro SD card in the shield.

The Lua program that I have provided has been gleaned from the web with additions by myself.

First of all you need to initialise the SPI interface otherwise the file commands will not work. Next you have to mount the drive, otherwise the commands will work on the flash memory rather than the SD card.

Once you have mounted the drive the program lists all the files on the SD card.

It then creates a text file (Hello.txt) and write a line of text to it, and closes the file. After reopening the file in append mode, it adds another line of text and closes the file again. It then opens the file in read mode and displays its contents before closing the file again. The file can be erased by the remove command.

The program changes the source back to flash memory and lists all the files there.

I have just showed a small sample of what can be done with the SD card module. See NodeMCU documentation for the full set of commands and examples. I think it is possible to serve web pages from an SD card or to put your Lua programs here rather than flash memory. More work to be done here!

Step 6: Conclusion

There are more shields that can be added onto the WeMos dev kit and I have covered a few of them that don't seem to have much written about them. I am still finding it difficult to find good Lua code and documentation on the web, but perseverance normally gets you what you want.

I am still working with this kit and may add more to this instructable at a later date. Meanwhile, I would be interested in any comments or code you have developed for this and other ESP8266 devices.

The examples above are for the WeMos kit, but should run OK on other ESP8266 devices, but you may need to wire them together rather than just stack the shields.