Back
Featured image of post How to blink the damn ESP32 built-in led

How to blink the damn ESP32 built-in led

A guided tutorial on how to install EspressIf ESP32 for Arduino and use it to blink the built-in led

Table of Content

Requirements

  • Install Arduino IDE. Visit their site for instructions.
  • Add permission to use the serial connection and operate with it.
  • Make sure your board is recognized properly.

Add permission for serial operations

In case your current user is not added to dialout group, you have to add it.

1
2
sudo adduser $USER dialout
sudo usermod -a -G dialout $USER

Check if ESP32 board is connected to your computer

The fastest way to check whether the board is connected to the computer or not is using lsusb command

1
Bus 002 Device 003: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light

You should see a line similar to previous one. If you dont, check your cable and board.

Finish Arduino IDE installation

After you download Arduino IDE files, you are required to execute install.sh script to complete installation, add shortcut, menu items, etc.

1
./install.sh

If you run install.sh without root permissions, you will see following error message

1
2
3
4
5
6
Adding desktop shortcut, menu item and file associations for Arduino IDE...


ln: failed to create symbolic link '/usr/local/bin/arduino': Permission denied
Adding symlink failed. Hope that's OK. If not then rerun as root with sudo.
 done!

So, you need to execute install.sh script with sudo

1
2
sudo ./install.sh
[sudo] password for sergio:

After that, the success results will be

1
2
3
4
Adding desktop shortcut, menu item and file associations for Arduino IDE...


 done!

Add support for EspressIf ESP 32 boards

To add support to our brand new ESP32 board, we need to download official core files provided by the manufacturer from https://github.com/espressif/arduino-esp32

1
2
mkdir espressif
git clone https://github.com/espressif/arduino-esp32.git esp32

Wait for ESPRESSIF ESP32 framework to be downloaded

1
2
3
4
5
6
7
8
Cloning into 'esp32'...
remote: Enumerating objects: 24105, done.
remote: Counting objects: 100% (808/808), done.
remote: Compressing objects: 100% (577/577), done.
remote: Total 24105 (delta 321), reused 451 (delta 186), pack-reused 23297
Receiving objects: 100% (24105/24105), 610.81 MiB | 19.19 MiB/s, done.
Resolving deltas: 100% (14919/14919), done.
Checking out files: 100% (6178/6178), done.

Now, go to esp32/tools folder to execute script to download required tools. I do have installed Python 3.6 so I call the script using this version.

1
2
cd esp32/tools
python3.6 get.py

After executing the script, I get the success output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
System: Linux, Bits: 64, Info: Linux-5.4.0-73-generic-x86_64-with-Ubuntu-18.04-bionic
Platform: x86_64-pc-linux-gnu
Downloading xtensa-esp32-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ...
Done
Extracting xtensa-esp32-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ...
Downloading xtensa-esp32s2-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ...
Done
Extracting xtensa-esp32s2-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ...
Downloading riscv32-esp-elf-gcc8_4_0-crosstool-ng-1.24.0-123-g64eb9ff-linux-amd64.tar.gz ...
Done
Extracting riscv32-esp-elf-gcc8_4_0-crosstool-ng-1.24.0-123-g64eb9ff-linux-amd64.tar.gz ...
Downloading esptool-3.0.0.3-linux.tar.gz ...
Done
Extracting esptool-3.0.0.3-linux.tar.gz ...
Downloading x86_64-linux-gnu.mklittlefs-c41e51a.200706.tar.gz ...
Done
Extracting x86_64-linux-gnu.mklittlefs-c41e51a.200706.tar.gz ...
Downloading mkspiffs-0.2.3-arduino-esp32-linux64.tar.gz ...
Done
Extracting mkspiffs-0.2.3-arduino-esp32-linux64.tar.gz ...
Renaming mkspiffs-0.2.3-arduino-esp32-linux64 to mkspiffs ...
Platform Tools Installed

After this, is time to restart the arduino IDE and check for right installation and support for ESP32 boards.

Arduino IDE with ESP32 board support
Arduino IDE with ESP32 board support

Pyserial not installed error

1
2
3
4
5
6
7
Pyserial is not installed for /usr/bin/python. Check the README for installation instructions.Traceback (most recent call last):
  File "/home/sergio/Downloads/arduino-1.8.15/hardware/espressif/esp32/tools/esptool/esptool.py", line 38, in <module>
    import serial
