Lab 5
Lab 5
⮚ To familiarize participants with the ESP32 hardware and software setup, including board components and
software installation.
⮚ To teach basic ESP32 programming and GPIO control, enabling participants to blink LEDs and read digital
inputs.
Learn about Arduino and the Arduino UNO and how you can integrate this board into your makerspace and coding program. Make interactive makerspace projects while learning to code and problem solve.
1
A brief introduction to ESP32 and OTA Lab 5
ESP 32:
The ESP32 is a series of low-cost and low-power System on a Chip (SoC) microcontrollers developed by
Espressif that include Wi-Fi and Bluetooth wireless capabilities and dual-core processor. If you’re familiar
with the ESP8266, the ESP32 is its successor, loaded with lots of new features.
ESP32 Specifications
If you want to get a bit more technical and specific, you can take a look at the following detailed specifications of the
ESP32 (source: http://esp32.net/)
2
A brief introduction to ESP32 and OTA Lab 5
3
A brief introduction to ESP32 and OTA Lab 5
● Processor: Tensilica Xtensa Dual-Core 32-bit LX6 microprocessor, running at 160 or 240 MHz
● Memory:
● RTC fast SRAM: 8 KB (for data storage and main CPU during RTC Boot from the deep-sleep
mode)
● RTC slow SRAM: 8KB (for co-processor accessing during deep-sleep mode)
● eFuse: 1 Kbit (of which 256 bits are used for the system (MAC address and chip configuration)
and the remaining 768 bits are reserved for customer applications, including Flash-Encryption
and Chip-ID)
● Embedded flash: flash connected internally via IO16, IO17, SD_CMD, SD_CLK, SD_DATA_0
and SD_DATA_1 on ESP32-D2WD and ESP32-PICO-D4.
● 0 MiB (ESP32-D0WDQ6, ESP32-D0WD, and ESP32-S0WD chips)
● Low Power: ensures that you can still use ADC conversions, for example, during deep sleep.
● Peripheral Input/Output:
4
A brief introduction to ESP32 and OTA Lab 5
What are the main differences between ESP32 and ESP8266 boards?
The ESP32 adds an extra CPU core, faster Wi-Fi, more GPIOs, and supports Bluetooth 4.2 and Bluetooth low energy.
Additionally, the ESP32 comes with touch-sensitive pins that can be used to wake up the ESP32 from deep sleep, and
built-in hall effect sensor.
In summary:
● The ESP32 supports analog measurements on 18 channels (analog-enabled pins) versus just one 10-bit ADC
pin on the ESP8266;
● The ESP32 supports Bluetooth while the ESP8266 doesn’t;
5
A brief introduction to ESP32 and OTA Lab 5
● The ESP32 is dual-core (most models), and the ESP8266 is single core;
These development boards come with all the needed circuitry to power and program the chip, connect it to your
computer, pins to connect peripherals, built-in power and control LEDs, an antenna for wi-fi signal, and other useful
features. Others even come with extra hardware like specific sensors or modules, displays, or a camera in the case of
the ESP32-CAM.
6
A brief introduction to ESP32 and OTA Lab 5
● USB-to-UART interface and voltage regulator circuit. Most full-featured development boards have
these two features. This is important to easily connect the ESP32 to your computer to upload code
and apply power.
● BOOT and RESET/EN buttons to put the board in flashing mode or reset (restart) the board. Some
boards don’t have the BOOT button. Usually, these boards go into flashing mode automatically.
● Pin configuration and the number of pins. To properly use the ESP32 in your projects, you need to
have access to the board pinout (like a map that shows which pin corresponds to which GPIO and its
features). So make sure you have access to the pinout of the board you’re getting. Otherwise, you
may end up using the ESP32 incorrectly.
● Antenna connector. Most boards come with an onboard antenna for Wi-Fi signal. Some boards come
with an antenna connector to optionally connect an external antenna. Adding an external antenna
increases your Wi-Fi range.
● Battery connector. If you want to power your ESP32 using batteries, there are development boards
that come with connectors for LiPo batteries—this can be handier. You can also power a “regular”
ESP32 with batteries through the power pins.
● Extra hardware features. There are ESP32 development boards with extra hardware features. For
example, some may come with a built-in OLED display, a LoRa module, a SIM800 module (for
GSM and GPRS), a battery holder, a camera, or others.
In most of our ESP32 projects, we use the ESP32 DEVKIT DOIT board, and that’s the one we recommend for
beginners. There are different versions of this board with a different number of available pins (30, 36, and 38)—all
boards work in a similar way.
7
A brief introduction to ESP32 and OTA Lab 5
8
A brief introduction to ESP32 and OTA Lab 5
It comes with a microUSB interface that you can use to connect the board to your computer to upload code or apply
power.
It uses the CP2102 chip (USB to UART) to communicate with your computer via a COM port using a serial interface.
Another popular chip is the CH340. Check what’s the USB to UART chip converter on your board because you’ll
need to install the required drivers so that your computer can communicate with the board (more information about
this later in this guide).
This board also comes with a RESET button (may be labeled EN) to restart the board and a BOOT button to put the
board in flashing mode (available to receive code). Note that some boards may not have a BOOT button.
It also comes with a built-in blue LED that is internally connected to GPIO 2. This LED is useful for debugging to
give some sort of visual physical output. There’s also a red LED that lights up when you provide power to the board.
9
A brief introduction to ESP32 and OTA Lab 5
Power Pins
Usually, all boards come with power pins: 3V3, GND, and VIN. You can use these pins to power the board (if you’re
not providing power through the USB port), or to get power for other peripherals (if you’re powering the board using
the USB port).
With the ESP32 you can decide which pins are UART, I2C, or SPI – you just need to set that on the code. This is
possible due to the ESP32 chip’s multiplexing feature that allows to assign multiple functions to the same pin.
If you don’t set them on the code, the pins will be configured by default as shown in the figure below (the pin location
can change depending on the manufacturer). Additionally, there are pins with specific features that make them suitable
or not for a particular project.
10
A brief introduction to ESP32 and OTA Lab 5
We have a detailed guide dedicated to the ESP32 GPIOs that we recommend you read: ESP32 Pinout Reference
Guide. It shows how to use the ESP32 GPIOs and explains what are the best GPIOs to use depending on your project.
The placement of the GPIOs might be different depending on your board model. However, usually, each specific
GPIO works in the same way regardless of the development board you’re using (with some exceptions). For example,
regardless of the board, usually GPIO5 is always the VSPI CS0 pin, GPIO 23 always corresponds to VSPI MOSI for
SPI communication, etc.
Our preferred method to program the ESP32 is with C/C++ “Arduino programming language”. We also have some
guides and tutorials using MicroPython firmware.
To program your boards, you need an IDE to write your code. For beginners, we recommend using Arduino IDE.
While it’s not the best IDE, it works well and is simple and intuitive to use for beginners. After getting familiar with
Arduino IDE and you start creating more complex projects, you may find it useful to use VS Code with the Platformio
extension instead.
11
A brief introduction to ESP32 and OTA Lab 5
If you’re just getting started with the ESP32, start with Arduino IDE. At the time of writing this tutorial, we
recommend using the legacy version (1.8.19) with the ESP32. While version 2 works well with Arduino, there are
still some bugs and some features that are not supported yet for the ESP32.
Enter the following into the “Additional Board Manager URLs” field. This will add support for ESP32 and ESP8266
boards as well.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json,
http://arduino.esp8266.com/stable/package_esp8266com_index.json
13
A brief introduction to ESP32 and OTA Lab 5
Then, go to Tools > Board and check that you have ESP32 boards available.
14
A brief introduction to ESP32 and OTA Lab 5
Now, you’re ready to start programming your ESP32 using Arduino IDE.
ESP32 Examples
In your Arduino IDE, you can find multiple examples for the ESP32. First, make sure you have an ESP32 board
selected in Tools > Board. Then, simply go to File > Examples and check out the examples under the ESP32 section.
15
A brief introduction to ESP32 and OTA Lab 5
16
A brief introduction to ESP32 and OTA Lab 5
You just need to go to Tools > Board > Boards Manager, search for ESP32, and check the version that you have
installed. If there is a more recent version available, select that version to install it.
First, make sure you have an ESP32 selected in Tools > Board. Then, go to File > Examples > WiFi > WiFiScan.
This will load a sketch that scans Wi-Fi networks within the range of your ESP32 board.
Connect your ESP32 development board to your computer using a USB cable. If you have an ESP32 DEVKIT DOIT
board, the built-in red LED will turn on. This indicates the board is receiving power.
17
A brief introduction to ESP32 and OTA Lab 5
Important: you must use a USB cable with data wires. Some USB cables from
chargers or power banks are power only and they don’t transfer data—these won’t
work.
Now, follow the next steps to upload the code.
1) Go to Tools > Board, scroll down to the ESP32 section and select the name of your ESP32 board. In my case, it’s
the DOIT ESP32 DEVKIT V1 board.
2) Go to Tools > Port and select a COM port available. If the COM port is grayed out, this means you don’t have the
required USB drivers. Check the section Installing USB Drivers before proceeding.
18
A brief introduction to ESP32 and OTA Lab 5
Some boards will automatically go into flashing mode and the code will be successfully uploaded straight away.
Other boards don’t go into flashing mode automatically, so you may end up getting the following error.
Or something like:
This means the ESP32 was not in flashing mode when you tried to upload the code. In this situation, you should long
press the board BOOT button, when you start seeing the “Connecting….” message on the debugging window.
Note: in some boards, a simple trick can make the ESP32 go into flashing mode automatically. Check it out on the
following tutorial: [SOLVED] Failed to connect to ESP32: Timed out waiting for packet header.
Now, the code should be successfully uploaded to the board. You should get a “Done uploading “message”.
Demonstration
To see if the code is working as expected, open the Serial Monitor at a baud rate of 115200.
Press the ESP32 RST or EN button to restart the board and start running the newly uploaded code.
19
A brief introduction to ESP32 and OTA Lab 5
Most ESP32 boards either use the CP2101 or CH340 drivers. Check the USB to UART converter on your board, and
install the corresponding drivers.
You’ll easily find instructions with a quick google search. For example “install CP2101 drivers Windows”.
One of the best things about ESP32 is that its firmware can be updated wirelessly. This kind of programming is called
“Over-The-Air” (OTA).
20
A brief introduction to ESP32 and OTA Lab 5
The OTA functionality comes in handy when there is no physical access to the ESP module. In addition, it reduces the
time required to update each ESP module during maintenance.
One key advantage of OTA is that a single central location can send an update to multiple ESPs on the same network.
The only disadvantage is that you must include an OTA code with each sketch you upload in order to use OTA in the
next update.
Each one has its own benefits, so you can use whichever one works best for your project.
This tutorial will walk you through the process of implementing Basic OTA.
The ESP32 add-on for the Arduino IDE includes an OTA library as well as a BasicOTA example. Simply navigate to
File > Examples > ArduinoOTA > BasicOTA.
21
A brief introduction to ESP32 and OTA Lab 5
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
22
A brief introduction to ESP32 and OTA Lab 5
// No authentication by default
// ArduinoOTA.setPassword("admin");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using
SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
23
A brief introduction to ESP32 and OTA Lab 5
void loop() {
ArduinoOTA.handle();
}
Now, open the Serial Monitor at 115200 baud rate and press the EN button on the ESP32. If everything is fine, you
should see the dynamic IP address assigned by your router. Make a note of it.
Remember that you must include the OTA code in every sketch you upload. Otherwise, you will lose OTA capability
and will be unable to perform the next over-the-air upload. Therefore, it is recommended that you modify the
preceding code to include your new code.
As an example, we will include a simple Blink sketch in the Basic OTA code. Remember to modify the SSID and
password variables with your network credentials.
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
pinMode(led, OUTPUT);
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// No authentication by default
// ArduinoOTA.setPassword("admin");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using
SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
25
A brief introduction to ESP32 and OTA Lab 5
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
ArduinoOTA.handle();
//loop to blink without delay
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = not(ledState);
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
}
After copying the above sketch to your Arduino IDE, navigate to Tools > Port option. Look for something like: esp32-
xxxxxx at your_esp_ip_address. If you are unable to locate it, you may need to restart your IDE.
26
A brief introduction to ESP32 and OTA Lab 5
Choose the port and press the Upload button. The new sketch will be uploaded in a matter of seconds. The on-board
LED should start blinking.
27
A brief introduction to ESP32 and OTA Lab 5
Lab Tasks
1. Connect 3 leds when you type FIRSTON so 1st led should be turn on and when you type
SECONDON second led should be on and so on. Similarly turn off the 1st led with FIRSTOFF
command and so on.
2. Connect a Serial monitor with PIR sensor whenever a motion is detected the serial monitor display
“Motion Detected” and when no motion detected serial monitor display “No Motion”
3. Make a spy robot if the distance of ultrasonic sensor is less than 20cm the LED will turn on.
4. By performing OTA, make a smart security system if the distance of ultrasonic sensor is less than 3
foot then BUZZ the alarm.
28