Arduino Style GPIO Library for ESP8266 OpenSDK

When I came across the GPIO control API’s provided by the OpenSDK and official SDK, I found it to be such a drag. Over time, though I love the ESP8266 OpenSDK, I started getting bugged of juggling with the gpio names, gpio pin numbers, and gpio functions provided by the OpenSDK.

I wanted a method that could simplify the approach to using the gpio’s and would also be compatible with the ESP8266 OpenSDK. As we all know, how easy it is to configure and control gpio’s on the ESP8266 using the Arduino Core Libraries. But unfortunately, those easy to use API’s are present only to be used with the Arduino IDE and is not present in the OpenSDK.

Hence, I decided to port the gpio related functions and its supporting files from the Arduino Core Libraries to the OpenSDK. Once the porting was complete, I built an example project using the new gpio functions and tested it to work well.

The below are the gpio API’s that you can be used along with your OpenSDK. They can be used same as that of Arduino UNO. For more details regarding its usage, please refer the official site of Arduino here.

I have tested this library to work on NodeMCU as well as ESP8266EX Development Boards. For this demonstration, we will use the NodeMCU. We will control the GPIO_4 i.e. D2 on the NodeMCU as shown below in Fig1(Click on Image for larger view).

Fig 1. NodeMCU Pin Mapping

The Fig2. shows the circuit diagram. It is a simple circuit in which the GPIO_4 is connected to a red color LED. Though the LED I am using can handle a current of up to ~20mA, It is always a good practice to connect to the LED via a 330Ω resistor to avoid toasting the LED.

Fig2. Example Circuit Diagram

In this demo, I will configure the GPIO_4 as output using the pinMode() API. And then I will switch ON and OFF the red LED every 100ms using the digitalWrite() API and a timer. If you want to include this library into your project that your building using the ESP8266 OpenSDK, then you need to include the following header(.h) and source(.c) files.

Header and Source files to Include

C

1

2

3

4

5

6

7

/* Include these(.h) files in your include directory */

user_esp8266_peri.h

user_GPIO_Config.h

user_MyConf.h

/* Include this (.c) files in your source directory i.e. user */

user_esp8266_digital_Pins.c

Note: The pin number passed as an argument to these gpio functions always corresponds to the actual GPIO number on the ESP8266. For example, to use GPIO_4, you need to pass the pin number as “4”.

You can download the Blinky_Test_de example project from my Github repository here or clone the project by running the below command.

For this demo, I have used the latest updated SDK i.e. esp_iot_sdk_v1.1.2_15_06_12. However, it should work for the older ones as well. If you haven’t setup the esp8266 toolchain and got started with the ESP8266 OpenSDK, then you can do so by following the instructions described here.

Steps to building and flashing the firmware

After you have cloned or downloaded the example project, please follow the below instructions to build the project and flash the firmware onto you ESP8266.

Append the below line to the end of the .bashrc script to export the path of the ESP8266 toolchain.

The above video shows the demonstration of the build and flashing process when the esp_flash script is run. Once the firmware flashing process has completed, you can see the NodeMCU and the LED in action. I hope this helper library will make it easy for folks to configure and use the GPIO’s on their ESP8266 with the OpenSDK.