Language: EN

guia-de-programacion-del-esp32-en-entorno-arduino

ESP32 programming guide in Arduino environment

In this article, we will see how to program an *ESP32 with the Arduino environment, and the differences between programming an ESP32 and an Arduino.

Espressif has made a significant effort to port the ESP32 Core to Arduino so that it is very similar to programming a conventional Arduino.

However, the ESP32 has many more options and possibilities than a traditional Arduino. So some things are slightly different.

Timing Functions

Millis and Delay

The timing functions millis() and micros() work the same as in Arduino.

The functions delay() and delayMicroseconds() also work. However, delay is non-blocking, while delayMicroseconds is blocking.

Yielding

Just as with the ESP8266, yielding is one of the most important features and leads to more errors and restarts.

The ESP32 needs to perform tasks related to WiFi connection management and the TCP/IP stack. If it doesn’t attend to them, it will likely restart.

For everything to work, we need to let the ESP32 “breathe”. For that, we have to call the yield function, which allows the ESP32 to take care of its tasks.

Fortunately, the yield function is executed during every delay and at the end of the loop. However, the delayMicroseconds() function does not call yield().

So basically, this means you cannot perform long blocking processes (>100ms) or the ESP32 will restart.

Connections and Hardware

Pin Designation

The ESP32 has many more pins, models, and development boards than the ESP8266. Therefore, the problem of each manufacturer labeling each pin as they wish is much worse.

So in the ESP32, my advice is always to refer to the pins without aliases, simply by the GPIO number.

digitalWrite(14, LOW);  // GPIO14 

And as always, in case of doubt, consult the documentation and the pinout of your board.

Digital Inputs and Outputs (GPIO)

Digital inputs and outputs (GPIO) are programmed practically the same as in any Arduino. So we can change the mode of a GPIO between input or output using the function:

pinMode(pin, mode)

Where the mode can be OUTPUT, INPUT, or INPUT_PULLUP.

Digital Output: Just like in Arduino, if the GPIO is in OUTPUT mode, we can use it as an output by assigning a value (LOW or HIGH).

digitalWrite(pin, output)

Digital Input: If the GPIO is in INPUT mode, we can read its value.

digitalRead(pin)

Analog Outputs (PWM)

Analog outputs (PWM) work quite differently on the ESP32 than on an Arduino or an ESP8266. This is because the ESP32 has several ways to generate a PWM signal.

Analog Inputs (ADC)

The analog inputs of the ESP32 are used similarly to Arduino, with the function:

analogRead(A0)

However, there are some differences compared to an Arduino or an ESP8266. We see it in this entry.

Interrupts

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 RISING, FALLING, HIGH, LOW, CHANGE.

PROGMEM

Just like in the case of the ESP8266, the PROGMEM macro works similarly to Arduino or the ESP8266. The difference is that using the same value twice will store the literal in memory twice.

So, for example:

string text1 = F("hello");
string text2 = F("hello");

If we want to avoid this, we can use the FPSTR function.

const char hello[] PROGMEM = "hello";
string text1 = FSPTR(hello);
string text2 = FSPTR(hello);

Communication

Serial Port

The use of the serial port on 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 on the ESP32, we have several UARTs available and additional functions.

I2C Bus

The use of I2C on the ESP32 is similar to Arduino and uses the same functions. But on the ESP32, we have several I2C buses available and additional functions.

SPI Bus

Again, the SPI on the ESP32 is used similarly and employs the same functions as in a conventional Arduino. But on the ESP32, we have several SPI buses available and additional functions.

WiFi Communication

Just like the ESP8266, the WiFi capability is one of the most important features of the ESP32. Therefore, we will cover it intensively in the rest of the tutorials in the series.

Arduino Libraries

The last question is: will my Arduino library work on the ESP32? Well, just like with the ESP8266, in general, it will not work for you.

If you use a library, it must be compatible with your ESP32. Moreover, it must be compatible with your specific ESP32 model, because an ESP32-S2 is not the same as an ESP32-S3.