Language: EN


How to convert ESP8266 code to ESP32

It’s been almost four years since we started talking about the ESP8266 and ESP32. A lot has happened in the field of these two interesting SoCs during this time.

During this time the ESP8266 has enjoyed great popularity, both in the hobbyist and commercial sectors, and has been integrated into a large number of products due to its low price.

However, the ESP32 has considerably reduced its price. In addition, recently, Espressif has commented on its intention to make the ESP8266 obsolete and replace it with the new ESP32-S3.

So let’s say you want to switch from the ESP8266 to its “big brother” the ESP32. You have plenty of reasons because, as we saw in this post, its features are far superior.

But then what about all your programs, and your code, and your… ! Don’t panic… It is usually very simple to convert a program from the ESP8266 to the ESP32 if you take into account these small tips.

Some may be wondering about the opposite case, converting code from ESP32 to ESP8266. In general, it is not possible except in very simple programs, since the ESP32 has much more power and functionality than the ESP8266.

WiFi Libraries

The names of the libraries change between ESP8266, and ESP32. In the ESP8266 they had the prefix ‘ESP8266’, while in the ESP32 they lack a suffix.

Thus, for example, the ‘ESP8266WiFi.h’ library on the ESP32 is called ‘WiFi.h’, the ‘ESP8266HTTPClient’ library becomes ‘HTTPClient’, and so on with the rest of the libraries.

Actually, this change in the ESP32 is an improvement. Or, in other words, it is a mistake that the developers of the ESP8266 libraries made, and that they corrected when they made the ESP32 libraries. The advantage of not having a prefix is that you can compile the same code for another machine, without having to change any code.

And if you want to make your program compatible with both devices, you can use a precompilation directive like this, with the appropriate library names.

#if defined(ESP8266)
    #define HARDWARE "ESP8266"
    #include "ESP8266WiFi.h"

#elif defined(ESP32)
    #define HARDWARE "ESP32"
    #include "WiFi.h"



There’s not much to say here. The GPIOs between the ESP8266 and the ESP32 are completely different. If you’ve guessed a pin, it’s purely by chance. You’ll have to look at which pins you want to use on the ESP32 and change it in your code.

Luckily for you, the ESP32 has many more pins available than the ESP8266. So, if it worked on the ESP8266, you have more than enough pins on the ESP32. You just have to choose which pins to use.

PWM Function

The functions implemented in the ESP32 do not include “analogWrite”, as the ESP8266 does. This is because the ESP32 has several ways to generate a PWM signal.

If your code on the ESP8266 uses a PWM output, the best thing you can do is look for one of the (many) libraries that define the “analogWrite” function on the ESP32, and you will practically not have to modify the code.

Other Libraries

And the rest of the libraries in the whole world? For reading sensors, for performing actions, for bringing you a coffee in bed? Well, logically, it will depend on the library. There are hundreds, so I can’t tell you in general.

In principle, you have a good chance that it will work directly on the ESP32, but it will depend on the internal functions it uses (timer, registers) and how “portable” the programmers have made it.

You’ll have to try, and if you’re unlucky and it’s not compatible, look for a replacement. But don’t worry, it shouldn’t be difficult. Practically all libraries have a ported version for the ESP8266.

Blog Examples

And all the examples we have in the blog? What about the series of posts about the ESP8266?? Well, the majority simply need to take into account what we’ve mentioned about the names of the libraries.

So we have updated all the blog posts that were possible (except for the specific ones for each processor) to reference both processors. Future posts will follow this criterion.

In addition, we have created a new repository on Github with the codes adapted to the ESP32, so you have the examples for the ESP8266 and for the ESP32 separately.


Version for ESP8266:

Version for ESP32: