Wireless communication is essential to any Home Automation, IoT or other embedded device and sensor setup. Whether adding devices in an existing house, accessing remote sensors or simply avoiding expensive wiring, in many cases some form of RF communication is the only feasible option. For in-house projects wired communications can be in some cases be useful, like Ethernet, 1-Wire, RS485 or even PLC (Power Line Communication). But these days wireless is all the rage and I will therefor dive deep into the world of RF communications, starting with this blog post, which will be mainly focused on an ESP8266 based development board.

Especially for in house projects, there can be alternatives to communicate like Power Line Communication (PLC). It is an interesting technology and I have a few PLC modem IC's, the TDA5051 from NXP (formerly Philips), in stock. I plan to do some designing and testing in the future when the need arises.

No wires

RF modules come in all sizes and shapes and are usually quite cheap these days. On ebay.com, aliexpress.com and other e-commerce sites you will find an overwhelming amount of RF modules. I wanted to be able to experiment with different frequencies (from 433 MHz up to 2.4GHz) and different types of modulation and antenna's. So, I ordered a bunch of RF modules from AliExpress. Ranging from simple 433 MHz modules like these, but also a bunch of nRF24L01 modules, like these, and a pair of complete embedded modules for Wi-Fi based on the famous ESP8266, also from AliExpress.

Today, I received my pair of NodeMCU boards in the mail, containing an ESP8266 WiFi SoC module and a CH430 Serial-USB interface on a tiny PCB. NodeMcu hardware and software is open sourced and these particular boards I ordered are manufactured by WeMos Let's have a closer look at this System-on-a-Chip. The NodeMCU comes pre-loaded with firmware that supports programming in Lua, a simple programming language targeted at the novice user.

Unpacking the ESP8266 module

There is already a lot of information on NodeMCU and other ESP8266 based boards out on the internet, so I will stick to the practical side and share my experience with you. The two boards I received came with a set of pin headers in various shapes to accommodate the boards to different uses, including breadboard experiments, PCB soldering and stacking shields on it. From a hardware point of view I can definitely recommend this little board for experiments (and even complete projects). It is very compact and the PCB seems to be of good quality, as is the soldering work. You may find even cheaper boards on eBay or AliExpress, but the WeMos models are very compact and of decent hardware quality.

Mini NodeMCU top view

The particular NodeMCU boards I ordered can be found at aliexpress. I paid EUR 6.50 for two of them with free shipping to The Netherlands. You just need some patience as it can take 2-4 weeks before they arrive in the mail. The boards are labeled 'D1 mini - Mini NodeMCU 4Mb Lua Wi-Fi IoT development board' and made by WeMos. As you can see on the picture above, it is quite small and has all it's relevant I/O pins on the side for easy access and can be connected to a computer through its micro USB connector for serial communications over an USB link.

The D1 mini is supposed to be compatible with Arduino tools, so you can write sketches for the device if you are familiar with the Arduino development environment and libraries. It uses the Arduino core library for ESP8266 you can find here on GitHub. I have not tried this myself but here you can read all about it.

Anatomy of the NodeMCU

Since the ESP8266 is a complete system-on-a-chip, there are few additional components on the PCB. Besides a bunch of resistors and decoupling capacitors, a micro-USB connector and a reset switch, there are only a few active components to make the ESP8266 board complete. The most relevant is a CH340 serial-USB controller, which is soldered at to bottom of the board. It is a cheap Chinese replacement of the famous but expensive FTDI FT232 chip, is produced by WCH (WinChipHead), and does it's job well. There are different models of the CH340 series that include the CH431 and CH342. On the D1 mini we find the CH340G (there is also an A, S, P, T, R and B version with small variations in package and pins). The CH340G needs a 12MHz crystal to provide the right clock signals. You can turn to the manufacturer's site for drivers for Windows, Linux and OSX - use Google Translate if Chinese is not your native language. You should download the driver for the CH341 as it also supports the CH340. There is even software for Android, which I haven't looked at. And since the datasheets are all in Chinese on the WCH site, I found an English version of the datasheet here and on the seeedstudio website. The particular model CH340G is not addressed but by keeping the Chinese datasheet with the pin layout of the CH340G next to it, you can work out the details.

Update: I forgot the obvious route; you simply can go to the WeMos website for appropriate drivers for the CH340G.

You must install the driver before you can communicate with the NodeMcu from a PC. The device presents itself on the USB interface as a CH340 serial-USB controller as can be seen in this screenshot of de Windows Device Manager.

NodeMCU Serial-USB device

There is also a tiny blue LED and a voltage regulator (located next to the micro-USB connector) to turn the 5V input power from the USB connection into 3.3V for the ESP8266.

