ESP32 IOT Kit Tutorial
ESP32 IOT Kit Tutorial
E-BOOK
By ANU
A STUDENT REQUIREMENT
TO THE FACULTY OF
COMMUNICATION
TABLE OF CONTENTS
I. INTRODUCTION
Io¶ zd¾c¶izo Tz A d¾ioz 1
ESP32 2
Se¶¶iog U A d¾ioz IDE 4
A d¾ioz P zg ammiog 5
ESP32 Bza d B iog-U 7
II.BASIC COMPONENTS
Io¶e faciog ×i¶h LED 10
Wz kiog ×i¶h P¾¨h B¾¶¶zo¨ 13
B¾ççe Io¶eg a¶izo 17
RGB LED Czo¶ zl 20
U¨iog a T imz¶ (Pz¶eo¶izme¶e ) 24
III. ACTUATORS
RelaÝ Mzd¾le Czo¶ zl 27
IV.SENSORS
Ligh¶ Seo¨z Io¶e face (LDR) 30
Iof a ed (IR) Seo¨z Io¶e face 34
DHT11 Seo¨z fz Teme a¶¾ e & H¾midi¶Ý 38
Why Arduino?
The Arduino software is user-friendly for beginners while stills providing enough flexibility for
experienced users. It is compatible with Mac, Windows, and Linux operating systems. Arduino has
been utilized in countless projects and applications.
Additionally, Arduino streamlines working with microcontrollers and offers distinct advantages for
teachers, students, and electronics hobbyists compared to other systems:
Inexpensive
Cross-platform
Simple, clear programming environment
Open source and extensible software
Open source and extensible hardware
Common Application
Prototyping
Educational purposes
DIY projects
Robotics
Home automation
1
ESP32
OVERVIEW
The ESP32 is a powerful, low-cost microcontroller board built around the ESP32 series SoC from
Espressif Systems. It is known for combining high-performance processing with built-in wireless
capabilities, making it a popular choice for IoT, automation, wearables, and embedded AI
applications. The ESP32 is widely used in both educational environments and professional product
development due to its versatility and rich feature set.
The board comes in several module variants—such as the ESP32-WROOM-32, ESP32-WROVER, and
other footprint-compatible modules. All variants are based on the same core SoC but differ in features
like RAM size, flash size, and antenna configuration. Despite these differences, they maintain the
same fundamental architecture and functionality.
KEY FEATURES
Microcontroller: ESP32-D0WDQ6 (dual-core Xtensa LX6)
Operating Voltage: 3.3V
Input Voltage (recommended): 5V via USB / 5–12V via VIN (depends
on development board)
Input Voltage (limits): 3.3V direct to module, 5–12V only through
onboard regulators (DevKit)
Digital I/O Pins: 34 GPIOs (multiplexed with peripherals; not all
available on DevKit)
Analog Input Pins: 18 × ADC channels (12-bit)
Analog Output Pins: 2 × DAC channels (8-bit)
PWM Channels: 16 (configurable LEDC PWM)
Communication: UART × 3, SPI × 3, I2C × 2, I2S × 2, CAN × 1
Wi-Fi: 802.11 b/g/n
Bluetooth: v4.2 BR/EDR + BLE
Flash Memory: 4 MB (typical for WROOM-32 module)
SRAM: 520 KB
External PSRAM: Not available on WROOM (available on WROVER)
Clock Speed: Up to 240 MHz
Operating Temperature: –40°C to +125°C (chip), module rated up to
+85°C
DC Current Consumption: 80–260 mA (active), <10 µA (deep sleep)
2
PIN-DIAGRAM
PIN DESCRIPTION
16 RX, 17 TX)
SPI: VSPI (GPIO 5 CS, 18 CLK, 19 MISO, 23 MOSI), HSPI (GPIO 15 CS,
3
Setting Up the Arduino IDE
STEP 1: DOWNLOAD THE ARDUINO IDE
1. Visit the Arduino Website (https://www.arduino.cc/): Open your web browser and go to the
Arduino Software page.
2. Select Your Operating System: Choose the appropriate version of the Arduino IDE for your
operating system (Windows, macOS, or Linux).
3. Download the Installer: Click on the download link to download the installer file to your
computer.
For Windows:
1. Run the Installer: Locate the downloaded installer file (usually in your Downloads folder) and
double-click it to run the installer.
2. Follow the Installation Wizard: Follow the on-screen instructions to complete the installation.
You may need to agree to the license agreement and choose the installation options. It is
recommended to install all the components.
3. Finish the Installation: Once the installation is complete, click on "Finish".
For macOS:
1. Open the Disk Image: Locate the downloaded disk image file (usually in your Downloads
folder) and double-click it to open.
2. Drag the Arduino Icon: Drag the Arduino application icon to the Applications folder.
3. Launch the Arduino IDE: Open the Applications folder and double-click the Arduino
application to launch it.
For Linux:
1. Extract the Archive: Locate the downloaded archive file (usually in your Downloads folder) and
extract it.
2. Run the Installer: Open a terminal window, navigate to the extracted folder, and run the
installation script by typing sudo ./install.sh.
3. Launch the Arduino IDE: After installation, you can launch the Arduino IDE by typing arduino
in the terminal.
4
Arduino Programming
OVERVIEW
Arduino programming is designed to be simple and accessible for beginners, yet powerful enough
for advanced users. Programs, or "sketches," are written in C or C++ and are uploaded to the Arduino
board or Arduino compatible Board to control its behaviour.
ARDUINO IDE
1. setup(): This function runs once when you press reset or power the board. It is used to initialize
variables, pin modes, start using libraries, etc.
void setup(){
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
2. loop(): This function runs continuously after the setup() function completes. It is used to actively
control the Arduino board.
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
EXAMPLE PROGRAM
Here is a simple example program that blinks the built-in LED on the ESP32 board:
5
// the setup function runs once when you press reset or power the board
void setup() { // initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
KEY FUNCTIONS
pinMode(pin, mode): Configures the specified pin to behave either as an input or an output.
digitalWrite(pin, value): Writes a HIGH or a LOW value to a digital pin.
digitalRead(pin): Reads the value from a specified digital pin, either HIGH or LOW.
analogRead(pin): Reads the value from the specified analog pin.
analogWrite(pin, value): Writes an analog value (PWM wave) to a pin.
delay(ms): Pauses the program for the amount of time (in milliseconds) specified as parameter.
ADDITIONAL RESOURCES
To advance your proficiency in Arduino programming, you can explore the official Arduino website,
which provides extensive documentation, tutorials, and community support.
6
Arduino Board Bring-Up
When you launch the Arduino IDE for the first time, your screen should look similar to the image
below:
Connect via USB Cable: Use a USB cable to connect your ESP32 board to your computer. One end
of the cable should be connected to the USB port on the ESP32 board, and the other end should be
connected to a USB port on your computer.
1. Launch the Arduino IDE: Open the Arduino IDE on your computer.
2. In your Arduino IDE, go to File > Preferences., Copy and paste the following line to the
Additional Boards Manager URLs field.
3. Additional Boards Manager - https://dl.espressif.com/dl/package_esp32_index.json
4. Select the Arduino Board: Go to Tools > Board and select your board (e.g., ESP32 DEV BOAD).
7
3.Select the Serial Port: Go to Tools > Port and select the serial port to which your ESP32 board is
connected. On Windows, it will be COMx (e.g., COM3), and on macOS or Linux, it will be something
like /dev/tty.usbmodemxxxx or /dev/ttyACM0.
1. Open the Example Sketch: Go to File > Examples > 01.Basics > Blink. This will open the Blink
example sketch
8
2. Upload the Sketch: Click the Upload button (right arrow icon) in the Arduino IDE. The code
will compile and upload to your ESP32 board. The built-in LED on the ESP32 board should start
blinking, indicating that the sketch has been successfully uploaded.
TROUBLESHOOTING
9
LED
A LED (Light Emitting Diode) is a semiconductor device which emits
light when electric current passes through it and this phenomenon we
call it as electroluminescence.
A LED contains two leads - positive terminal (longer one) and negative
terminal (shorter one). The colour of the LED depends upon the materials
used in semiconducting elements.
How to Use
To create a basic connection, an LED can be connected to a battery to make it glow. The positive lead
of the LED should be connected to the battery's positive terminal, and the negative lead to the
negative terminal. It's important to ensure that the battery voltage matches the LED's voltage rating.
If the voltage is too high, resistors can be used to regulate the voltage and protect the LED from
damage.
Common Application
1. To provide light
2. As indicators
3. Traffic Signals
Safety Measures
Sample Project
Let’s learn how to write a simple Arduino sketch to blink an LED on and off.
Materials Required
10
Circuit
Place the led and resistor on the breadboard.Connect the component as follows:
Connect the longer leg (anode) of the LED to the 3.3V rail on the breadboard through a 220-ohm
resistor.
Connect the shorter leg (cathode) of the LED to digital pin 23 on the ESP32.
Sketch
// the setup function runs once when you press reset or power the board
const int led_pin=23;
void setup ()
{
pinMode(led_pin, OUTPUT); // initialize digital pin13 as an output.
}
11
Verify your sketch by clicking the checkmark icon (Verify/Compile).
If no errors, upload the sketch by clicking the right arrow icon (Upload).
The LED connected to pin 23 should now start blinking on and off at a one-second interval.
Explana tion
Code Explanation:
Troubleshooting
If the LED doesn't blink, check connections and ensure the correct pin numbers and components are
used.
12
PUSH BUTTON
A pushbutton is a mechanical switch used to control electronic circuits. A
pushbutton has two states: pressed (closed circuit) and released (open
circuit).
How to Use
To use a pushbutton, you’ll typically set up a pull-down or pull-up resistor to ensure the input pin
reads a stable value when the button is not pressed. Most Commonly pull-up configuration is used.
One leg of the pushbutton is connected to the input pin of the microcontroller. The same leg is
connected to 3.3V via pull-up resistor (e.g., 10kΩ). The other leg of the pushbutton is connected to
GND.
Common Application
Safety Measures
1. Do not touch the terminals or other charged parts of the Switch while power is being supplied.
2. Do not use damaged switches.
Sample Project
Let’s learn how to use a push button to control an LED on and off.
Materials Required
13
Circuit
Place the led, pushbutton and resistor on the breadboard. Connect the components as follows:
Connect one leg of the push button to digital pin 22 on the Arduino.
Connect the other leg of the push button to the ground (GND) rail on the breadboard.
Connect the shorter leg (cathode) of the LED to digital pin 9 on the Arduino.
Connect the longer leg (anode) of the LED to the 3.3V rail on the breadboard through a 220-ohm
resistor.
Sketch
14
const int BUTTON=22;// Pin for the pushbutton
const int LED=23;// Pin for the LED
void setup()
{
pinMode( BUTTON , INPUT );// Set button pin as input with internal pull-up resistor
pinMode( LED , OUTPUT );// Set LED pin as output
void loop()
{
if(digitalRead(BUTTON)==HIGH)
{
digitalWrite(LED,HIGH);// Turn on LED
} else
{
digitalWrite(LED,LOW);// Turn off LED
}
}
Explana tion
Whenever pushbutton is pressed the pin 22 pull to ground which makes the ESP32 to detect
high and it turns on the led.
Code Explanation:
In the setup function, pinMode sets pin 23 as an output for the LED and pin 22 as an input for the
push button.
In the loop function, digitalRead checks the state of pin 23. If the button is pressed (pin 22 is
HIGH), the LED on pin 23 lights up; otherwise, it remains off.
Troubleshooting
15
Ensure the push button is correctly wired, and check connections and component orientations.
Verify that the code is uploaded correctly and that there are no syntax errors.
16
BUZZER
A buzzer is a simple device used to generate sound. Inside buzzer there is a
piezo membrane, that vibrates due to electric current because of that it
produces sound. There are two types of buzzers 1. Active buzzer ,2. Passive
buzzer.
How to Use
If your buzzer has a sticker on top of it, pull it off. Connect one pin (it doesn’t matter which one) to
the ESP32 ground (Gnd) and the other end to digital pin 23. From the ESP32, you can make sounds
with a buzzer by using tone function of Arduino.
Common Application
Safety Measures
1. Do not insert foreign objects into the sound-emitting hole or open hole.
2. Do not use this product in an atmosphere containing chlorine gas, sulfidizing gas, acid, or other
corrosive substances.
Sample Project
Let’s learn how to interface a Active buzzer module with three pins with Arduino and control it
using a simple Arduino sketch.
Materials Required
17
5. Jumper wires.
Circuit
Connect the VCC pin of the buzzer module to the 3.3V pin on the ESP32.
Connect the GND pin of the buzzer module to the GND pin on the ESP32.
Connect the I/O pin of the buzzer module to digital pin 23 on the ESP32.
Sketch
18
const int buzzerPin = 23; // Pin where the active buzzer is connected
void setup()
{
// Set buzzer pin as output
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
// Turn the buzzer on
digitalWrite(buzzerPin, HIGH);
delay(1000); // Wait for 1 second
Explana tion
Buzzer:
When pin 23 is set HIGH in the code, the buzzer will produce a sound.
Code Explanation:
In the setup function, pinMode sets pin 23 as an output for the buzzer. In the loop function,
digitalWrite is used to turn the buzzer on (HIGH) and off (LOW) alternately with a delay of 1000
milliseconds (1 second) between each state change.
Troubleshooting
Ensure the buzzer module is correctly wired, and check connections and component orientations.
Verify that the code is uploaded correctly and that there are no syntax errors.
19
RGB LED
RGB LED is a combination of three colours & it has four terminals. With
these colours we can produce different colours by adjusting the intensity of
each colour in LED using PWM pins in ESP32 board.
The pixels used in screens of devices like mobiles, laptops and TVs are
also rgb pixels. These kinds of LEDs are very common in decorative and
indicative purposes.
How to Use
Common Application
1. Displays
2. Decorative purposes
Safety Measures
Sample Project
Let’s learn how to interface an RGB LED with Esp32 and control its colours using simple
Arduino code.
Materials Required
20
Circuit
Place the RGB Led & Resistors on the breadboard. Connect the component as follows:
Sketch
21
void setup()
{
// put your setup code here, to run once:
pinMode(21,OUTPUT);//blue
pinMode(22,OUTPUT);//green
pinMode(23,OUTPUT);//red
}
void loop()
{
analogWrite(23,255);//red led on
analogWrite(22,0);
analogWrite(21,0);
delay(1000);
analogWrite(23,0);
analogWrite(22,255);//green led on
analogWrite(21,0);
delay(1000);
analogWrite(23,0);
analogWrite(22,0);
analogWrite(21,255);//blue led on
delay(1000);
}
Explana tion
Code Explanation:
In the setup function, pinMode sets digital pins 21, 22, and 23 as outputs for controlling the RGB
LED. In the loop function, analogWrite is used to vary the PWM (Pulse Width Modulation) signal
on each pin, adjusting the brightness of each color component (red, green, blue) to create different
colors.
Troubleshooting
22
Ensure the RGB LED is correctly oriented (common cathode to GND) and that resistors are
correctly placed to limit current flow.
Check connections and verify that the code is uploaded correctly without any syntax errors.
23
TRIMPOT
A Trimpot (trimming potentiometer) is a variable resistor in which
resistance can be varied so that current flows through the load (any output
device like LED etc.,) either can be increase or decrease.
How to Use
To establish a basic connection, connect one leg to 3.3V, another leg to GND, and finally, connect the
center leg to any analog pin on the microcontroller.
Common Application
Safety Measures
1. Use the Correct Type of Trimpot,if not it could lead to inaccurate adjustments or overheating.
2. Handle the trimpot carefully, especially when adjusting. Excessive force can damage the
adjustment screw or the internal components.
Sample Project
Let’s learn how to interface a trimpot with Esp32 to read analog values and control an output
based on those values.
Materials Required
Circuit
24
Connect the left pin of the trimpot to the 3.3V pin on the Esp32.
Connect the right pin of the trimpot to the GND pin on the Esp32.
Connect the middle pin of the trimpot to the D34 analog input pin on the Esp32.
Sketch
int potPin = 34; // Pin connected to the middle pin of the trimpot
int potValue = 0; //Variable to store the potentiometer value
void setup ()
{
Serial.begin(9600) ; // Initialize serial communication at 9600 bits per second
}
void loop ()
{
potValue = analogRead(potPin); // Read the value from the trimpot (0 to 1023)
Serial.print("Potentiometer Value: ") ;// Print the potentiometer value to the Serial Monitor
Serial.println(potValue); delay (500) ; // Wait 500 milliseconds
}
Explanation
25
Trimpot:
With the help of screw driver adjust the trimpot,this allows the Esp32 to read the voltage level
from the trimpot.
Code Explanation:
Troubleshooting
26
RELAY
A relay is an electrically operated switch that allows
you to control a high-power or high-voltage device
(like a motor, fan, or light) with a low-power signal
from a microcontroller, such as an Arduino.
How to Use
To make a simple connection, we can connect the Relay of Vcc,Gnd,Signal pins on the
microcontroller’s 5v,Gnd,digital output pins.On the other end connect these three sockets to your
load(e.g., a bulb etc.,)
Common Application
Safety Measures
1. If connecting to AC mains, be cautious and ensure proper insulation and safety measures to
avoid electric shock.
2. Always ensure you are aware of the voltage and current ratings of your relay and your load.
3. Working with relay should be done under expert supervision.
Sample Project
Let’s learn how to interface a relay module with an Esp32 to control a led using simple
Arduino code.
Materials Required
27
7. Jumper wires.
Circuit
Place the Led and resistor, relay on the breadboard. Connect the component as follows:
Connect the VCC pin of the relay module to the 5V pin on the Esp32.
Connect the GND pin of the relay module to the GND pin on the Esp32.
Connect the IN pin of the relay module to digital pin 23 on the Esp32.
Connect one end of the LED to the NO (Normally Open) terminal of the relay module.
Connect the other end of the LED to the GND rail on the breadboard through a 220-ohm resistor.
Connect the COM (Common) terminal of the relay module to the 5V pin on the Esp32.
Sketch
28
int relayPin = 23; // Pin connected to the relay
void setup()
{
pinMode(relayPin, OUTPUT); // Set relayPin as OUTPUT to control the relay
digitalWrite(relayPin, LOW); // Start with the relay off
}
void loop()
{
digitalWrite(relayPin, HIGH); // Turn the relay ON
delay(1000);// Wait for 1 second
digitalWrite(relayPin, LOW); // Turn the relay OFF
delay(1000);// Wait for 1 second
}
Explana tion
Relay Module :
When pin 23 is set to HIGH, the relay is activated, causing the LED to turn on.
When pin 23 is set to LOW, the relay doesn't activate, causing the LED to turn off.
Code Explanation:
In the setup function, pinMode sets the relay pin as an output, and digitalWrite(relayPin, LOW)
initializes the relay in the OFF state.
In the loop function, digitalWrite(relayPin, HIGH) turns the relay ON, and digitalWrite(relayPin,
LOW) turns the relay OFF, with a 1-second delay between each state.
Troubleshooting
29
LDR
The LDR (Light Dependent Resistor) is a sensor that detects the presence
of light or measures its intensity. It functions as a resistor, where its
resistance decreases with increasing light intensity.
How to Use
To set it up, connect the Vcc and Gnd pins of the LDR module to the +5V and Gnd pins on the
microcontroller. For an analog output, connect the D34 pin of the module to the D34 pin on the
microcontroller. Alternatively, if you prefer a digital output, connect the D23 pin to any digital pin on
the microcontroller.
Common Application
Safety Measures
Sample Project
Let’s learn how to interface an LDR (Light Dependent Resistor) sensor with an Esp32 to
measure ambient light levels using simple Arduino code.
Materials Required
Circuit
30
Connect the LDR sensor module as follows:
Sketch
31
// Interfacing ESP32 with LDR sensor
const int ldrPin = D34; // Analog pin 0
void setup() {
// The setup() function will only run once, after each powerup or reset of the Esp32 board.
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); // Here LED is determined as an output or an indicator.
pinMode(ldrPin, INPUT); // Here LDR sensor is determined as input.
}
void loop() {
// Void loop is ran again and again and contains main code.
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200) {
digitalWrite(LED_BUILTIN, HIGH); // If LDR senses darkness, LED pin is set high, causing the LED to glow.
Serial.print("Darkness over here, turn on the LED: ");
Serial.println(ldrStatus);
} else {
digitalWrite(LED_BUILTIN, LOW); // If LDR senses light, LED pin is set low, causing the LED to stop glowing.
Serial.print("There is sufficient light, turn off the LED: ");
Serial.println(ldrStatus);
}
}
Explana tion
LDR Sensor :
The AO pin is connected to D34 on the ESP32, which reads the analog value representing the
light intensity.
Code Explanation:
The analogRead function reads the analog value from the LDR sensor.
In the setup function, Serial.begin(9600) initializes serial communication at 9600 baud rate.
In the loop function, the analog value is read from the LDR and printed to the Serial Monitor
with a 500-millisecond delay between readings.
32
Troubleshooting
33
IR SENSOR MODULE
IR Sensor Module is a combination of IR Transmitter and IR Receiver.
This module is used to sense obstacle in its line of sight.
How to Use
To set it up, connect the Vcc and Gnd pins of the IR Sensor module to the +5V and Gnd pins on the
microcontroller. Then connect the D23 pin to any digital pin on the microcontroller.
Common Application
Safety Measures
Sample Project
Let’s learn how to interface an IR sensor module with an Esp32 to detect objects using simple
Arduino code.
Materials Required
34
Circuit
Place the Esp32 board and the IR sensor module on the breadboard.
Connect the IR sensor module as follows:
GND to GND on the Esp32
VCC to 5V on the Esp32
OUT to digital pin 23 on the Esp32
Sketch
35
const int irSensorPin = 23; // IR sensor connected to digital pin 2
void setup()
{
pinMode(irSensorPin, INPUT); // Set up the IR sensor pin as input
Serial.begin(9600);// Initialize serial communication for debugging
}
void loop()
{
int sensorValue = digitalRead(irSensorPin);// Read the sensor value
if (sensorValue == LOW)// If sensor detects an object
{
Serial.println("Object detected");// LOW means object detected (for most IR modules)
}
else
{
Serial.println("No object");
}
delay(100);// Small delay to avoid rapid printing
}
Explana tion
IR Sensor:
When it detects obstacle,pin 23 is set to LOW then it prints the status of object on serial monitor.
Code Explanation:
The digitalRead function reads the digital value from the IR sensor.
In the setup function, Serial.begin(9600) initializes serial communication at 9600 baud rate.
In the loop function, the digital value is read from the IR sensor and printed to the Serial Monitor
with a 500-millisecond delay between readings.
Troubleshooting
36
Verify that the Arduino is properly connected to the computer.
Adjust the sensitivity of the IR sensor if the values are not responding correctly to objects.
37
TEMPERATURE AND HUMIDITY SENSOR
How to Use
To make a simple connection, DHT11 sensor can be connected to an Esp32 or other microcontroller by
connecting the power (VCC and GND) and reading data through a digital pin.
Common Application
Safety Measures
1. Consider Time Drift and Temperature Drift errors while measuring the values
2. DHT11 sensor is designed to work within 3.3V to 5V so supplying a higher voltage can damage
the sensor permanently.
Sample Project
Let’s learn how to interface a DHT11 temperature and humidity sensor with an Esp32 and read
the sensor data.
Materials Required
Circuit
38
Place the Esp32 board and DHT11 sensor on the breadboard.Connect the component as follows:
DHT11 Sensor
Sketch
39
#include "DHT.h"
#define DHTPIN 23 // Pin to which the DHT11 is connected #define
DHTTYPE DHT11 // DHT11 sensor
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
dht.begin(); // Initialize the DHT11 sensor
}
void loop()
{
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Read temperature in Celsius
// Print temperature and humidity to the Serial Monitor
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
Explana tion
DHT11 SENSOR
The data pin is connected to digital pin 2 on the Arduino to read the sensor data.
Code Explanation
40
Troubleshooting
41
OLED
An OLED (Organic Light Emitting Diode) 0.96" I²C display is a small, low-
power digital screen used to visually show text, numbers, icons, and simple
graphics. It uses tiny organic LEDs as pixels, so each pixel emits its own
light, providing high contrast and clear visibility even in low-light
conditions. The display typically communicates with ESP32 using the I²C
(protocol, which requires only two data lines (SCL and SDA),
How to Use
To make a simple connection, the 0.96" OLED I²C display can be connected to an ESP32 by connecting
the power pins (VCC and GND) and using the I²C pins (SDA and SCL) to send and receive data.
Common Application
Safety Measures
1. Avoid supplying voltage above the recommended 3.3V–5V range, as overvoltage can
permanently damage the OLED panel.
2. Prevent long-term display of static images to avoid pixel burn-in and ensure longer screen life.
Sample Project
Sample Project – Let’s learn how to interface a 0.96" OLED I²C display with an ESP32 and show text.
Materials Required
Circuit
42
Place the Esp32 board and Display sensor on the breadboard.Connect the component as follows:
OLED DISPLAY
Sketch
43
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello OLED!");
display.display();
}
void loop() {
}
Explanation
OLED Display
The SDA and SCL pins of the OLED are connected to the ESP32’s I²C pins (GPIO 21 for SDA and
GPIO 22 for SCL) to communicate with the display.
Code Explanation
The Adafruit SSD1306 and GFX libraries are used to control the OLED display.
In the setup function, display.begin() initializes the OLED, and display.clearDisplay() prepares the
screen. The text size, text color, and cursor position are set, and a message is printed to the OLED.
In the loop function, nothing is repeated because the display shows the text once and remains on the
screen.
44
Troubleshooting
Ensure all I²C connections (VCC, GND, SDA, SCL) are secure and correctly connected to the ESP32.
Verify that the ESP32 is properly connected to the computer and the correct COM port is selected.
Make sure the OLED I²C address (usually 0x3C) is correct in the code. Confirm that the required
libraries (Adafruit SSD1306 and Adafruit GFX) are installed.
45
RESISTOR COLOUR CODING
164