In this article, we are going to see how to program an ESP32 with the Arduino environment, and the differences between programming an ESP32 and an Arduino.
Espressif has made a very important effort to port the ESP32 Core to Arduino to be very similar to the programming of a conventional Arduino.
However, the ESP32 has many more options and possibilities than a traditional Arduino. So some things are somewhat different.
Millis and Delay
The time functions
micros() work the same as in Arduino.
delayMicroseconds() also work. However,
delay is non-blocking, while
delayMicroseconds is blocking.
As with the ESP8266, Yielding is one of the most important features and the cause of more errors and restarts.
The ESP32 needs to perform WiFi connection and TCP/IP stack management tasks. If it doesn’t attend to them, it will probably restart.
For everything to work, we need to let the ESP32 “breathe”. For that, we need to call the
yield function, which allows the ESP32 to attend to its business.
yield function executes in all
delay and at the end of the
loop. However, the
delaymicroseconds() function does not make a call to
So, basically, you cannot perform long blocking processes (>100ms) or the ESP32 will restart.
Connections and hardware
The ESP32 has many more pins, models, and development boards than the ESP8266. Therefore, the problem that each manufacturer labels each pin as they like is much worse.
So in the ESP32 my advice is that you always call the pins without aliases, simply with the GPIO number.
digitalWrite(14, LOW); // GPIO14
And as always, in case of doubt consult the documentation and the pinout of your board.
Digital outputs and inputs (GPIO)
Digital inputs and outputs (GPIO) are programmed almost the same as in any Arduino. So we can change the mode of a GPIO between input or output with the function:
Where the mode can be
Digital output: Like Arduino, if the GPIO is in
OUTPUT mode we can use it as an output by assigning a value (LOW or HIGH).
Digital input: If the GPIO is in
INPUT mode as an input, we can read its value.
Analog outputs (PWM)
The analog outputs (PWM) work quite differently in the ESP32 than in an Arduino or an ESP8266. This is because the ESP32 has several ways of generating a PWM signal.
Analog inputs (ADC)
The analog inputs of the ESP32 are used similar to Arduino, with the function:
However, there are some differences with an Arduino or an ESP8266. We’ll see this in this post.
The ESP32 has interrupts on all GPIO pins. The usage is similar to hardware interrupts in Arduino.
attachInterrupt(digitalPinToInterrupt(interruptPin), handler, FALLING);
The options are
Just like in the case of ES8266, the
PROGMEM macro works similarly to Arduino or the ESP8266. The difference is that using the same value twice will save the literal value twice in memory.
So, for example:
string text1 = F("hello");
string text2 = F("hello");
If we want to avoid this, we can use the
const char hello PROGMEM = "hello";
string text1 = FSPTR(hello);
string text2 = FSPTR(hello);
The use of the serial port in the ESP32 is very similar to its use in Arduino and uses all the functions we are used to for sending and receiving data (read, write, print, println…)
But in the ESP32, we have several UARTs and additional functions available.
The use of I2C in the ESP32 is similar to Arduino and uses the same functions. But in the ESP32, we have several I2C and additional functions available.
Again, the SPI of the ESP32 is used similarly and uses the same functions as in a conventional Arduino. But in the ESP32, we have several SPIs and additional functions available.
Like the ESP8266, WiFi capability is one of the most important points of the ESP32. Therefore, we will see it intensively in the rest of the tutorials in the series.
The last question, will my Arduino library work on the ESP32? Well, just as was the case with the ESP8266, in general, it won’t work.
If you use a library, it will have to be compatible with your ESP32. Furthermore, with your specific model of ESP32, because an ESP32-S2 is not the same as an ESP32-S3.