Working With ESP32
Working With ESP32
About ESP32-WROOM32
The ESP32-WROOM32 is a microcontroller board. The large silver chip with the text "ESP32-WROVER" is
prominently displayed on the top right side of the board.
Page 1
Created by Turbolearn AI
A more powerful, next-generation SoC from Espressif Systems, designed as an upgrade to the
ESP8266.
A microcontroller featuring both integrated Wi-Fi and Bluetooth.
Capabilities:
Connect to Wi-Fi: Same capabilities as ESP8266 but often with better performance.
Connect via Bluetooth: Supports both Bluetooth Classic and Bluetooth Low Energy (BLE).
Run Complex Programs: Significantly faster processor (dual-core up to 240 MHz) and more
RAM allow for more demanding tasks, faster processing, and handling Wi-Fi/Bluetooth
simultaneously.
Ideal for: Advanced IoT projects, applications needing Bluetooth, projects requiring more processing
power or more I/O pins, low-power BLE devices.
ESP32-WROOM32 Pinout
GPIO: Around 11-17 General Purpose Input/Output pins (number varies depending on the specific
module).
Wi-Fi: Integrated 2.4 GHz
Common Interfaces:
UART (Serial communication)
SPI (Serial Peripheral Interface)
I2C (Inter-Integrated Circuit)
PWM (Pulse Width Modulation)
Analog Input: 1x Analog-to-Digital Converter (ADC) pin (usually 10-bit resolution).
Warning
Page 2
Created by Turbolearn AI
The above is a pinout diagram for the ESP32 DEVKIT V1 - DOIT development board. The board is shown in
the center, with its pins labeled and color-coded on either side, indicating their various functions such as
ADC, DAC, touch, and communication interfaces.
There are many types of ESP32/ESP8266, that are either different versions of the boards or made by
different manufacturers.
The pinout of each board may be different.
Please refer to the DATASET of your chosen board for accurate information.
Recommendation
Here are some recommendations for boards to use:
The image shows a collection of three electronic circuit boards, each with distinct features and labels. The
first one on the left labeled "Esp8266 NodeMCU (V3)", the middle one labeled "ESP32 - NodeMCU-32S",
and the rightmost one labeled "ESP32-WROOM32 - 30 Pin".
Page 3
Created by Turbolearn AI
Writing Code: Simple text editor for writing your programs (called "sketches") in the Arduino
language (based on C/C++).
Verifying/Compiling Code: Checks your code for errors and translates it into instructions the
microcontroller understands.
Uploading Code: Sends your compiled program from the computer to the microcontroller board (like
Arduino, ESP8266, ESP32) via USB.
Managing Boards: Select the specific type of board you are using.
Managing Libraries: Easily add pre-written code packages (libraries) to control sensors, displays,
motors, Wi-Fi, etc.
Serial Monitor: Built-in window to send/receive messages between your computer and the
microcontroller – essential for debugging and seeing output.
Steps:
1. Creating a new "Sketch"
2. Write the Code and Verify
3. Select Other Board and Port
Press and Hold the “BOOT” or “Flash” button when the terminal says: Connecting . . .
Page 4
Created by Turbolearn AI
Device Simulation
Simulation is a great way to learn and code simple IoT programs without buying the physical parts.
Serial Communication
Serial communication is a process where data is sent or received one bit at a time, sequentially, over a
single communication channel or wire. Allowing us to:
Debug: Print status messages, variable values, and error codes to see what the ESP32 is doing.
Log Data: Send readings or data to a computer for storage and analysis.
Interact: Send commands from a computer to the ESP32 to control its behavior.
Program: The flashing of the ESP32 often happens over a serial connection (via USB).
The UART 0 pins on the ESP32 are the default and are linked to the USB port for flashing.
You should not use UART 0 for communication, instead use UART 1, UART 2..., otherwise flashing
will fail.
You can also remap UART pins to other pins, you should use the safe pins.
View Output: See any data the ESP32 sends via Serial.print() or Serial.println().
Send Input: Type data and send it to the ESP32 for your code to read via Serial.read().
It's critical that the baud rate set in the Serial Monitor exactly matches the rate specified in your
ESP32 code (e.g., Serial.begin(115200)).
Common rates include 9600, 115200 . If they don't match, you'll see garbage characters or nothing at
all.
Page 5
Created by Turbolearn AI
Page 6
Created by Turbolearn AI
These are digital pins on the ESP32 that you can programmatically control.
Voltage Level
ESP32 GPIOs operate at a 3.3V logic level.
Page 7
Created by Turbolearn AI
pin: The GPIO number you want to configure (e.g., 2, 13, 27).
OUTPUT: Configures the pin as a digital output. You can then set it HIGH or LOW.
INPUT: Configures the pin as a digital input. You can then read its state. By default, an input pin is
"floating" – if nothing is connected, its state can be unpredictable.
INPUT_PULLUP: Configures the pin as a digital input and enables an internal pullup resistor. This
connects the pin to VCC (3.3V) through this resistor. If an external switch then connects the pin
to GND, the pin will read LOW. If the switch is open, it reads HIGH.
INPUT_PULLDOWN: internal pull-down resistors, connecting the pin to GND.
Resolution: The ESP32's ADCs are typically 12-bit. They can represent an analog voltage as a digital
value ranging from 0 up to 2 − 1 = 4095.
12
AnalogRead() Function
The analogRead() function is used to read analog values from specified pins that are ADC-capable, such as
pins 34 and 36 on the ESP32.
Page 8
Created by Turbolearn AI
It returns an integer value that corresponds to the ADC's resolution and the current attenuation
setting.
For example, a 12-bit resolution ADC will return values from 0 to 4095.
To learn about analogWrite(), refer to the PWM documentation.
In the image above, the ESP-WROOM-32 microcontroller module is shown with the I2C SCL (GPIO22) and
I2C SDA (GPIO21) pins highlighted.
Master-Slave Architecture
One device on the bus is the master (typically the ESP32), which initiates communication and
generates the clock signal.
Other devices are slaves (e.g., sensors, displays) that respond to the master.
Addressing
Each slave device on an I²C bus has a unique 7-bit (or sometimes 10-bit) address, allowing the
master to select which slave to communicate with. This enables multiple slaves to share the
same SDA and SCL lines.
Note: You can find the device address in the device documentation or by using an address scanner script.
Page 9
Created by Turbolearn AI
To use I2C, include the Wire.h library with #include <Wire.h>. Here are some basic commands:
Wire.begin(optional_sdaPin, optional_sclPin): Initializes the I²C bus. Call this in setup(). If pins are
not specified, it uses the defaults (SDA - GPIO 21, SCL - GPIO 22).
Wire.beginTransmission(deviceAddress): Starts communication with the slave device at the given 7-
bit address.
Wire.write(data): Sends a byte of data (or an array of bytes) to the slave.
Wire.endTransmission(): Ends the transmission and returns a status code (0 for success).
Wire.requestFrom(deviceAddress, numBytes): The master requests numBytes of data from the slave.
Wire.available(): Returns the number of bytes available to be read after a requestFrom().
Wire.read(): Reads a byte of data that was sent from the slave.
The image shows the pin configuration of the ESP-WROOM-32 microchip. Note the VSPI MOSI, VSPI
MISO, VSPI CLK, and VSPI CS0 labels highlighted in green for SPI.
Key Features:
Page 10
Created by Turbolearn AI
Master-Slave Architecture: Similar to I²C, with the ESP32 typically acting as the master.
Full-Duplex: Data can be sent from master to slave (MOSI) and from slave to master (MISO)
simultaneously.
The ESP32 has multiple SPI controllers.
VSPI (Versatile SPI) and HSPI (Hardware SPI) are the main ones often used.
SPI0/SPI1 are typically reserved for accessing the integrated flash memory and PSRAM.
Default Pins (Arduino Core for VSPI):
GPIO23 for MOSI
GPIO19 for MISO
GPIO18 for SCLK
GPIO5 for CS (though any GPIO can be used as a CS pin, controlled manually in your code)
SPI.begin(sckPin, misoPin, mosiPin, ssPin): Initializes the SPI bus. If pins are not specified, it uses
the defaults for VSPI. The ssPin here is often a default, but you'll usually control CS pins manually.
SPI.beginTransaction(SPISettings(speed, bitOrder, dataMode)): Configures the SPI bus settings
before communication.
speed: Clock speed (e.g., 1000000 for 1MHz).
bitOrder: MSBFIRST or LSBFIRST.
dataMode: SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3 (defines clock polarity and phase).
digitalWrite(csPin, LOW): Manually activate the Chip Select line by pulling it LOW.
SPI.transfer(data): Sends a byte of data and receives a byte of data simultaneously. If you only want
to send, you can ignore the return value. If you only want to receive, send a dummy byte (e.g., 0x00).
SPI.transfer(buffer, size): Transfers multiple bytes.
digitalWrite(csPin, HIGH): Manually deactivate the Chip Select line by pulling it HIGH after the
transaction.
SPI.endTransaction(): Releases the SPI bus configuration.
Introductory
One of the most compelling features of the ESP32 is its built-in WiFi (and Bluetooth), which
makes it an excellent choice for Internet of Things (IoT) projects.
Page 11
Created by Turbolearn AI
The ESP32 supports 2.4 GHz WiFi (IEEE 802.11 b/g/n) standards and can operate in several WiFi modes,
offering flexibility in how it connects to or creates networks.
Modes of Operation
Station Mode (STA):
The ESP32 acts like a typical WiFi client device (like a laptop or smartphone).
It connects to an existing WiFi network, usually provided by a wireless router.
Once connected, it can access the internet or communicate with other devices on the same local
network.
Useful for IoT devices that need to send data to a server or be controlled over the internet.
Page 12
Created by Turbolearn AI
STA Mode
const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";
WiFi.begin(ssid, password);: Initiates the connection attempt. This is non-blocking, so you need to
check the status.
WiFi.status(): Returns the connection status. Key values:
WL_CONNECTED: Successfully connected.
WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED, etc.
WiFi.localIP(): Once connected, this returns the IP address assigned to the ESP32 by the router.
AP Mode
const char* ap_ssid = "ESP32_AP";
const char* ap_password = "password123"; // Use a good password, min 8 chars for WPA2-PSK
WiFi.softAP(ap_ssid, ap_password);: Creates the Access Point.
WiFi.softAPIP(): Returns the IP address of the ESP32's AP (usually something like 192.168.4.1 by
default).
Page 13
Created by Turbolearn AI
Project Ideas
1. Smart Weather Station:
Collect environmental data using the microcontroller.
Analyze the data.
Visualize the data on a dashboard.
Notify the user of the weather data (via phone app, email).
2. Smart Irrigation System:
Collect the soil moisture of a plant.
Analyze if the plant needs to be watered.
Automatically water the plant (via pump).
Notify/show the user the state of the plant.
Implement a manual stop trigger (via phone app/dashboard).
3. Gas-Leak Detector:
Collect harmful gas data in the environment.
Compare to a threshold.
Notify the user when the air is no longer safe (via beep/phone app/email).
Display the current condition of the gas in the environment on a dashboard or phone app.
4. Heart-Monitoring Device:
Collect heartbeat data from the user.
Notify if the heartbeat is too high (via beep, phone app, email).
Display the heartbeat on a dashboard or phone app.
Feel free to add more ideas or create your own (perhaps with some AI integration).
It is recommended to use Github to manage your project. Ask for guidance, not just for answers!
Page 14