Introduction to the ESP32
The ESP32 is a microcontroller produced by Espressif Systems, it is the successor to the ESP8266. In this tutorial we will look at the anatomy of ESP32 source code and how to build and run a program that blinks the onboard LED and sends a message to your computer via the serial link.
Anatomy of a ESP32 source code file
A complete C++ program file called main.cpp is illustrated below, you can download a link to the actual file here.
// Dr Paul Lunn - Coventry University
// 17/06/2019
// ESP32 Blink test
#include <Arduino.h>
const int SERIAL_0_SPEED = 115200;
const int ON_BOARD_LED = 2;
// The setup() function is called once at the beginning of the program
// and is used to initalise the system
void setup()
{
pinMode(ON_BOARD_LED, OUTPUT); // initialize digital pin ON_BOARD_LED as an output.
Serial.begin(SERIAL_0_SPEED); // begin serial link
Serial.println("ESP32 Blink Test"); // print text onto screen via serial link
}
// The loop() function runs over and over again forever
void loop()
{
digitalWrite(ON_BOARD_LED, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(ON_BOARD_LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
There are several sections in this code which are common to most ESP32 source code files that you will be using, we shall consider each section separately:
Comments
// Dr Paul Lunn - Coventry University
// 17/06/2019
// ESP32 Blink test
A comment is an area of text that follows two backslashes "//". The comment starts after the // and runs until the end of the line. Comments are used to document your code and provide information to the reader. They are ignored by the compiler.
Include
#include <Arduino.h>
The #include command instructs the pre-processor to include libraries and other source code, in this file we want the pre-processor to install the Arduino libraries so that we can utilise functionality provided by this library, such as digitalWrite() and delay().
Constants
const int SERIAL_0_SPEED = 115200;
const int ON_BOARD_LED = 2;
As these constants are declared outside of a function they have global scope to this source file, and are available to any function declared in the file. We declare variables like this, so that we have one place to change a value.
Setup()
void setup()
{
pinMode(ON_BOARD_LED, OUTPUT); // initialize digital pin ON_BOARD_LED as an output.
Serial.begin(SERIAL_0_SPEED); // begin serial link
Serial.println("ESP32 Blink Test"); // print text onto screen via serial link
}
The setup() function is required in any ESP32 main.cpp file. This function executes when your ESP32 board powers up and is used to set up the board. In this example we initialize a serial link between your computer and the ESP32, and we use pinMode() to set our ON_BOARD_LED pin to an output.
Loop()
void loop()
{
digitalWrite(ON_BOARD_LED, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(ON_BOARD_LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
After the setup() function has executed the ESP32 will move on to execute the loop() function. All code inside the function body will be executed continuously whilst it is connected to a power supply. In loop() is where we put the main functionality of our project. In this example the code uses digitalWrite() to turn a LED on and off, we use a delay of 1000 ms to control the rate that the LED turns on and off.
Exercises
1 - build and upload the main.cpp to your ESP32, does the LED blink on and off? 2 - Connect your ESP32 to a serial link, does it output some text when you press the rest button? 3 - Adapt the code in main.cpp to change the frequency that the LED turns on and off