0% found this document useful (0 votes)
137 views49 pages

ESP32 IOT Kit Tutorial

The document is an e-book on the ESP32 microcontroller, detailing its features, setup, and programming using the Arduino IDE. It covers basic components, actuators, sensors, and provides example projects, including LED and push button applications. The content is aimed at students and electronics enthusiasts for educational purposes and DIY projects.

Uploaded by

Sathish Kumar
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
0% found this document useful (0 votes)
137 views49 pages

ESP32 IOT Kit Tutorial

The document is an e-book on the ESP32 microcontroller, detailing its features, setup, and programming using the Arduino IDE. It covers basic components, actuators, sensors, and provides example projects, including LED and push button applications. The content is aimed at students and electronics enthusiasts for educational purposes and DIY projects.

Uploaded by

Sathish Kumar
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

ESP32-IOT

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 imz¶ (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 Teme a¶¾ e & H¾midi¶Ý 38

ARDUINO STARTER KIT - ANUELECTRONICS.COM


A STUDENT REQUIREMENT
TO THE FACULTY OF
COMMUNICATION
TABLE OF CONTENTS

III.DISPLAYS AND INDICATORS


I2C LCD Display Connection 42

ARDUINO STARTER KIT - ANUELECTRONICS.COM


ARDUINO
Arduino is an open-source electronics platform based on easy-to-use hardware and software. It
consists of microcontroller boards (like Arduino UNO) that can be programmed to read inputs (e.g.,
sensors) and control outputs (e.g., lights, motors). It's widely used for DIY electronics projects,
prototyping, and learning programming and electronics.

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

GPIO Pins: 34 total GPIOs (usable count depends on board)

Analog Inputs (ADC): 18 channels on GPIO 0, 2, 4, 12–15, 25–27, 32–39

Analog Outputs (DAC): 2 channels on GPIO 25 and GPIO 26


PWM: 16 channels (available on most GPIOs via LEDC)
UART: UART0 (GPIO 1 TX, 3 RX), UART1 (GPIO 9, 10), UART2 (GPIO

16 RX, 17 TX)

SPI: VSPI (GPIO 5 CS, 18 CLK, 19 MISO, 23 MOSI), HSPI (GPIO 15 CS,

14 CLK, 12 MISO, 13 MOSI)


I2C: Default pins GPIO 21 (SDA) and GPIO 22 (SCL)

I2S: Available on GPIO 25, 26, 27, 14, 15 (configurable)

Touch Sensors: GPIO 0, 2, 4, 12–15, 27, 32, 33

Boot/Strapping Pins: GPIO 0, 2, 4, 5, 12, 15

Power Pins: 3V3, 5V, GND, EN

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.

STEP 2: INSTALL THE ARDUINO IDE

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

The Arduino Integrated Development Environment (IDE) is a cross-platform application (for


Windows, macOS, and Linux) that simplifies the process of writing code and uploading it to the
Arduino compatible board. The IDE comes with a software library called "Wiring," which
provides many common input/output operations to make programming easier.

BASIC STRUCTURE OF AN ARDUINO SKETCH

An Arduino sketch typically consists of two main functions:

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:

STEP 1: CONNECT THE ESP32 BOARD TO YOUR COMPUTER

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.

STEP 2: CONFIGURE THE ARDUINO IDE

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.

4.Select the Programmer: Go to Tools > Programmer and select "ArduinoISP".

STEP 5: VERIFY THE CONNECTION

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

If the Arduino IDE does not recognize the board:

Ensure the USB cable is properly connected.


Try using a different USB cable or USB port on your computer.
Check if the necessary drivers are installed (especially on Windows).
Restart the Arduino IDE and your computer if needed.

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

1. Don’t put the LED close to the eyes


2. Supplying a high voltage might damage the LED.

Sample Project

Let’s learn how to write a simple Arduino sketch to blink an LED on and off.

Materials Required

1. ESP32 board (e.g., ESP32 DEV Board).


2. USB cable (for connecting esp32 to computer).
3. Breadboard.
4. LED (Red colour).
5. 220-ohm resistor.
6. Jumper wires.

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

// 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.
}

// the loop function runs over and over again forever


void loop ()
{
digitalWrite(led_pin, HIGH); // turn the LED on (HIGH is the voltage level)
delay (1000); // wait for a second
digitalWrite(led_pin, LOW); // turn the LED off by making the voltage LOW
delay (1000); // wait for a second
}

Upload and Run the Sketch

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:

In the setup function, pinMode sets pin 23 as an output.


In the loop function, digitalWrite turns the LED on and off with a delay of 1000 milliseconds (1
second) between each state change.

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

It acts as a switch that controls the flow of electricity in a circuit when


