100% found this document useful (1 vote)
17 views14 pages

Working With ESP32

The document provides an overview of the ESP8266 NodeMCU and ESP32-WROOM32 microcontroller boards, detailing their capabilities, pinouts, and ideal use cases for IoT projects. It also covers setting up the Arduino IDE for programming, uploading a basic 'Hello World' program, and troubleshooting common issues. Additionally, it discusses communication protocols like UART, I2C, and SPI, along with GPIO and ADC functionalities.

Uploaded by

104240647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
17 views14 pages

Working With ESP32

The document provides an overview of the ESP8266 NodeMCU and ESP32-WROOM32 microcontroller boards, detailing their capabilities, pinouts, and ideal use cases for IoT projects. It also covers setting up the Arduino IDE for programming, uploading a basic 'Hello World' program, and troubleshooting common issues. Additionally, it discusses communication protocols like UART, I2C, and SPI, along with GPIO and ADC functionalities.

Uploaded by

104240647
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Created by Turbolearn AI

ESP8266 NodeMCU - What It Is


A low-cost System-on-a-Chip (SoC) made by Espressif Systems.
It's essentially a microcontroller with built-in WiFi networking capabilities.
Capabilities:
Connect to Wi-Fi: Allows your projects to join existing Wi-Fi networks or create their own
(Access Point).
Run Programs: Executes code to read sensors, control outputs, process data, and communicate
over the network.
Ideal for: Simple IoT devices, Wi-Fi sensors, basic remote control, and data logging over Wi-Fi.

Esp8266 NodeMCU - 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).

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".

For beginners, it is best to use more common and well-documented boards.


For ESP32 that uses WROOM32 chip, the pinouts are similar for most boards.
Here are some recommendations:
ESP8266 NodeMCU (V3)
ESP32 - NodeMCU-32S
ESP32-WROOM32 - 30 Pin

Setting Up Your IDE - Arduino IDE


IDE = Integrated Development Environment.

It's a free, open-source software application created by the Arduino team.


Runs on your computer (Windows, macOS, Linux).
Specifically designed to make programming microcontroller boards easy.

What is it Used For?

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.

"Hello_World" - Uploading Your First Program


This program will blink the built-in light of your ESP32/ESP8266. This is often called the "Hello World"
program for IoT development.

Steps:
1. Creating a new "Sketch"
2. Write the Code and Verify
3. Select Other Board and Port

Troubleshooting - No Ports Founded


Case 1: No USB Driver for CP210X
Some Board manufacturers use the CP2102 UART Chip instead of the CH340.
Install USB Drivers – CP210x USB to UART Bridge (Windows PC)
Install USB Drivers – CP210x USB to UART Bridge (Mac OS X)
Case 2: Bad USB cable
Some Micro-USB/USB-C cables may not have a wire for Data transmission, we recommend
using a high-quality phone charger with data transfer capabilities/ keyboard wire.

Which Board/Port to Choose


Which Board to Choose:
ESP32-WROOM32 30 PIN: ESP32 DEV MODULE.
ESP32-NODEMCU 32-S: NODE32S.
ESP8266 (V3): Generic ESP8266 module.
Which Port to Choose:
In the case that there are too many ports and you do not know which one is your Board, you can
go to Device Manager → Ports.
Usually, the USB-SERIAL CH340 or CP210X will be the Device.

Uploading Your First Program


Press the Upload Button. If you encounter an error of not in Download Mode:

Press and Hold the “BOOT” or “Flash” button when the terminal says: Connecting . . .

Successfully Uploaded the Code!

Page 4
Created by Turbolearn AI

ESP32 Communication & Interfacing

Device Simulation
Simulation is a great way to learn and code simple IoT programs without buying the physical parts.

Wokwi is a Great Website for this.


It also has Bluetooth and Wifi Simulation, however, you need to pay to use these features (or
use the PlatformIO extension in VSCode)

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).

UART Protocol (Universal Asynchronous Receiver/Transmitter)


A hardware component that facilitates serial communication between devices. "Asynchronous"
means the sender and receiver agree on a speed (baud rate) and use start/stop bits to frame
each byte of data to time the bits.

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.

The Serial Monitor


A built-in tool in the Arduino IDE (and similar IDEs like PlatformIO) that acts as a terminal. It
allows you to:

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().

Baud Rate: speed in bits per second (bps).

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.

Basic Commands Reference

Page 5
Created by Turbolearn AI

Command Description Example

Initializes the default serial port


