What the hell is that?

First of all, let’s explain what is an ESP32 at hardware level. ESP32 is a 32-bit RISC single or dual core SoC. It can run from 80 up to 240Mhz with 320 to 512KB of SRAM. But probably the most important feature of this SoCs are Bluetooth/BLE and WiFi integrated. And of course, very cheap.

In the next table you can see a brief summary of the different flavours:

 

Model Cores Speed RAM GPIOs
ESP32-C3 Single core 160MHz 400KB 22
ESP32-S2 Single core 240MHz 320KB 43
ESP32-S3 Dual core 240MHz 512KB 44

All of them have RTC, SPI, I2C, UART, PWM, 12-bit ADCs and DACs

 

You can check more info about the different models in the manufacturer website.

In the previous summary you can check we didn’t say nothing about ROM. This is because the ESP32 used to be commercialized in a closed module with the ESP32 itself, the mandatory passive electronic components and a SPI flash memory. Almost plug and play.

 

ESP32 module
Basic ESP32 module with it’s own USB converter

 

You can find an infinite number of solutions of the module mounted in development boards. Most of them have a micro-USB interface to power and/or program it. So you just need a USB cable to start to play with it.

ESP32 modules
Different commercial modules with an ESP32

 

What can I do with this stuff

Well, the options are almost infinite. Keep in mind you have a device with a similar compute power like a computer of the latest 90’s or the first 2000’s. You can connect any kind of sensor you can imagine: pressure? yes. Temperature?, why not?. A webcam? of course. A display? yes, you can!.

You can connect it to the Internet and send all that information to any server. Or to your smartphone thru BLuetooth. But you can use it like server role too: you can mount a web server and connect to it.

 

What cannot do with this device

This kind of hardware are often used to find out a solution of a particular problem. Obvioulsy, we must decide for a solution that fit the best way to fix it: related to price, easy to develop it, knowledge of the environment, etc.

Most of the stuff you can do with an ESP32 can be done with any microcontroller like 8, 16 or 32-bit Microchip universe, SoCs from Nordic, Texas Instruments, Silicon Labs, etc. An ESP32 is a choice as good as any others if it fills all the needed for your purpose.

The reasons you can decline for other solutions will be the hardware limitations. According to your needs, any alternatives would be -in a lowest step- an Arduino Uno or, in a highest step, a Raspberry Pi.

Although there are more advantages than disadvantages with the ESP32, I personally would’t use for low consuption applications. It’s true it can decrease the consumption as low as 3uA (in a very deep sleep mode) but the average consuption is not the ideal for a long-term battery solution. Keep in mind that you will need to feed it with more than 100mA in full performance mode. Wifi and Bluetooth are hungry features.

 

Development tools

This module is very common in Arduino-compatible boards. It is probably the best and most simple solution if you need an Arduino Bluetooth/BLE or Wifi board. This means you can use Arduino IDE and all of it’s compatible library IDEs. The great advantage of this IDE is you will find a lot of examples and libaries developed for it.

 

Arduino IDE
Arduino IDE lets you work with an infinite number of boards

 

But in this case, we decided for a not as common configuration for our development configuration. We bet for Visual Studio Code as IDE, PlatformIO as plugin to manage our project files and ESP-idf native libraries. This let us to have full control of our code using pure C/C++ as prgramming language.

If you are still interested, just continue reading.

 

Installing the tools

Visual Studio Code

We will start from the begining: let’s install Visual Studio Code. This great free tool, from Microsoft, works in any common computer (Windows, Linux or MacOS). It is developed in Electron. This is something impressive for me when I discovered, because it works very smooth and it is not a resource-eater as, for example Eclipse or Netbeans (sorry Java).

VSCode Web
You can download Visual Studio Code for any platform

 

You can download from the Visual Studio website.

You can choose between MacOS package, Windows installer and .deb or.rpm for penguin based OS. In my case, .deb. My system is Linux Mint 19

Python

You will need Python version 3.5 or higher to work with ESP32 tools. Of course you can get Python for any platform you use.

Python downloads website
Python downloads website

 

For Windows, you must download the installer and launch it. It’s mandatory to add the Python binaries to the PATH:

 

PlatformIO

PlatformIO is a platform for development of embedded devices. We will use it to develop an ESP32, but it can support a lot of any other devices or boards: PIC32, Nordic and Silicon Labs SoCs, etc.

It has it’s own IDE, but we will use it’s own Visual Studio Code extension. To install it, just go to “Extensions” menu in VSCode, look for “platformio ide” and install it. Other way to install it is just hit F1 (to open the command box) and insert the next command “ext install platformio.platformio-ide”. This order will install the desired extension.

 

VSCode with PlatformIO
PlatformIO home page over Visual Studio Code

 

Creating a new project

The simpliest way to create a new project is to use the Platformio wizard. In the Home page of the environment, you can click on “New Project”. You must indicate the name. In our case: my-first-esp-project. The you will have to select the board you are using. There are a few commercial boards with ESP32. If you have doubts about what board you have, or you are using a privative hardware, the best choice is “Espressif ESP32 Dev Module”. The next and last step is the framework. You can select two options: Arduino or Espressif IoT Development framework. The last one is what we’ll use. You can find a lot of literature about Arduino framework. Finally you can choose the folder where to put the new project: In the default folder or a different location.

