Using the Arduino IDE to program the ESP8266

If you are using anAd Blocking Browser Extension
please consider deactivating it for this website.Also see our Ads Policy.

After several steps of preparation, the ESP8266 can finally be programmed. Fortunately this step has been made fairly simple by using, what is a familiar platform for many, the Adruino IDE. This post will show how to upload a few variations of the basic Blink sketch to an ESP-01 together with the wiring of LEDs.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

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 for using all three these pins will be given.

Before sketches can be uploaded, the ESP-01 needs to be booted in ‘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 on board LED (GPIO1) as output

To start off, the sketch below will make the ESP-01’s blue on-board 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. 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’s, the on-board 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 in the variable section. Also see the 4 basic sections of an Arduino sketch for more information.

/*
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/data sheet.

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 generally safe to use. 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. It doesn’t matter which side of the resistor is used where.

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 [and the resistor needs to be connected from the cathode (short leg) to GPIO0/GPIO2]. This will mean that GPIO0/GPIO2 will be LOW (i.e. on) during boot up 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 (also see the 4 basic sections of an Arduino sketch for more information):

// 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 was uploaded, the LED needs to be connected as follows:

To connect a LED to GPIO2, the green wire needs to be moved to GPIO2 in stead. Both GPIO0 and GPIO2 can be used as digital outputs at the same time.

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a shit. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

Getting started with breadboard power supplies15 October 2018Breadboard power supplies are easy-to-use, companion modules which provide regulated voltage to the power rails of solderless breadboards. They are both easy to use and extremely useful for fast, efficient breadboard prototyping of micro-electronic projects. More…

Using the UART interface5 June 2018UART (universal asynchronous receiver/transmitter) is a communication interface used between two microcontrollers. Data signals are transferred using two lines, one for transmitting signals (Tx) and one for receiving signals (Rx). More…

The FTDI USB to TTL serial converter module5 June 2018The FTDI USB to TTL serial converter module is an UART (universal asynchronous receiver-transmitter) board used for TTL serial communication. It is a breakout board for the FTDI FT232R chip with an USB interface, can use 3.3 or 5 V DC and has Tx/Rx and other breakout points. More…

Connecting the ESP-01 module to a breadboard and FTDI programmer12 September 2017 | Updated 4 June 2018When compared with some other microcontroller boards (e.g. the Arduinos), the ESP8266 is slightly more complex to hook up to a serial port for programming. This post will show how to connect the ESP-01 module to a breadboard and FTDI programmer for ‘flashing’ purposes. More…