ESP8266 FreeRTOS SDK – blinking LED example

posted in: Internet of Things (IoT) | 23

The ESP8266 is an extremely versatile chip that can do a whole lot more than just sit on an ESP-01 module! The simple ESP8266 Blinking LED project was one of my first goals when getting started with developing with this chip because it assures you that you can compile a dirt simple program – which is quite a feat. I used the ESP8266 FreeRTOS SDK, the official version provided by Espressif Systems. I will add further details to the post as we continue into this.

If you need help compiling programs for the ESP8266 using the FreeRTOS SDK, let me know in a mail or comment and I would write an article on that.

Getting the FreeRTOS based SDK template to compile is quite a challenge for those who have not worked with cross compilers before. I will probably cover setting that up in another article. For now, let’s take a look at what goes into blinking an LED using the ESP8266. The hardware used is the ESP-LAUNCHER, again straight from Espressif Systems. I will switch to a breadboard mounted ESP-12 module soon when i receive my package. 🙂

In FreeRTOS, to blink an LED, you need a task that can toggle the state of a GPIO port periodically. If you are not familiar with RTOS tasks or threads, you should take a look at the FreeRTOS official website, where you can find plenty examples or guidelines to write your programs with FreeRTOS. I recommend getting yourself familiarized with the FreeRTOS before starting off with using it with ESP8266.

This program I wrote works in the following manner:

  • The ESP8266 starts execution of user_init () after booting up and completing some internal tasks.
  • In user_init (), we create a task that can blink an LED. Before creating the task, we declare the target pin as GPIO.
  • The task now created is executed in its time slice as decided by the FreeRTOS scheduler.

So what does the blink function do?
The blink function simply toggles the state of an LED. It is of the form:
{toggle GPIO, delay, toggle GPIO, delay}
And the stuff inside those curly brackets just keeps on repeating forever. Note that the tasks in FreeRTOS must never return. If they do, they must delete themselves. But this is not a concern here as we need an infinitely looping task anyway.

The source code from the program (user_main.c) is shown here. You may copy that or just download the ESP8266 blinking LED source files from here:

Download Code (user_main.c)



23 Responses

  1. can you include your esp_common.h file as well? thank you!

    • It is the same file, unmodified, as in the FreeRTOS SDK v.1.4.0
      If you are getting errors on missing definitions, you should put the files gpio.h and gpio.c in the same folder as user_main.c
      You may email me if you still face issues, I’d be able to respond faster. 🙂

  2. How to compile and upload the code on ESP 8266?

    • You will need to compile using the environment provided by Espressif and upload using the flash download tool. You may try to search the process 🙂

  3. vkthakar

    hi pratik,
    which RTOS lib you used?

    i use 1.4.0.
    but when i make this code then gpio state not change.

    and yes, i have problem that when i use boot loader inducing in 1.4.0 SDK then device not boot up but if i use boot loader of 1.2.0 SDK then device runs correctly.
    UART and wifi related functionality works correctly.

  4. animesh sachan

    can you/anybody tell me the steps to compile RTOS sdk based program and how to upload the program to the ESP8266? I am very new and familiar with ESP8266 programming in arduino IDE only.

  5. Milton Morazan

    Pratik Hi, I’m new to the ESP8266. I want to use the device as a remote control and at the same time serve as a webserver.

    as I install your library?

    Milton Morazan.

    • That should be possible with a bit of coding. Let me know more about your project, and if it is a personal project, I can come up with articles that show how to do what you want to do! 🙂

  6. Hi,
    I want to use ESP12E for taking some sensors data.
    For that i want to use freeRTOS sdk. So which IDE I require to write and compile the code into ESP12E.


  7. Hi,
    i am able to compile and load non OS sdk which are downloaded from websites, but i don’t know what are the steps to take care for programming rtos sdk in eclipse.even i didn’t found any article regarding on that.can someone please give an article in detail………

    • That definitely sounds like a good idea. I will research this over this week and will let you know if I write an article related to this. Would you prefer Windows or Linux?

      • l prefer linux to work,can i have some detail information about programming of

  8. Chris Sparks

    You may want to add that you need to add “-ldriver” to the Makefile. Assuming one has build the driver subdirectory in the SDK.

  9. Chris Sparks

    I tried this with my NODEMCU and I get no blinking. The pins mappings are different I think so does anyone have a clue to fix this?

  10. Hi pratik,
    i got a issue while uploading the ESP_RTOS_SDK, open_ssl_demo example to esp8266-01. i able to compile with crosstool-ng and getting two bin files (eagle.flash.bin——–>0x00000,eagle.irom0text.bin—->0x40000)
    ,even iam able to upload those files by using flashdownload(flashmode in esp8266),but module is not responding when i make it in run mode.should we follow any instruction in case of RTOS

    • You sure that you need to load the file at 0x40000?
      And also, make sure the compiled BIN is small enough to fit on ESP-01 module (which has 4MBit flash).

  11. Narendhar

    can you give more details about how to use esp8266 rtos sdk

    • Narendhar

      iam using linux environment

    • Can write some tutorials on that, what information do you want? Can you provide me a list of articles that you want to have?

  12. Hi pratik,

    I have tried rtos sdk sample code compilation and flashed the firmware to module but not booting and also not getting boot messages in serial console. What might be the problem?

    • Have you put the ESP8266 in flash mode? Check if power is sufficient and serial connection is working well.

Leave a Reply