ImportError: No module named serial

exit status 1
Error compiling for board ESP32 Dev Module.

The solution to previous error is to install PySerial. We can install easily with pip utility.

1
pip install pyserial

A success result will show

1
2
3
4
5
Collecting pyserial
  Downloading https://files.pythonhosted.org/packages/07/bc/587a445451b253b285629263eb51c2d8e9bcea4fc97826266d186f96f558/pyserial-3.5-py2.py3-none-any.whl (90kB)
    100% |████████████████████████████████| 92kB 1.8MB/s 
Installing collected packages: pyserial
Successfully installed pyserial-3.5

After this, you can compile Arduino sketchs with no problem. The Arduino IDE will report to you sketch final size, for example

1
2
Sketch uses 205592 bytes (15%) of program storage space. Maximum is 1310720 bytes.
Global variables use 11976 bytes (3%) of dynamic memory, leaving 315704 bytes for local variables. Maximum is 327680 bytes.

Flashing a Arduino code to ESP32

To verify the installation and support for ESP32, we will flash an example file. This sketch will make the builtin led to blink.

ESP32 board
ESP32 board

This ESP32 board has a built in LED on pin D9, which we will control using a very simple Arduino program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#define LED_BUILTIN 2
#define DELAY 1000

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(DELAY);
  digitalWrite(LED_BUILTIN, LOW);
  delay(DELAY);
}

In order to successfully complete the code flashing process, you need to press BOOT button of the board while you flash the sketch. The button must be kept hold until you see Writing at ... line.

An example success output should be like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Sketch uses 214148 bytes (16%) of program storage space. Maximum is 1310720 bytes.
Global variables use 12304 bytes (3%) of dynamic memory, leaving 315376 bytes for local variables. Maximum is 327680 bytes.
python /home/sergio/Downloads/arduino-1.8.15/hardware/espressif/esp32/tools/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0xe000 /home/sergio/Downloads/arduino-1.8.15/hardware/espressif/esp32/tools/partitions/boot_app0.bin 0x1000 /tmp/arduino_build_418056/empty.ino.bootloader.bin 0x10000 /tmp/arduino_build_418056/empty.ino.bin 0x8000 /tmp/arduino_build_418056/empty.ino.partitions.bin 
esptool.py v3.1-dev
Serial port /dev/ttyUSB0
Connecting........_____....._
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: OFUSCATED_MAC_HERE
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (effective 703.8 kbit/s)...
Hash of data verified.
Compressed 18576 bytes to 12483...
Writing at 0x00001000... (100 %)
Wrote 18576 bytes (12483 compressed) at 0x00001000 in 1.3 seconds (effective 111.4 kbit/s)...
Hash of data verified.
Compressed 214288 bytes to 120110...
Writing at 0x00010000... (12 %)
Writing at 0x0001c9c3... (25 %)
Writing at 0x00021f78... (37 %)
Writing at 0x000273f9... (50 %)
Writing at 0x0002c783... (62 %)
Writing at 0x00035ed3... (75 %)
Writing at 0x0003cef3... (87 %)
Writing at 0x00042749... (100 %)
Wrote 214288 bytes (120110 compressed) at 0x00010000 in 10.7 seconds (effective 160.0 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.1 seconds (effective 438.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

By default, my ESP32 board each time is reset shows

1
2
3
4
5
6
7
8
21:21:53.893 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
21:21:53.893 -> configsip: 0, SPIWP:0xee
21:21:53.893 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
21:21:53.893 -> mode:DIO, clock div:1
21:21:53.964 -> load:0x3fff0030,len:1412
21:21:53.964 -> load:0x40078000,len:13400
21:21:53.964 -> load:0x40080400,len:3672
21:21:53.964 -> entry 0x400805f8

so beware in case you see similar output.

Verify it works

Verify that you see the onboard LED blinking on and off.

References



💬 Share this post in social media

Thanks for checking this out and I hope you found the info useful! If you have any questions, don't hesitate to write me a comment below. And remember that if you like to see more content on, just let me know it and share this post with your colleges, co-workers, FFF, etc.

You are free to use the content, but mention the author (@MrSergioAnguita) and link the post!
Last updated on Dec 22, 2021 21:16 CET
Please, don't try to hack this website servers. Guess why...