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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/******************************************************************************** * Function Name : pinMode * Description : Configures the pin as OUTPUT or INPUT * Parameters : @pin - The GPIO Pin No. * @mode - INPUT or OUTPUT * *****************************************************************************/ void ICACHE_FLASH_ATTR pinMode(uint8_t pin, uint8_t mode); /******************************************************************************** * Function Name : digitalWrite * Description : Make GPIO output as HIGH or LOW * Parameters : @pin - The GPIO Pin No. * @val - HIGH or LOW * *****************************************************************************/ void ICACHE_FLASH_ATTR digitalWrite(uint8_t pin, uint8_t val); /******************************************************************************** * Function Name : digitalRead * Description : Read the status of the GPIO pin * Parameters : @pin - The GPIO Pin No. * Return : 1 or 0 * *****************************************************************************/ int ICACHE_FLASH_ATTR digitalRead(uint8_t pin); |
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).

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.

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.
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.
1 |
git clone https://github.com/deeplyembeddedWP/Arduino_Style_GPIO_Library_ESP8266_OpenSDK.git |
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.
1 |
export PATH=/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin/:$PATH |
Now run the esp_flash script present within the Blinky_Test_de project. This script will build the project and flash the firmware onto the ESP8266 using the esptool as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
vinaydivakar@debian:/opt/Espressif/ESP8266_SDK/Blinky_Test_de$ ./esp_flash.sh Initiating the firmware flash process CC user/user_main.c CC user/user_esp8266_digital_Pins.c AR build/app_app.a LD build/app.out FW firmware/ esptool.py v2.1 Creating image for ESP8266... esptool.py v2.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266 Uploading stub... Running stub... Stub running... Configuring flash size... Flash params set to 0x0240 Compressed 29888 bytes to 21656... Wrote 29888 bytes (21656 compressed) at 0x00000000 in 1.9 seconds (effective 124.7 kbit/s)... Hash of data verified. Compressed 164840 bytes to 121826... Wrote 164840 bytes (121826 compressed) at 0x00040000 in 10.8 seconds (effective 122.3 kbit/s)... Hash of data verified. Leaving... Hard resetting... |
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.