A word of warning - the ESP8266 operates at 3.3V and doesn't like to be fed 5V signals, like from a standard Arduino. It probably won't kill your device immediately, but you will most likely get erratic behavior.

Finally there is a pair of transistors that can do the needed magic with the GPIO0 and RST lines to put the ESP8266 in the right mode for booting, flashing, etc. These are controlled by the CTS and RTS lines of the serial interface side of the CH340. Since the USB side has no such signals, there is some 'magic' involved to control these lines. More on that later.

The NodeMCU hardware design is open source and you can find the schematic here on GitHub. And elaborate documentation for the NodeMCU is available on readthedocs.

To see if I could persuade the D1 mini to do anything useful (well, that is a matter of perspective), like connecting to a Wi-Fi Access Point, I started PuTTY for serial communications with the NodeMCU. The baud rate must be set to 9600 and any handshaking can best be turned off as shown in the screenshot below. There you can also see the COM port to specify in PuTTY.

NodeMCU PuTTY setup

Pressing the tiny reset button on the side of the board initializes the device and it will display its basic information on the terminal. After some initial garbage characters - probably because the serial interface is not yet fully initialized at that moment - you can see the information about the firmware and the included modules. Quite an impressive list.

NodeMCU Startup

It ends with an error stating that 'lua.ini' is missing, but that was to be expected since no Lua program has been uploaded yet. It looks like the device is working. Next, I checked if I could connect to one of my Wi-Fi AP's. That requires only a few commands with Lua. Replace the SSID and password with your actual values, and keep the quotes around it.


This worked like a charm as you can see in the screenshot of the output below.

NodeMCU WiFi access

Pretty cool, don't you think? Just by entering three commands you are connected to your Wi-Fi network and potentially to the Internet (of Things) from such a small embedded device. Ok, perhaps not really something to get too excited about, but at least is shows the device is working properly. Some people will be quite happy with the device as is and do their magic with Lua programming and connecting sensors to the NodeMCU. But not me. Apart from the fact that I have read about stability issues with larger Lua programs and its memory consumption, I want to have native code running directly on the ESP8266 for some of my planned projects. Besides, what's the fun in using something that's ready to use; I want to learn and build. That's what a true Maker is all about... Did I just call myself a Maker? Ugggh!

Closer look at the ESP8266EX

The Espressif ESP8266, or more precisely the ESP8266EX chip (in QFN32 package) contains most of the magic. An external crystal (40, 26 or 24MHz) and an SPI Flash chip (usually between 2 and 8 Mb) complete the module, next to a few passive components. The components inside the ESP8266 are shown in the block diagram below (taken from the ESP8266 data sheet).

ESP8266EX block diagram

You can clearly see there's an analog side (left) and a digital side (right). The digital side is the most interesting since that's where start doing to programming and other magic. Notice there is no ROM in the chip, only (Static) RAM. That's why an external Flash ROM chip is needed. The I2C interface also provides the UART functions, and the GPIO has 4 pins that can provide timer driven PWM signals. The diagram gives a very simple overview of the core components and some are even missing, like the timer and ADC functions.

The chip operates at 3.3V (not 5V like many Arduino type devices!) and has two digital power lines that are usually both connected to a 3.3V power source. In addition there are five analog power lines, needed for some internal functions like PLL, that derive their power from the 3.3V line with some capacitors to minimize the impact of sudden current increase during transmit. For reset and power on/off there are two pins: the actual EXT_RSTB pin (pin 32) and the CHIP_EN pin (pin 7). The latter functions as a chip enable pin and when low powers off the ESP8266EX. Note that CHIP_EN must not be powered before the power pins. It's probably best and easiest to just keep wired to the 3V3 with a 1k pull-up resistor, otherwise it must be controlled from another MCU. And while we are on the topic of powering the device, the ESP8266 is quite sensitive to ripple and we should ensure it stays below 80mV while transmitting. The ESP has 3 power modes: active, sleep and deep sleep.

The RF output (pin LNA) has 50 ohms impedance and any external antenna should match that; be careful with cheap Chinese models. For a compact design PCB antenna is the obvious choice, but be aware that this reduces the range considerable compared to an external antenna. In all cases, try to keep 15mm around the antenna PCB area free of other components and signals. High frequency signals from any external MCU can, if PCB layout is not optimal, interfere with the RF signal. Another reason to get to know the inner workings of the ESP8266 so we can run our software native on the chip.

The CPU of the ESP8266 is a custom chip from Tensilica, a modified L106 32-bit processor running at 80MHz. This Tensilica Diamond CPU is fully programmable on microcode level. This allowed Espressif to create their own processor instructions. Details on the custom L106 are therefor quite scarce. So far I haven't been able to find a definitive answer on the RAM size on the chip, but the data sheet states there is about 50KB available (after the SDK boot loader stuff and others are loaded).

The ESP8266 has the usual I/O features, including 2 UARTS, I2C interface, a 10-bit ADC, 4 PWM outputs and several general purpose I/O pins (GPIO). Many pins have multiple functions and through programming (setting or clearing bits in specific registers) they are put in the right mode. On the Espressif reference schematic you can see some of those I/O functions.

Boot modes

To better understand how to program and use the ESP8266, we must take a good look at the various boot and flash modes. So far I have found the following modes, but there are other scenario's to discover once we are going to dig deeper.

Mode Description
UART Download Pull GPIO0 and GPIO15 low and leave GPIO2 unconnected to enter UART download mode when powering up. Writes program into Flash or RAM.
Flash boot Pull GPIO0 high, GPIO15 low and leave GPIO2 unconnected to boot from flash when powering up.

The ESP8266EX supports up to 16Mb flash memory, and the minimum is 512Mb (that is mega bit). It works like a set of memory banks and you can switch between them. And the OTA (over the air) programming function uses that fact to flash the firmware in another bank of the Flash ROM before switching.

An overview of the pins that are relevant for flashing and booting is shown in the table below. This is what I have found and verified so far. There are other options to be discovered...

PIN IN/OUT Main function(s)
CHIP_EN IN Chip enable. Low powers down, high powers up (for normal operation)
GPIO0 IN Low at boot enters flash upload mode; high for normal boot
GPIO15 IN Low at boot to enter flash or normal boot; high enters special boot modes
GPIO16 IN Wake up from deep sleep when connected to RST

Chain of tools

Now we have established that the NodeMCU works and can be communication with a PC, it is time to start looking at the software and programming side of things. When you start digging into the world of the ESP8266, it is easy to get lost in all it's different versions and models, the latest of which seems to be the ESP8266-12E. A very complete (so it seems) overview can be found here. And since Espressif, the manufacturer of the ESP8266, provides software that is not always without bugs and documentation that is sometimes just plain wrong information, there is a certain learning curve to overcome. Add to that the fact that many small variations of the ESP8266 are built by various OEM's, and you can say there is some chaos and scattered information to overcome before you can fully master the ESP8266 in all its different shapes and sizes. Fortunately, there is quite a community around the ESP8266, which in large part explains it's success, but the information is a bit scattered and sometimes outdated or hard to find between all the forum posts. One of the sites definitely worth mentioning is esp8266.com with an active developer group and Github corner where the sources can be found. Ha, there is even a BASIC interpreter available for the ESP8266, go figure! On GitHub you can also find a Wiki.

To do any native development with the ESP8266, you need a toolchain tailored for this device. The manufacturer provides an SDK but it has some binaries and other closed-source stuff in it, which make it more difficult to include in your (commercial) projects or tailor it to your needs.

Recently Espressif announced a new model, the ESP32. Although information is still scarce at the time of writing this blog, it sounds like it will be a powerful device that also supports Bluetooth 4.0 (LE). But, it is not necessary a direct replacement of the ESP8266, since it will most likely have a higher price point.

The toolchain we are looking for enables us to compile the software to native machine code, provide libraries and bootstrap code, link everything together in one specially formatted file for writing to the device's ROM, the flashing program to do the actual writing to ROM, do debugging on the device, and stuff like that. These toolchains are sometimes also referred to as SDK (Software Development Kit). Basically we have three choices:

  1. Download and start using the Espressif SDK, with some binaries we can not modify if needed.
  2. Turn to an open sourced version of some parts of the SDK, combined with the manufacturer's SDK material.
  3. Use a development tool (IDE) that can support the ESP8266 out-of-the-box.

In one of the next blog posts, I will look at using a supported IDE, but first I am going ro dig a little deeper in the other options to better understand what makes up the toolchain for the ESP8266, starting with the official SDK from Espressif. They released a new version shortly before I started this blog post.

The ESP8266 SDK

We'll have a quick look at the SDK and see what is relevant to download and study. In a next blog post I will share my findings with you. For the official SDK we turn to the Download Section on the manufacturer's website. There we find the latest release of the ESP8266 SDK (currently V1.5.3), dated April 18, 2016. Let's just download it and have a closer look. There are two versions of the SDK: one for stand-alone scenario's and one for an RTOS-based (Real Time Operating System) system. We go for the non-OS version for now as we are aiming to make some simple sensor reading programs first - no need for an OS. To be sure you get the right one: it is called ESP8266 NONOS SDK V1.5.3. Please, always read the release notes carefully.

All the official documentation for the SDK and the ESP8266 hardware and interfaces can be found here.

This concludes my initial remarks on the ESP8266. Next time we will take a good look at the SDK and documentation (I have some reading and testing to do first).

Add a comment

Next Post Previous Post

Add a comment