pressed.

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

1. To turn ON and OFF the flow of current in a circuit.


2. Buttons are used in calculators, push button phones, etc.

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

1. ESP32 board (e.g., Arduino Uno).


2. USB cable (for connecting ESP32 to computer).
3. Breadboard.
4. LED (any colour).
5. 220-ohm resistor.
6. Push button.
7. Jumper wires.

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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
}
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Press the push button connected to pin 22.
The LED connected to pin 23 should turn on while the button is pressed and turn off when
released.

Explana tion

How Push Button Works

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.

Active Buzzer-It has in-built oscillator so it can generate sound at fixed


single frequency tone.
Passive buzzer -It doesn’t have in-built oscillator, so we need to give
external frequencies to generate sound. It can produce different tone &
Sound.

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

1. Siren in security locks.


2. Alarm devices.
3. Any project where sound needs to be produced

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

1. ESP32 board (e.g., ESP32 DEV Board).


2. USB cable (for connecting ESP32 to computer).
3. Breadboard.
4. Buzzer module with three pins (VCC, I/O, GND)

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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

// Turn the buzzer off


digitalWrite(buzzerPin, LOW);
delay(1000); // Wait for 1 second
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
The buzzer should produce a sound with a 1-second on/off cycle.

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

RGB LEDs typically come in two types: common cathode and


common anode. If RGB is Common cathode Connect the long leg
to GND on the Arduino. If RGB is Common Anode Connect the
long leg to 3.3V on the Esp32.

Common Application

1. Displays
2. Decorative purposes

Safety Measures

1. Supplying a very high voltage might damage the LED.

Sample Project

Let’s learn how to interface an RGB LED with Esp32 and control its colours using simple
Arduino code.

Materials Required

1. ESP32 board (e.g., ESP32 DEV Board).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. RGB LED (common cathode).
5. 3 x 220-ohm resistors.
6. Jumper wires.

20
Circuit

Place the RGB Led & Resistors on the breadboard. Connect the component as follows:

Identify the common cathode of the RGB LED.


Connect the common cathode of the RGB LED to the ground (GND) rail on the breadboard.
Connect the red pin of the RGB LED to digital pin 23 on the Esp32 through a 220-ohm resistor.
Connect the green pin of the RGB LED to digital pin 22 on the Esp32 through a 220-ohm
resistor.
Connect the blue pin of the RGB LED to digital pin 21 on the Esp32 through a 220-ohm
resistor.

Sketch

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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);
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
The RGB LED should cycle through red, green, and blue colors with a 1- second delay between
each colour change.

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

1. LCD Contrast Adjustment


2. Current Control
3. To calibrate or adjust sensitivity for accurate sensor readings.

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

1. Esp32 board (e.g., Esp32 DEV BOARD).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. Trimpot (10k ohms is common)
5. Jumper wires.

Circuit

Place the Esp32 board on the breadboard.Connect the component as follows:

Insert the trimpot into the breadboard.

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Open the Serial Monitor (Tools > Serial Monitor) to see the potentiometer values being printed.

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:

In the setup function, Serial.begin(9600) initializes serial communication.


In the loop function, analogRead reads the value from the trimpot connected to D34. This value is
then printed to the Serial Monitor using Serial.println.

Troubleshooting

Ensure all connections are secure and correct.


Check that the trimpot is functioning and that the Esp32 is properly connected to the
computer.

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.

Relays are used when the microcontroller cannot


directly power a high-voltage or high-current device.

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

1. To control circuits such as a lamp.


2. In complex appliances such as refrigerators and air conditioners
3. Home Automation.

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

1. Esp32 board (e.g., Esp32 DEV BOARD).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. LED (Red colour).
5. 220-ohm resistor.
6. Relay module

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
The Relay should turn on and off every 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

Ensure all connections are secure and correct.


Check that the relay module is properly oriented and that the Arduino is properly connected to
the computer.

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.

Here photosensitive materials like cadmium selenide is used to fabricate


the zig zag lines, to protect those lines transparent material is coated on
top of LDR in order to absorb the light.

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

1. Detects light intensity.


2. Automatic ON/OFF Street Light.

Safety Measures

1. Prevent exposure to excessive light, as it could potentially damage the sensor.


2. Avoid electrical overload to prevent damaging the sensor and circuit.

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

1. Esp32 board (e.g., Esp32 Dev Board).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. LDR sensor module
5. Jumper wires.

Circuit

30
Connect the LDR sensor module as follows:

GND to GND on the Esp32


VCC to 5V on the Esp32
AO to D34 on the Esp32

Sketch

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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);
}
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Open the Serial Monitor by clicking the magnifying glass icon in the top-right corner of the
Arduino IDE or by going to Tools > Serial Monitor.
You should see the LDR values being printed to the Serial Monitor.

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

