ESP8266 Tutorial: Programming the Onboard GPIO Pins

By Robin Thomas

esp8266

The ESP8266 is a great tool for enabling your project to access the internet. You can plug it into an Arduino easily as shown and allow your project to communicate through the internet. Or even more exciting, to control it from anywhere in the world!

The ESP8266 is a very low-cost module that comes pre-programmed with an AT command set firmware, meaning, you can simply hook this up to your Arduino device and get about as much WiFi-ability as a WiFi Shield offers. This module has a powerful on-board processing and storage capability that allows it to be integrated with the sensors and other application through its GPIOs.

In this tutorial, we can see how to get started with the ESP-01 Wi-Fi module, configure it, and verify that there is communication established between the module and another device without using an FTDI chip.

Bill of Materials

  1. Arduino board.
  2. ESP8266.
  3. Breadboard.
  4. Female to male and male to male jumpers.

How Does it Work?

The ESP8266 communicates with the Arduino through a serial interface. That is, it uses theArduino’s Rx and Tx pins (digital pins 0 and 1) hooked up to the module for receiving commands and communicates back. The module features a full TCP/UDP stack support and can be configured as a web server.esp8266 pinout

The ESP8266 or ESP-01 module also has two GPIO pins that can be used for directly manipulating any output devices like LEDs or relays. Moreover, the ESP-01 module powers the onboard ESP8266 through 3.3V. Therefore if your Arduino doesn’t have a 3.3V output pin, you will have to use a voltage regulator like the 78R33 to step down the 5V from the Arduino to 3.3V in order to power the ESP8266.

Setting up the Arduino

First, download Arduino IDE ensure that you have the latest software version (some older versions won’t work). Visit the following URL: https://www.arduino.cc/en/Main/Software.

Upload Bareminimum sketch from examples to your Arduino, this is to make sure that there are no other programs running on the Arduino and using serial communication channel. AT firmware is compatible with the Arduino IDE, so we are going to use this firmware for this tutorial.

 

Arduino Bareminimum ESP8266

 

Now, connect the ESP8266 using the instructions below

  • VCC will be connected to the 3.3V power supply
  • GPIO0 and GPIO2 are general purpose digital ports. GPIO0 also controls the module mode (programming or normal operation). In our case (normal operation), it shall be connected to 3.3V (high). GPIO2 is not used in this example.
  • Rx: Goes to Arduino pin0 (But needs a voltage adjusting)
  • CH_PD: Chip enable. Keep it on high (3.3V) for normal operation
  • RST: Reset. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.
  • GND is ground.
  • Tx: Goes to Arduino pin1.

 

ESP8266 connection

 

ESP8266 modes: (Setup continued)

Open the serial monitor and change the baud rate to 115200 and choose NL and CR (new line and carriage return) from the drop-down menu. Type AT on the serial monitor you will see: OK as the response. If you see garbage value on the screen, try resetting the module, or checking your baud rate. Make sure the NL and CR option is set. Moreover, the ESP8266 module has three operational modes:

1Access Point (AP)

In AP, the Wi-Fi module acts as a Wi-Fi network, or access point (hence the name). It allows other devices to connect to it. And establishes a two-way communication between the ESP8266 and the device that is connected to it via Wi-Fi.

2Station (STA)

In STA mode, the ESP-01 can connect to an AP(access point) such as the Wi-Fi network from your house. This allows any device connected to that network to communicate with the module.

3. Both

In this mode ESP-01 act as both an AP as well as in STA mode.

Refer to the following site for more ESP8266 AT commands.

Installing the ESP8266 Platform

First, the Arduino environment has to be setup to make it compatible with the ESP-01 module. It is required to have Arduino version 1.6.4 or higher in order to install the ESP8266’s platform packages.

1.Open the preferences window from the Arduino IDE. Go to File >Preferences

 

ESP8266 preferences

 

2. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field and click the “OK” button

 

ESP8266 setting

 

3. Open boards manager. Go to: Tools > Board > Boards Manager…

 

ESP8266 setting

 

4. Scroll down, select the ESP8266 board menu and install “esp8266 platform”.

 

ESP8266 setting

 

5. Choose your ESP8266 board from Tools > Board > Generic ESP8266 Module

 

ESP8266 setup

Controlling the Input and Output

In Arduino IDE, go to “examples” and open the blink program. Change the pin number to 2 from 13, since there are only two GPIObpins for the ESP8266 (GPIO0 and GPIO2).

NOTE: Make sure that GPIO0 is grounded while uploading the code.

ESP8266 connection

 

Connect the ESP-01 module as per the diagram above, then upload the sketch shown below and watch the magic!

 

ESP8266 program

 

Feedback and Questions are welcome! Thanks for reading.

Recommended Posts
Showing 25 comments
  • Apiwat
    Reply

    I found a solution.
    1. GPIO0 => GND
    2. RST => VCC
    3. Upload
    if can’t upload. pull RST out and jump to VCC again, then upload. (For me, i try 2 or 3 times)
    it worked for me!!

  • Sumesh Sivan
    Reply

    I am confused. The BOM is
    1. Arduino board.
    2. ESP8266.
    3. Breadboard.
    4. Female to male and male to male jumpers.

    But in the circuit diagram (4th image), there is a transistor/ chip wired in the breadboard.

    Please explain this.

  • jai garg
    Reply

    MY ESP IS NOT ACCEPTING AT COMMANDS AFTER UPLOADING THIS SKETCH WHAT TO DO

  • matsolo
    Reply

    For all people having error when uploading. It is very easy actualy . It is just that you cannot upload the prog in your esp8266 if it is not in flashing mode.

    To put it in flashing mode : gpio 0 to ground (steady) then reset pin to ground ( just a pulse). Your esp is now ready to be uploaded with your program

  • Mr C
    Reply

    Mine works fine (i think!), LED flashes along with the Blue LED on the ESP8266

  • Jason
    Reply

    Thank you for this tutorial to help get me started…

    My arduino sketch seemed to upload okay, but I’m not getting any output from the serial port. I took out all references to wifi and uploaded this simple sketch below, but still nothing:

    void setup() {
    delay(100);
    Serial.begin(115200);
    delay(10);
    }

    void loop() {
    Serial.println(“TEST”);
    delay(1000);
    }

    I’m using the following adapter with a added button to put it into programming mode:
    https://www.amazon.com/ESP8266-Wireless-Computer-Communication-Development/dp/B01J2UXXCA/ref=cm_cr_arp_d_product_top?ie=UTF8
    Then connect the reset pin to ground for a moment to reset.

    Am I missing anything?

  • Oguzhan Ozdogan
    Reply

    Hi, I want that connect between raspberry and arduino/esp8266 with modbus. I did it with ethernet shield. But I cant do it ESP8266. Can you help me this about?

  • Aadithya S
    Reply

    I’ new to this, so bear with me if I sound stupid. Can the GPIO0 and GPIO2 be both used as a input? Can any of the the other pins be used as a input ? I would like to have 3 input pins if possible, Thanks for your time?

  • Anders Borg
    Reply

    “Therefore if your Arduino doesn’t have a 3.3V output pin”

    The Uno has (confirmed). It’s even shown in the example, but it’s not used. Too low wattage?

    Cheers,
    Anders

  • Barun Kumar Jha
    Reply

    Hi Robin,
    Is it possible to directly connect sensor data pin to GPIO pin of ESP8266 module and send data to receiver module/board?

  • Dhamodharan Krishnan
    Reply

    This Sketch is not working as described I am getting below error as described by Scott. Please advise.

    Sketch uses 222,153 bytes (51%) of program storage space. Maximum is 434,160 bytes.
    Global variables use 31,528 bytes (38%) of dynamic memory, leaving 50,392 bytes for local variables. Maximum is 81,920 bytes.
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

  • Scott
    Reply

    I’ve followed your tutorial, which was well laid out and easy to follow, by the way! When trying to upload the sketch, I get the following errors:

    Arduino: 1.6.13 (Windows 10), Board: “Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck, Disabled, None”

    Sketch uses 222,237 bytes (51%) of program storage space. Maximum is 434,160 bytes.
    Global variables use 31,580 bytes (38%) of dynamic memory, leaving 50,340 bytes for local variables. Maximum is 81,920 bytes.
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    Please HELP!! I’m finding this esp8266 so frustrating…nothing seems to work as it should…

  • Bharat
    Reply

    Dear Mr. Robin ,
    Thanks a lot , We have implemented the project in local wifi successfully .But now we want to implement it on internet. We have internet access through Jio sim on a mobile .Can we do port forwarding in such condition also . Will it involve any additional cost. Please guide us . Thanks

  • sri
    Reply

    the best

  • NIKHIL RAJ
    Reply

    Sketch uses 221,999 bytes (51%) of program storage space. Maximum is 434,160 bytes.
    Global variables use 31,564 bytes (38%) of dynamic memory, leaving 50,356 bytes for local variables. Maximum is 81,920 bytes.
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    • Dave W
      Reply

      Have you selected the correct SUB Port in Arduino IDE?

      • Dave W
        Reply

        Doh.. USB Port..

    • rishi
      Reply

      hello nikhil,

      Did u solve that problem actually i am also facing the same problem if u solved that so please help me out also.

      Thanks

      • Apiwat
        Reply

        I found a solution.
        1. GPIO0 => GND
        2. RST => VCC
        3. Upload
        if can’t upload. pull RST out and jump to VCC again, then upload. (For me, i try 2 or 3 times)
        it worked for me!!

  • Ikya
    Reply

    What about an ESP8266 14 GPIO pins?

  • raul
    Reply

    if the GIPO2 pin is 2, the pin for the GPIO 0 is 0?

  • Mohamed Hegab
    Reply

    It Gives me

    warning: espcomm_sync failed

    I connected the circuit like the diagram

  • Thomas Kanattu
    Reply

    Well done

    • raul
      Reply

      Mohamed
      NOTE:Make sure that GPIO0 is grounded while uploading the code.
      then connect the GPIO0 to ground before upload the sketch

Leave a Comment

− 1 = 1

Start typing and press Enter to search