ESP32 - Getting started
With the introduction of the ESP8266 low-cost Wi-Fi microchip by Espressif Systems, I would say enabling a WiFi ready solution to any of our projects became really handy. There are so many articles on ESP8266 series in available in the internet already and I would not be talking about this in this post.
We will be discussing about a lot more powerful chip - ESP32, the ESP-WROOM-32, in this article of mine. You can find the datasheet of this chip from here.
The dual radio availability (Bluetooth and WiFi) in ESP32 makes this more popular in the IoT products rather than just in hobbyist projects. And this chip is more capable of handling such traffic too.
This is not another DIY project post using ESP32 or a how to guide using Arduino IDE, there are so many good articles out there, I would recommend the following site as a goto place for ESP32 guide.
Who is this article for?
If you are already familiar with ESP-IDF environment and have managed to build a firmware from scratch using the steps provided from Espressif website. Then this article may not be of much help to you.
If you have ESP32 board and you have got familiar with it using Arduino or if you wanna try getting started by building a embedded firmware from scratch, then this is an article for you.
By the end of the article you should have the following set up and running in your machine,
Toolchain to compile code for ESP32
Build tools - CMake and Ninja to build a full Application for ESP32
ESP-IDF that essentially contains API (software libraries and source code) for ESP32 and scripts to operate the Toolchain
Text editor to write programs (Projects) in C, e.g., Eclipse, git bash
Hello world project for ESP32
ESP-IDF tools installer
The installer includes all the cross compiler needed to build ESP image, cmake and ninja tool. It also downloads one of the ESP-IDF release versions.
Once you download the installer, start executing it. After you agree to the terms and conditions, the very first step would be to choose the python version ( 3.7 and above should be fine).
Next step would be to install Git ( this helps in fetching the github espressif repositories).
After successful installation of Git, we need to download ESP-IDF repository. This contains all the components for building an ESP firmware.
Select the file location you want the ESP-IDF to be installed.
Verify all the file paths and click install. The tool should take care of everything needed.
This creates an “ESP-IDF Command Prompt” shortcut in the Start Menu. This shortcut opens the Command Prompt and sets up all the required environment variables.
Getting used to ESP-IDF console:
Coding with ESP-IDF gets tricky when it comes to development with such command prompt environment.
I would say how I go around with this when I am working on a project, may be there are better productive ways, please feel free to comment.
Being an old school programmer and Linux lover, I have always loved to program via Vim text editor and it has always proved handy for me. But the same might be really hard for others. I would suggest, use any text/code editor environment for writing your code and use the tool to compile and upload.
If you’d like to use the Eclipse IDE instead of running idf.py, check out the Eclipse guide.
Now we are all ready to build our very first ESP32 project based in ESP-IDF, our very first Hello world program.
Copy the example hello world code from our examples directory of esp-idf.
xcopy /e /i %IDF_PATH%\examples\get-started\hello_world hello_world
The above commands will copy the hello world code from examples to the home directory where we can compile.
It is also possible to build examples in-place, without copying them first. But it always best practice that, we have our custom projects from home directory instead of running it from examples.
The hello world code provided basically creates a FreeRTOS task and prtins Hello world repeatedly along with chip info.
To build our very first project we need to execute the following commands.
cd %userprofile%\esp\hello_world idf.py set-target esp32 idf.py menuconfig
set target basically sets our target environment to esp32
menuconfig is the critical command which configures all the components
By this time we need to connect our ESP32 board to our computer to set the serial interface.
If the previous steps have been done correctly, the following menu appears:
This menu is used to set up project specific variables, e.g. Wi-Fi network name and password, the processor speed, etc. Setting up the project with menuconfig may be skipped for “hello_word”. This example will run with default configuration.
For building the project run the following command,
This command will compile the application and all ESP-IDF components, then it will generate the bootloader, partition table, and application binaries. If there are no errors, the build will finish by generating the firmware binary .bin file.
After a successful build you should see the following output:
Flashing the image to the board run the following command,
idf.py -p PORT [-b BAUD] flash
Replace PORT with your ESP32 board’s serial port name
When flashing, you will see the output log similar to the following:
esptool.py --chip esp32 -p COM10 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x10000 hello-world.bin esptool.py v3.0-dev Serial port /dev/ttyUSB0 Connecting........_ Chip is ESP32D0WDQ6 (revision 0) Features: WiFi, BT, Dual Core, Coding Scheme None Crystal is 40MHz MAC: 24:0a:c4:05:b9:14 Uploading stub... Running stub... Stub running... Changing baud rate to 460800 Changed. Configuring flash size... Compressed 3072 bytes to 103... Writing at 0x00008000... (100 %) Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.0 seconds (effective 5962.8 kbit/s)... Hash of data verified. Compressed 26096 bytes to 15408... Writing at 0x00001000... (100 %) Wrote 26096 bytes (15408 compressed) at 0x00001000 in 0.4 seconds (effective 546.7 kbit/s)... Hash of data verified. Compressed 147104 bytes to 77364... Writing at 0x00010000... (20 %) Writing at 0x00014000... (40 %) Writing at 0x00018000... (60 %) Writing at 0x0001c000... (80 %) Writing at 0x00020000... (100 %) Wrote 147104 bytes (77364 compressed) at 0x00010000 in 1.9 seconds (effective 615.5 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin... Done
Monitoring the output
To check if “hello_world” is indeed running, type idf.py -p PORT monitor (Do not forget to replace PORT with your serial port name).
The command launches the IDF monitor application, and following should be the output:
$ idf.py -p COM10 monitor Running idf_monitor in directory [...]/esp/hello_world/build Executing "python [...]/esp-idf/tools/idf_monitor.py -b 115200 [...]/esp/hello_world/build/hello-world.elf"... --- idf_monitor on /dev/ttyUSB0 115200 --- --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- ets rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) ets ...
If you wanna see our printf statements scroll up a little you should see something like the following,
... Hello world! Restarting in 10 seconds... This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 2MB external flash Restarting in 9 seconds... Restarting in 8 seconds... Restarting in 7 seconds...
You can combine building, flashing and monitoring into one step by running: :
idf.py -p PORT flash monitor
So there it is, our very own ESP-IDF firmware build from scratch. This is the base of any project you can work on.
In my upcoming posts we shall discuss building a complete ESP32 firmware from scratch more in detail.