The base of any Platformio project is the file “platformio.ini”. You can configure all the features of your project. You can get a lot of information about it in this page [https://docs.platformio.org/en/latest/projectconf/index.html]. This is the file the wizard will generate for us:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf

Here we have some interesting things are worth to comment:

  •  platform = espressif32
    We are indicating the development platform to use in our project. ESP32 will need espressif32 libraries.
    We could indicate too this way: platform = espressif32@2.0.0. It is very interesting because we’ll block the development version, to avoid unexpected changes in future updates.
  • board = esp32dev
    This is useful when we work with commercial boards. We can indicate one of the more common.
  • framework = espidf
    What framework we’ll use. In this case we will use the native libraries from the ESP32 manufacturer. Other alternative would be framework = arduino, but in this case we will explain how to use the natives libaries with it’s own gcc C/C++ compiler.

This options are the basic ones. Now we’ll explain other very interesting for our projects.

  • board_build.partitions = part_single_app_no_OTA.csv
    This parameter indicates a CSV file where it’s indicated the internal memory partitions. It’s a little tricky but very useful if you are thinking in OTA features and/or embedded files.
  • board_build.embed_txtfiles =
    html/index.html
    certs/ota_ca_cert.pem

    Here we can indicate files that will be embedded in the internal memory of our ESP32. We can reference it’s content in our code by this way:
extern const uint8_t _cert_start[] asm("_binary_ota_ca_cert_pem_start");
extern const uint8_t _cert_end[] asm("_binary_ota_ca_cert_pem_end");

The compiler/linker creates start and end memory references for each embededd file, so you can easyly access to it’s content as a read-only data block. Keep in mind the name of the reference if you compare with the original filename:

ota_ca_cert.pem -> _binary_ota_ca_cert_pem_start _binary_ota_ca_cert_pem_end

“Hello world!”

Well, after all that stuff to configure our environment, let’s talk about the more important thing: code!!. By default, our code must be in a folder called src, inside our project folder. This could be changed in, of course, platformio.ini. But I think i’s a good place to save our files.

Our project wizard would had created some structures in our working folder. One of them, the folder “src” and the basic file “main.c”. Let’s change it’s content with this:

#include <stdio.h>

void app_main() {
printf("\r\n\r\nHello world\r\n\r\n");
}

After edit and save the file, go to the PlatformIO menu and select, in the open project “General -> Build”. This will launch the compiler and linker. You will nedd a little of patience in the first execution, because it will need to compile all the libraries (a pair of minutes). If everything go well, we’ll expect something like this:

RAM: [ ] 3.2% (used 10620 bytes from 327680 bytes)
Flash: [= ] 13.9% (used 145430 bytes from 1048576 bytes)

It’s a simple summary of how many resources our brand new program consume.

If we want to test it in a real board, we must connect our ESP32 thru it’s serial port to our computer. Almost all commercial boards use to have a embedded USB-serial converter. If this is not the case you can use a external USB-serial converter to connect to the ESP32 serial pins.

The simpliest way to launch it is from PlatformIO menu. Go to the open project “General -> Upload and Monitor”. In this option, the environment will compile -if it’s necessary-, upload the binary to the ESP32 and launch the app.

If you have a simple serial converter you must enter in boot mode in your board when trigger your flashing order.

When the app is launched you can interact and debug you app from the Visual Studio Console. If nothing go wrong, you must see in it:

Hello world

 

If you have any problem with the upload and/or the console, maybe you have to set the serial connection port or speed. You can set it in your platformio.ini file:

upload_port = /dev/ttyUSB1 # set your serial port to upload the firmware
upload_speed = 115200 # set your serial port speed to upload the firmware
monitor_port = /dev/ttyUSB1 # set your serial port to monitor the app
monitor_speed = 115200 # set your serial port speed to monitor the app

 

ESP32 internal settings

ESP32 and his SDK has a lot of features. To save resources, you can enable or disable it. All of them are indicated in the file CMakeLists.txt. This file is created by the PlatformIO widzard. You can open it an watch what options are enabled or disabled and a lot of any other parameters. You can edit too, but it is no recommended at all. It is recommended to use the SDK configuration tool. It’s called Menuconfig and you can launch it from the console or from the PlatformIO menu, in our open project Platform -> Run Menuconfig.

Menuconfig is opened in the console

 

This configuration file/app is too big to explain here. We could make a full entry just to indicate all of it’s options. Just indicate that if you need to enable or disable the WiFi, Bluetooth, http server, security resources, etc. This is the place where you must enter.

 

Conclusions

This post try to be a brief summary of the ESP32 world: what characteristics it has and what tools you will need to begin to play with it.

We try to escape from the Arduino religion and try to explain how to begin to use it’s own SDK libraries. Probably you can’t find as info or examples as Arduino, but you have the freedom of use pure C/C++ and instantiate directly the manufacturer libraries.

 

Even you can find more entries about Arduino and ESP32, the manufacturer docs and examples are very good. You can begin to explore all the posibilities of this SoC and SDK in their own repository, full of examples.

Espressif has a very good website with a lot of information about this SoC and a forum where you can find a lot of solutions for simple and complex issues.