Ensure all connections are secure and correct.


Verify that the Arduino is properly connected to the computer.
Adjust the potentiometer on the LDR module if the values are not responding correctly to
changes in light.

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.

IR Transmitter-It’s a transparent LED they can easily spot in all TV remote.


The Infra Red Radiation which is emitted by IR LED is invisible to human
eye.

IR Receiver-It’s a photodiode (dark in colour) when reflected infrared


radiation falls on photodiode which produces electric current.The
Detection Range of an IR sensor is 2 to 20cm & the Detection Angle is 30°
to 40°.

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

1. In obstacle avoider robots.


2. In aids for visually impaired

Safety Measures

1. Avoid using or storing the sensor under direct sunlight.


2. Avoid using it in regions with chemical or gaseous exposures

Sample Project

Let’s learn how to interface an IR sensor module with an Esp32 to detect objects using simple
Arduino code.

Materials Required

1. Esp32 board (e.g., Esp32 Dev Board).


2. USB cable (for connecting ESP32 to computer).
3. Breadboard.
4. IR sensor module
5. Jumper wires.

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

Open Arduino IDE.


Go to File > New to open a new sketch.
Enter the following code:

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
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Open the Serial Monitor by clicking the magnifying glass icon in the top-right corner of the
Arduino IDE or by going to Tools > Serial Monitor.
You should see the digital values being printed to the Serial Monitor.

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

Ensure all connections are secure and correct.

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

A DHT11(temperature and humidity sensor) is a low-cost digital device


used to measure the temperature and relative humidity of the surrounding
environment. They often use temperature sensors (like thermistors or
thermocouples) and humidity sensors (like capacitive or resistive types) to
detect environmental conditions.

It measures temperature from 0 to 50°C and humidity from 20% to 90%.

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

1. To measure the humidity of surroundings.


2. To measure the temperature

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

1. Esp32 board (e.g., Esp32 Dev Board).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. DHT11 temperature and humidity sensor.
5. Jumper wires.

Circuit

38
Place the Esp32 board and DHT11 sensor on the breadboard.Connect the component as follows:

DHT11 Sensor

VCC to 5V on the Esp32


GND to GND on the Esp32
Data pin to digital pin 23 on the Esp32

Sketch

Open Arduino IDE.


Go to Sketch > Include Library > Manage Libraries.
In the Library Manager, search for "DHT sensor library".
Install the "DHT sensor library" by Adafruit.
Go to File > New to open a new sketch.
Enter the following code:

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);
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Open the Serial Monitor by clicking the magnifying glass icon in the top-right corner of the
Arduino IDE or by going to Tools > Serial Monitor.
Observe the temperature and humidity readings being printed on the Serial Monitor.

Explana tion

DHT11 SENSOR

The data pin is connected to digital pin 2 on the Arduino to read the sensor data.

Code Explanation

The DHT library is used to handle the DHT11 sensor.


In the setup function, Serial.begin(9600) initializes serial communication at 9600 baud rate, and
dht.begin() initializes the DHT sensor.
In the loop function, the temperature and humidity are read and printed to the Serial Monitor
every two seconds.

40
Troubleshooting

Ensure all connections are secure and correct.


Verify that the Esp32 is properly connected to the computer.
Ensure the DHT11 sensor is functioning correctly.

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

1. To display sensor readings such as temperature, humidity, or other data.


2. To show device status, menus, and simple graphics in compact electronic projects.

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

1. Esp32 board (e.g., Esp32 Dev Board).


2. USB cable (for connecting Esp32 to computer).
3. Breadboard.
4. OLED Display.
5. Jumper wires.

Circuit

42
Place the Esp32 board and Display sensor on the breadboard.Connect the component as follows:

OLED DISPLAY

VCC to 3.3V on the ESP32


GND to GND on the ESP32
SDA to GPIO 21 on the ESP32 and SCL to GPIO 22 on the ESP32

Sketch

Open Arduino IDE.


Go to Sketch > Include Library > Manage Libraries.
In the Library Manager, search for "Adafruit SSD1306".
Install the "Adafruit SSD1306" by Adafruit.
Go to File > New to open a new sketch.
Enter the following code:

43
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 display(128, 64, &Wire);

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() {
}

Upload and Run the Sketch

Verify your sketch by clicking the checkmark icon (Verify/Compile).


If no errors, upload the sketch by clicking the right arrow icon (Upload).
Open the Serial Monitor by clicking the magnifying glass icon in the top-right corner of the
Arduino IDE or by going to Tools > Serial Monitor.
Observe the temperature and humidity readings being printed on the Serial Monitor.

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

You might also like