Using the Arduino IDE to program the ESP8266

Using the Arduino IDE to program the ESP8266
Published: by

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.

Before being able to connect the ESP8266 to any programming software, the basic knowledge of wiring the chip into ‘flash’ mode (vs. boot mode) will be required. Refer to connecting the ESP-01 module to a breadboard and FTDI programmer for more information. 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.

To get started you will need

These were the parts that were used. Most are available from BangGood, eBay and RS Components. Direct links to some of the parts are supplied lower down.

  • Mini-B USB cable
  • Breadboard and solderless breadboard jumper cable set
  • Larger than ~185 Ohm resistor
  • 3 mm or 5mm LED

Apart from and ESP-01 or ESP-01S and the requirements for wiring this board into ‘flash’ mode, you will also need a device with a recent version of the Arduino IDE. Also see installing the Arduino IDE on a Raspberry Pi 3 for more information.

ESP-01 Arduino IDE examples

ESP-01 pinout

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:

Using the ESP-01's GPIO0 and GPIO2 as output

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.

More ESP8266 related posts

Try one of Lynda.com’s online software and skills learning courses
Try one of Lynda.com’s online software and skills learning courses
19 November 2018
Improve your knowledge on anything from programming to business skills with Lynda.com’s huge range of professional video courses. More…
The FTDI USB to TTL serial converter module
The FTDI USB to TTL serial converter module
5 June 2018
The 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 programmer
Connecting the ESP-01 module to a breadboard and FTDI programmer
12 September 2017 | Updated 4 June 2018
When 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…
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.