Serial.begin(baudrate) (UART0) and sets the $Serial.begin(115200);$
communication speed.
Sends data to the serial port.
The data can be text, numbers,
$Serial.print("Value: ");
Serial.print(data) variables, etc. It does not
Serial.print(sensorValue);$
automatically add a new line
after the data.
Similar to Serial.print(), but
adds a newline character (\n)
and a carriage return (\r) at the
Serial.println(data) $Serial.println("Hello, ESP32!");$
end, so the next print will start
on a new line in the Serial
Monitor.
Allows for formatted printing,
$Serial.printf("Temperature: %.2f
similar to printf in C. This is
Serial.printf(format, ...) C, Humidity: %d %%\\n", temp,
very useful for neatly
hum);$
displaying variables.
Returns the number of bytes
(characters) available for
reading from the serial port. $if (Serial.available() > 0) { ...
Serial.available()
Useful to check if there's }
incoming data before trying to
read it.
Reads a single incoming byte
(character) from the serial $char incomingByte =
Serial.read()
buffer. Returns -1 if no data is Serial.read();$
available.
Reads characters from the
Serial.readString() serial buffer into a String object
until a timeout occurs.
Reads characters into a String
until a specific terminator $String command =
Serial.readStringUntil(terminator)
character is encountered or a Serial.readStringUntil('\\n');$
timeout occurs.

GPIO - General Purpose Input/Output

Page 6
Created by Turbolearn AI

These are digital pins on the ESP32 that you can programmatically control.

They are the "eyes, ears, and hands" of the microcontroller:


Output: Control LEDs, relays, send signals to other chips.
Input: Read the state of buttons, switches, or signals from sensors.
Each GPIO pin can be configured to be either an input (to read signals) or an output (to send signals).

Voltage Level
ESP32 GPIOs operate at a 3.3V logic level.

A HIGH signal is ~3.3V .


A LOW signal is ~0V (GND).
Crucial Warning: ESP32 pins are generally NOT 5V tolerant. Connecting a 5V signal directly to an
ESP32 input pin can damage it. Use a logic level shifter if you need to interface with 5V devices.
When picking a GPIO pin, you should always check if it is OKAY TO USE.

pinMode(pin, MODE) Function


The pinMode(pin, MODE) function is used in your setup() to configure a GPIO pin's behavior.

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.

Digital Input/Output Functions


Output:
The digitalWrite(pin, VALUE) function sets the state of a GPIO pin configured as an OUTPUT.
pin: The GPIO number.
VALUE:
HIGH: Sets the pin's output voltage to 3.3V.
LOW: Sets the pin's output voltage to 0V (GND).
Input:
The digitalRead(pin) function reads the current state of a GPIO pin configured as an INPUT or
INPUT_PULLUP.
Returns HIGH (usually 1) if the voltage is above a certain threshold.
Returns LOW (usually 0) if the voltage is below a certain threshold.

ADC - Analog to Digital Converter


Takes an analog voltage, which is a continuous signal that can have any value within a certain range
(output of a potentiometer or types of sensors), and convert it to a digital number that the microcontroller
can understand and process.

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

A 0V input correspond to a digital value of 0.


The maximum input voltage (e.g., 3.3V) would correspond to a value of 4095.
ADC Channels/Units: Two main ADC units: ADC1 and ADC2.
If you are using any wifi function, ADC2 will not be usable, find out why!
To read an analog value, $analogRead(pin)$:
pin: The GPIO number

Analog to Digital Conversion (ADC)

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.

Communication Protocols: I2C and SPI

I²C (Inter-Integrated Circuit)


Also known as "I-two-C" or "I-Squared-C", this is a two-wire interface that uses only two signal lines:

SDA (Serial Data): Carries the data being transmitted.


SCL (Serial Clock): Carries the clock signal that synchronizes data transfer, generated by the master.

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.

I²C Basic Commands (using Wire.h library)

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.

SPI (Serial Peripheral Interface)


Typically uses four wires:

MOSI (Master Out, Slave In): Data from master to slave.


MISO (Master In, Slave Out): Data from slave to master.
SCLK (Serial Clock): Clock signal generated by the master to synchronize data transfer.
CS (Chip Select): Used by the master to select which slave device it wants to communicate with.
Each slave needs its own dedicated CS line, which is active LOW.

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 Basic Commands (using SPI.h library)


To use SPI, include the SPI.h library with #include <SPI.h>. Here are some basic commands:

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.

ESP32 - WiFi Functionality

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

Access Point Mode (AP):


The ESP32 creates its own WiFi network, allowing other devices to connect directly to it.
Useful for:
Direct device-to-device communication when no existing WiFi network is available.
Initial configuration of an ESP32 device (e.g., a phone connects to the ESP32's AP to
provide credentials for the home WiFi network).
Creating a small, localized network.
The ESP32 in AP mode acts like a mini-router.
STA+AP Mode (Station + Access Point):
The ESP32 can operate in both modes simultaneously, connecting to an existing WiFi network
(STA) while also hosting its own network (AP).
Useful for bridging networks or for devices that need to offer a direct configuration interface
while also being connected to a main network.

Basic WiFi Commands (WiFi.h Library)

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).

Pro Tips: Dashboards


It is best practice to create your own Web App dashboard and communicate via HTTP, MQTT, or SDK,
either locally hosted or using a cloud provider (e.g., Azure IoT Hub, AWS IoT).
Alternatively, use platforms to quickly create a dashboard to visualize and remotely control your
system:
Node-Red: Software that helps construct dashboards quickly without much coding.
Blynk: A famous and powerful IoT Cloud provider that can also create dashboards quickly.
Thinkspeak: A free and easy-to-use Cloud IoT platform.
Adafruit-IO

Page 13
Created by Turbolearn AI

Pro Tips: PlatformIO + Wokwi


While the Arduino IDE is great for beginners, PlatformIO (as a VS Code extension) grants you robust
library management, versatile board support, and integrated debugging features for more ambitious
IoT projects.
Wokwi, also a VS Code extension, provides powerful simulation capabilities, including Wi-Fi
functionality, allowing you to test complex IoT scenarios without physical hardware.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy