Using the Microcontroller ESP8266 for automation tasks for the IoT

Test Hardware

For the initial test set up you will need an ESP8266-12 chip, an additional external power source, a USB like serial module [3], two switches, an LED and some resistors. The switch diagram in Figure 4 makes clear how to put the components together. Figure 5 shows the corresponding experimental assembly on a prototyping board.

Figure 4: The switch diagram for the experimental set up of the ESP8266.
Figure 5: The first experimental set up with the ESP8266 on a prototyping board.

The pins are located at a somewhat unconventional distance from one another on the ESP8266. Instead of the usual grid dimension of 2.54 mm, the dimension on the ESP8266 is 2 mm. This means that the chip does not fit perfectly on the board. As a workaround, solder wires onto the ESP8266 board. The wires should be about 2 centimeters long and half insulated. This little trick lets you rig the ESP8266 onto the prototyping board.

The table entitled "Pin Allocation (Bootloader)" shows the pins that need to be hooked up in order to activate the ESP8266 bootloader. Since you can only upload new programs when the bootloader has been activated, the pins have to be correctly wired when the microcontroller is started. As a precaution, do not connect the pins directly to the supply voltage. Instead, insert 10 KOhm resistors.

Since the USB serial module does not deliver a particularly strong output current, you will need an additional external power supply. A suitable power supply that can handle about 100 milliamperes suffices. In other words, a small plug-in unit which produces a stable operating current of between 3.0 and 3.6 Volts will work. Use batteries if you wish.

The ESP8266 does not deal well with too much operating voltage. Be very careful to set the USB to serial module a maximum output current of 3.3. The additional power source should also not exceed a maximum of 3.6 Volts. To prevent any mistakes with the operating voltage, first re-measure the output current of the USB to Serial module and the external power supply before connecting the ESP8266 supply voltage.

First Test

The development environment for creating the ESP8266 programs is now ready. Connect the experimental hardware set up to the PC via a USB port. The time has now come to create the first test program and upload it to the ESP8266. First open the example program Blink which is found in the Arduino development environment under File | 01. Examples | Basics | Blink. Using the functions setup() and loop(), change the number of the I/O port you want to activate from 13 to 16 as is shown in Figure 6.

Figure 6: The Blink program from the Arduino IDE, adapted for the ESP8266.

Next go to the Tool menu to check whether the board settings are correct (Figure 7). If so, move on to compiling and uploading your test program to the ESP8266. Pay close attention to the Port. It has to correspond to the one to which the USB to Serial controller is connected. After checking the settings, compile the program, and forward it on to the ESP8266. All you have to do here is click with the mouse on the switch with the right arrow in the IDE tool bar. This is located underneath the menu bar.

Figure 7: The board settings have to be a perfect match for the environment you are using.

While a blinking LED may be very fun to look at, the microcontroller is also supposed to react to actions. Therefore you will need to modify the program so that you can turn the LED on with one switch and then off with another. Listing 3 shows a suitably adapted version of the program.

Listing 3

Blink (modified)

void setup() {
  pinMode(12, INPUT);
  pinMode(13, INPUT);
  pinMode(16, OUTPUT);
}
void loop() {
  if (digitalRead(12)==true){
  digitalWrite(16, HIGH);
  }
  if (digitalRead(13)==true){
  digitalWrite(16, LOW);
  }
}

You will get an error message when you try to upload the program since the first Blink program is still running on the ESP8266. Listing 4 shows the accompanying message on the Arduino IDE. You will need to put the ESP8266 back into bootloader mode by cutting the operating current briefly. Then you can load a new program that automatically restarts the microcontroller. If you do not want the ESP8266 to go to bootloader mode immediately after starting, remove the wiring from the "Pin Allocation (Bootloader)" table.

Listing 4

Output of upload

The Sketch uses 198.668 Bytes (45%) of the program memory location. The maximum is 434.160 Bytes.
Universal variable use 32.982 Bytes (40%) of the dynamic memory, 48.938 Bytes remain for local variables. The maximum is 81.920 Bytes.
warning: espcomm_sync failed
error: espcomm_open failed

Table 1

Pin Allocation (Bootloader)

ESP-Pin

Logic Level

CH_PD

HIGH (VCC)

GPIO2

HIGH (VCC)

GPIO15

LOW (GND)

GPIO0

LOW (GND)

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content