After preparation, the ESP8266 can finally be programmed. This step has been made fairly simple by using, what is a familiar platform for many, the Arduino IDE.
Table of contents
Introduction to using the Arduino IDE to program the ESP8266
The ESP8266 chip can be programmed by using the Arduino IDE. Here we will show how to connect a LED to the ESP-01 module (with the ESP8266 chip) and upload a few variations of the basic Blink sketch to its memory using the Arduino IDE. The ESP-01S module can also be used.
- Get the ESP-01 module (blue) from BangGood or Amazon.com
- Get the ESP-01S module (black) from Amazon or BangGood
Requirements
Before being able to connect the ESP8266 to any programming software, the chip needs to be wired in ‘flash’ mode (vs. boot mode). In addition to this, the ESP8266 core needs to be added to the Arduino IDE and the appropriate FTDI programmer drivers need to be installed.
- A Mini-B USB cable to connect the FTDI programmer to a PC Amazon.com or BangGood
- A breadboard for prototyping Amazon.com or BangGood
- Solderless breadboard jumper cable set for prototyping Amazon.com or BangGood
- Larger than ~185 Ohm resistor Amazon.com or BangGood
- 3 mm or 5mm LED Amazon.com or BangGood
- Get the Basic Arduino Uno R3 Starter Kit from Amazon.com or BangGood
- Get the Microelectronics Starter Kit from Amazon.com or BangGood
ESP-01 Arduino IDE examples
The ESP-01 has two, easy-to-use, GPIO pins (GPIO0 and GPIO2) and a blue on-board LED (GPIO1). Examples of using all three of these pins will be given.
Before sketches can be uploaded, the ESP-01 needs to be booted into ‘programming’ mode. When using the breadboard and FTDI programmer method, the SPDT slide switch needs to connect GPIO0 to GND before pressing the reset switch.
After a sketch is uploaded, the ESP-01 should be wired back to ‘normal’ operating more and rebooted i.e. the SPDT slide switch should not connect GPIO0 to GND before pressing the rest switch.
Using the ESP-01’s onboard LED (GPIO1) as output
To start off, the sketch below will make the ESP-01’s blue onboard LED blink at 1-second intervals. Because GPIO1 on the ESP-01 also doubles as the Tx pin, the serial monitor cannot be accessed at the same time the onboard LED is used. Without having to connect anything to the ESP-01, upload the following Blink sketch:
/* ESP8266 Blink Turns on an LED on for one second, then off for one second, repeatedly. Blink the blue LED on the ESP-01 (and the ESP-12) module ESP-01s have an on-board LED you can control. On the ESP-01 and ESP-12, it is attached to GPIO1. (which is also the TXD pin; so Serial.print() cannot be used at the same time) */ #define LED 1 // define connection of LED, in this case GPIO1 // the setup function runs once when you press reset or power the board void setup() { pinMode(LED, OUTPUT); // initialize the LED pin as an output } // the loop function runs over and over again forever void loop() { digitalWrite(LED, LOW); // turn the LED ON delay(1000); // wait for a second digitalWrite(LED, HIGH); // turn the LED OFF delay(1000); // wait for one second }
Note that on the ESP-01, the onboard LED is turned on by a LOW voltage and off by a HIGH voltage.
Using the ESP-01’s GPIO0 and GPIO2 as output
The sketch below will make an external LED connected to the ESP-01’s GPIO0 blink at 1-second intervals. To use GPIO2 instead, simply change #define LED 0
to #define LED 2
in the variable section.
/* ESP8266 Blink Turns on an LED on for one second, then off for one second, repeatedly. */ #define LED 0 // define connection of LED, in this case GPIO0 // the setup function runs once when you press reset or power the board void setup() { pinMode(LED, OUTPUT); // initialize the LED pin as an output } // the loop function runs over and over again forever void loop() { digitalWrite(LED, LOW); // turn the LED off delay(1000); // wait for a second digitalWrite(LED, HIGH); // turn the LED on delay(1000); // wait for a second }
Out of the box, LEDs will have a long leg (anode) and a short leg (cathode). The specifications (forward Voltage or Vf @ a specific current or If) of LEDs (usually size and colour dependent) will be available from the manufacturer/datasheet.
Each pin of the ESP8266 supplies 3.3V DC and 12mA of current. It is generally not recommended to draw more than 7mA per pin, so according to Ohm’s law, a current limiting resistor of ~185 Ohm or more is recommended.
Resistors can be connected directly to a breadboard. One leg is connected to the cathode of the LED and the other leg is connected to the ground (GND) pin of the ESP-01. There is no polarity on the resistors, so it does not matter which side of the resistor is connected to ground.
LEDs can be connected directly to a breadboard. Because the input status of GPIO0 and GPIO2 is used during the boot process to determine the mode which the ESP8266 needs to enter, their default status should not be altered. Instead of connecting the LED between the GPIO pin and GND, it needs to be connected between Vcc and GPIO0/GPIO2. The resistor needs to be connected from the cathode (short leg) of the LED to GPIO0 or GPIO2.
This means that GPIO0/GPIO2 will be LOW (i.e. on) during bootup until its value is set to HIGH (i.e. off). To keep the LED off until the loop section has been reached, the following command can be added to the sketch’s setup section:
// the setup function runs once when you press reset or power the board void setup() { pinMode(LED, OUTPUT); // initialize the LED pin as an output digitalWrite(LED, HIGH); // turn the LED off during setup }
After the sketch has been uploaded, the LED needs to be connected as follows:
To connect a LED to GPIO2, the green wire needs to be moved to GPIO2 instead. GPIO0 and GPIO2 can also be simultaneously used as digital outputs.
Conclusion
The ESP8266 chip can be programmed by using the Arduino IDE. Here we showed how to connect a LED to the ESP-01 module (with the ESP8266 chip) and upload a few variations of the basic Blink sketch to its memory using the Arduino IDE.