A few years ago, the ESP8266 revolutionized the embedded IoT world. For less than $3, you could get a programmable, WiFi-enabled microcontroller capable of monitoring and controlling things from anywhere in the world. Building upon this success, Espressif Systems introduced the ESP32, a supercharged upgrade perfect for any Internet of Things (IoT) application, incorporating WiFi and Bluetooth 4.0 (BLE/Bluetooth Smart).
Introducing the ESP32
The ESP32 is a series of microcontrollers designed and manufactured by Espressif Systems. The reasons behind its widespread popularity are numerous:
Dual-core Processor
The ESP32 boasts a Tensilica Xtensa® Dual-Core 32-bit LX6 microprocessor. It operates at a clock frequency adjustable from 80 to 240 MHz and delivers performance up to 600 DMIPS (Dhrystone Million Instructions Per Second). This processing power allows for complex computations and real-time tasks.
Alt text: Power icon representing the powerful processing capabilities of the ESP32 microcontroller.
Ultra-Low Power Consumption
Featuring dynamic power scaling and various power modes, the ESP32 excels in ultra-low power consumption. This makes it ideal for wearable electronics, mobile devices, and other IoT applications where battery life is crucial. Understanding ESP32’s sleep modes and power management is key to maximizing battery life.
Wi-Fi Capabilities
The ESP32 incorporates an 802.11b/g/n HT40 Wi-Fi transceiver. This allows it to connect to existing Wi-Fi networks (Station mode) for internet access or create its own Wi-Fi network (Soft access point mode), enabling other devices to connect to it. This makes it suitable for various wireless communication applications.
Alt text: Bluetooth icon symbolizing the ESP32’s Bluetooth connectivity, ideal for short-range wireless communication.
Dual-mode Bluetooth Capabilities
The ESP32 supports Bluetooth 4.0 (BLE/Bluetooth Smart) and Bluetooth Classic (BT). This dual-mode capability makes it highly versatile for various IoT applications needing short-range wireless communication. BLE is perfect for low-power applications like wearables, while Bluetooth Classic is suitable for audio streaming.
Rich Set of Peripherals
The ESP32 has a wide range of integrated peripherals, including capacitive touch sensors, Analog-to-Digital Converters (ADCs), Digital-to-Analog Converters (DACs), UART, SPI, I2C, PWM, and more. These peripherals enable the ESP32 to interface with a variety of sensors and actuators, making it suitable for a broad spectrum of applications.
Alt text: Icon representing the multiple development platforms available for ESP32 programming.
Many Development Platforms
Several development platforms are available for programming the ESP32, making it accessible to developers of varying skill levels. These include the Arduino IDE, MicroPython, Espruino, and the Espressif SDK. You can also find other options listed on WikiPedia. This flexibility allows developers to choose the tools that best suit their preferences and project requirements.
Robust Design
The ESP32 is designed to operate reliably in industrial environments. It can withstand operating temperatures ranging from -40°C to +125°C, ensuring dependable performance in harsh conditions.
Alt text: Cost icon highlighting the ESP32’s affordability, making it accessible for hobbyists and professionals.
Low Cost
With prices starting around $6, the ESP32 is an affordable microcontroller. This makes it accessible to hobbyists, students, and professionals alike, fostering innovation and experimentation in the IoT space.
ESP32 Development Boards
To start working with the ESP32, you’ll need a development board. While many boards offer similar core functionality, some are better suited for specific projects.
Here are a few popular ESP32 development boards:
ESP32 DevKit V1
This is an excellent board for beginners due to its popularity and ease of use. The ESP32 DevKit V1 has everything needed to get started with ESP32 development. It’s a versatile and well-supported option.
ESP32 OLED Kit
The ESP32 OLED Kit includes an OLED display, making it suitable for IoT applications requiring visual output. It also features a u.FL connector for an external antenna and a Lipo battery connector for creating battery-powered projects.
Alt text: A close-up shot of an ESP32 OLED board, showcasing the integrated OLED display and various connection ports.
ESP32-CAM
The ESP32-CAM board features a built-in OV2640 2MP camera and a microSD card slot, ideal for IoT projects that require image capture and processing. Its camera capabilities enable advanced features like image tracking and recognition.
ESP32 SIM800L TTGO T-Call
The ESP32 SIM800L TTGO T-Call board allows you to connect to the internet via a SIM card data plan, eliminating the need for Wi-Fi. This makes it suitable for remote applications where Wi-Fi is unavailable.
Alt text: The ESP32 SIM800L TTGO T-Call board, demonstrating its SIM card slot and antenna connection for cellular communication.
M5Stack
M5Stack is a modular product development toolkit based on the ESP32. The M5 ecosystem includes a main controller and stackable modules (similar to Arduino shields) to expand project functionality.
Alt text: A M5Stack ESP32 development kit, showing the stackable modular design for expandable functionality.
ESP32 DevKit V1 Board Hardware Overview
The ESP32 DevKit v1 is the most widely used ESP32 development board. This section provides a detailed look at its hardware components.
ESP-WROOM-32 Module
The ESP32 DevKit v1 board is equipped with the ESP-WROOM-32 module, which contains the Tensilica Xtensa® Dual-Core 32-bit LX6 microprocessor. This processor has two independently controlled CPU cores, operates at an adjustable clock frequency of 80 to 240 MHz, and performs at up to 600 DMIPS.
ESP-WROOM-32 Chip Features:
- Xtensa® Dual-Core 32-bit LX6
- Up to 240MHz Clock Frequency
- 520kB internal SRAM
- 4MB external flash
- 802.11b/g/n Wi-Fi transceiver
- Bluetooth 4.2/BLE
Alt text: Detailed diagram of the ESP-WROOM-32 chip, highlighting its key components and specifications.
The module also includes 448 KB of ROM, 520 KB of SRAM, and 4 MB of Flash memory for program and data storage.
The ESP32 integrates an 802.11b/g/n HT40 Wi-Fi transceiver, allowing it to connect to a Wi-Fi network for internet access (Station mode) or create its own Wi-Fi network (Soft access point mode).
The ESP32 also supports WiFi Direct for peer-to-peer connections, which is simpler to set up and offers faster data transfer speeds than Bluetooth.
The chip supports both Bluetooth 4.0 (BLE/Bluetooth Smart) and Bluetooth Classic (BT).
Power
The ESP32 operates within a voltage range of 2.2V to 3.6V. The board includes an LDO voltage regulator to maintain a stable 3.3V output, providing up to 600 mA, sufficient for the ESP32’s maximum current draw during RF transmissions.
Power Requirements:
- Operating Voltage: 2.2V to 3.6V
- On-board 3.3V 600mA regulator
- 5 µA during Sleep Mode
- 250mA during RF transmissions
Alt text: Illustration of the ESP32’s power supply circuit, including the voltage regulator and key voltage levels.
The 3.3V regulator’s output is accessible via the 3V3 header pin, which can power external circuitry.
The ESP32 development board is typically powered through the on-board MicroB USB connector. Alternatively, a regulated 5V power supply can be connected to the VIN pin to power the ESP32 and its peripherals.
Peripherals and I/O
The ESP32 has 48 GPIO pins, but only 25 are accessible via pin headers. These pins can be configured for various peripheral functions, including:
- 15 ADC channels: 15 channels of 12-bit SAR ADC with selectable ranges of 0-1V, 0-1.4V, 0-2V, or 0-4V
- 2 UART interfaces: 2 UART interfaces with flow control and IrDA support
- 25 PWM outputs: 25 PWM pins to control things like motor speed or LED brightness
- 2 DAC channels: Two 8-bit DACs to generate true analog voltages
- SPI, I2C & I2S interface: Three SPI and one I2C interfaces for connecting various sensors and peripherals, as well as two I2S interfaces for adding sound to your project.
- 9 Touch Pads: 9 GPIOs with capacitive touch sensing
Multiplexed I/Os:
- 15 ADC channels
- 2 UART interfaces
- 25 PWM outputs
- 2 DAC channels
- SPI, I2C & I2S interface
- 9 Touch Pads
Alt text: Diagram showing the multiplexing capabilities of the ESP32 GPIO pins, allowing multiple functions per pin.
The ESP32’s pin multiplexing feature allows multiple peripherals to share a single GPIO pin.
Input Only GPIOs
Pins GPIO34, GPIO35, GPIO36(VP) and GPIO39(VN) cannot be configured as outputs and lack internal pull-up and pull-down resistors.
On-board Switches & LED Indicators
The ESP32 development board has two buttons:
- Reset (EN): Resets the ESP32 chip.
- Boot: Used to download new software or programs.
Switches & Indicators:
- EN – Reset the ESP32 chip
- Boot – Download new programs
- Red LED – Power Indicator
- Blue LED – User Programmable
Alt text: Close-up view of the ESP32 development board, highlighting the reset and boot buttons, as well as the LED indicators.
The board also includes two LED indicators:
- Red LED: Indicates the board is powered on.
- Blue LED: User-programmable and connected to the board’s D2 pin.
Serial Communication
The board includes Silicon Labs‘ CP2102 USB-to-UART Bridge Controller, which converts USB signals to serial for programming the ESP32 chip.
Serial Communication:
- CP2102 USB-to-UART converter
- 5 Mbps communication speed
- IrDA support
Alt text: Block diagram of the CP2102 USB-to-UART converter, illustrating its role in serial communication.
ESP32 Development Board Pinout
The ESP32 DevKit V1 development board has 30 pins in total. For convenience, pins with similar functionality are grouped together. The pinout is as follows:
Alt text: Detailed ESP32 pinout diagram with labeled GPIO pins and their corresponding functions.
Power Pins: VIN (regulated 5V power supply), 3V3 (output from the on-board voltage regulator, up to 600 mA), and GND (ground).
GPIO Pins: The ESP32 development board has 25 GPIO pins that can be assigned different functions by programming the appropriate registers. There are several kinds of GPIOs: digital-only, analog-enabled, capacitive-touch-enabled, etc. All GPIOs can be configured as interrupts.
ADC Channels: ESP32 integrates two 12-bit SAR ADCs and supports measurements on 15 channels (analog-enabled pins). Some of these pins can be used to build a programmable gain amplifier for measuring small analog signals.
DAC Channels: The ESP32 includes two 8-bit DAC channels for converting digital signals to true analog voltages.
Touch Pads: The board has 9 capacitive touch-sensing GPIOs.
SPI Pins: ESP32 has three SPIs (SPI, HSPI, and VSPI) in slave and master modes. Only VSPI and HSPI are usable SPI interfaces, and the third SPI bus is used by the integrated flash memory chip. VSPI pins are commonly used in standard libraries.
I2C Pins: The ESP32 has a single I2C bus. The SDA and SCL pins are, by default, assigned to GPIO21 and GPIO22, respectively. However, you can bit-bang the I2C protocol on any GPIO pin with wire.begin(SDA, SCL)
command.
UART Pins: The ESP32 dev. board has two UART interfaces, UART0 and UART2, that support asynchronous communication (RS232 and RS485) and IrDA at up to 5 Mbps.
PWM Pins: The board has 25 channels (nearly all GPIO pins) of PWM pins controlled by a PWM controller.
EN Pin: Used to enable the ESP32. When pulled HIGH, the chip is enabled; when pulled LOW, the chip operates at low power.
For more information, refer to our comprehensive ESP32 pinout guide. This guide also explains which ESP32 GPIO pins are safe to use and which pins should be used with caution.
[ESP32 Pinout Reference
One of the advantages of the ESP32 is that it has a lot more GPIOs than the ESP8266. You won’t have to juggle or multiplex…](https://lastminuteengineers.com/esp32-pinout-reference/)
ESP32 Development Platforms
Several development platforms are available for programming the ESP32.
You can go with Espruino (JavaScript SDK and firmware closely emulating Node.js), Mongoose OS (an operating system for IoT devices that is recommended by Espressif Systems and Google Cloud IoT), MicroPython (implementation of Python 3 for microcontrollers), an SDK provided by Espressif, or one of the platforms listed on WiKiPedia.
Fortunately, the amazing ESP32 community recently took the IDE selection a step further by creating an Arduino add-on. If you’re just getting started with the ESP32, we recommend starting with this environment, which we’ll cover in this tutorial. For more information, visit the ESP32 Arduino GitHub repository.
Installing the ESP32 Arduino Core
To install the ESP32 core in the Arduino IDE, ensure you have the latest version of the Arduino IDE (Arduino 1.8.5 or higher) installed on your computer.
Then, launch the Arduino IDE and navigate to File > Preferences.
Alt text: Screenshot of the Arduino IDE Preferences window, showing the “Additional Boards Manager URLs” field.
Fill in the “Additional Board Manager URLs” field with the following. This will include support for both ESP32 and ESP8266 boards.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then, click the “OK” button.
Alt text: The “Additional Boards Manager URLs” field in the Arduino IDE Preferences filled with the ESP32 and ESP8266 JSON URLs.
Now navigate to Tools > Board > Boards Manager…
Alt text: Screenshot of the Arduino IDE Boards Manager window.
Filter your search by entering ‘esp32‘. Look for ESP32 by Espressif Systems. Click on that entry, and then choose Install.
Alt text: The ESP32 package by Espressif Systems in the Arduino IDE Boards Manager, ready for installation.
After installing, restart your Arduino IDE and navigate to Tools > Board to ensure you have ESP32 boards available.
Alt text: Arduino IDE Boards Manager showing the installed ESP32 boards.
Now select your board in the Tools > Board menu (in our case, it’s the DOIT ESP32 DEVKIT V1). If you are unsure which board you have, select ESP32 Dev Module.
Alt text: A list of ESP32 boards available in the Arduino IDE under the Tools > Board menu.
Finally, connect the ESP32 board to your computer and select the Port (if the COM port does not appear in your Arduino IDE, you will need to install the CP210x USB to UART Bridge VCP Drivers):
Alt text: Selection of the COM port for the connected ESP32 board in the Arduino IDE.
That’s it! You can now begin writing code for your ESP32 in the Arduino IDE.
You should make sure you always have the most recent version of the ESP32 Arduino core installed.
Simply navigate to Tools > Board > Boards Manager, search for ESP32, and verify the version you have installed. If a newer version is available, you should install it.
ESP32 Example Sketches
The ESP32 Arduino core includes several example sketches. To access the example sketches, navigate to File > Examples > ESP32.
You will see a selection of example sketches. You can choose any of them to load the sketch into your IDE and start experimenting with it.
Alt text: ESP32 example sketches listed under File > Examples > ESP32 in the Arduino IDE.
Basic Example: Blink
To ensure that everything is properly configured, we’ll upload the most basic sketch of all – Blink!
This test uses the on-board LED. As previously mentioned in this tutorial, the D2 pin of the board is connected to the on-board Blue LED and is user programmable.
int ledPin = 2;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
When the code is uploaded, the LED will begin to blink. To get your ESP32 to run the sketch, you may need to press the EN button.
Alt text: Animated GIF showing the blue LED blinking on the ESP32 Development Board after uploading the Blink sketch.
Troubleshooting – Booting up ESP32
Some ESP32 boards enter flashing mode automatically, and the code is uploaded successfully, while others do not, and you may receive the “A fatal error occurred: Failed to connect to ESP32… Timed out waiting for packet header…” error:
This is a common issue, and it indicates that your ESP32 is not in flashing or uploading mode. You can resolve this issue by following the steps outlined below.
- Hold down the BOOT button on your ESP32 board.
- Press the Upload button in the Arduino IDE to upload a new sketch.
- Release the BOOT button when the Writing at 0x00001000… (100%) message appears in the Arduino IDE log after Connecting….
- You should now see the “Done uploading” message.
That’s it. Now, press the EN button to restart the ESP32 and run the newly uploaded sketch.
Remember! You’ll have to repeat that button sequence every time you want to upload a new sketch.