ESP32 Micro Controller
ESP32 Micro Controller
1 ESP32 MICROCONTOLLER:
3.1.2 Introduction:
SP32 is a low-cost System on Chip (SoC) Microcontroller from Espressif Systems, the
developers of the famous ESP8266 SoC. It is a successor to ESP8266 SoC and comes in both
single-core and dual-core variations of the Tensilica’s 32-bit Xtensa LX6 Microprocessor
with integrated Wi-Fi and Bluetooth.
The good thing about ESP32, like ESP8266 is its integrated RF components like Power
Amplifier, Low-Noise Receive Amplifier, Antenna Switch, Filters and RF Balun. This makes
designing hardware around ESP32 very easy as you require very few external components.
3.1.2 Specifications:
Single or Dual-Core 32-bit LX6 Microprocessor with clock frequency up to 240 MHz
34 Programmable GPIOs.
Cryptographic Hardware Acceleration for AES, Hash (SHA-2), RSA, ECC and RNG.
pulled
0 OK outputs PWM signal at boot, must be LOW to enter flashing mode
up
4 OK OK
13 OK OK
16 OK OK
17 OK OK
18 OK OK
19 OK OK
21 OK OK
22 OK OK
23 OK OK
25 OK OK
26 OK OK
27 OK OK
32 OK OK
33 OK OK
34 OK input only
35 OK input only
36 OK input only
39 OK input only
GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-up or pull-
down resistors. They can’t be used as outputs, so use these pins only as inputs:
GPIO 34
GPIO 35
GPIO 36
GPIO 39
GPIO 6 to GPIO 11 are exposed in some ESP32 development boards. However, these pins
are connected to the integrated SPI flash on the ESP-WROOM-32 chip and are not
recommended for other uses. So, don’t use these pins in your projects:
GPIO 6 (SCK/CLK)
GPIO 7 (SDO/SD0)
GPIO 8 (SDI/SD1)
GPIO 9 (SHD/SD2)
GPIO 10 (SWP/SD3)
GPIO 11 (CSC/CMD)
The ESP32 has 10 internal capacitive touch sensors. These can sense variations in anything
that holds an electrical charge, like the human skin. So, they can detect variations induced
when touching the GPIOs with a finger. These pins can be easily integrated into capacitive
pads and replace mechanical buttons. The capacitive touch pins can also be used to wake up
the ESP32 from deep sleep.
T0 (GPIO 4)
T1 (GPIO 0)
T2 (GPIO 2)
T3 (GPIO 15)
T4 (GPIO 13)
T5 (GPIO 12)
T6 (GPIO 14)
T7 (GPIO 27)
T8 (GPIO 33)
T9 (GPIO 32)
The ESP32 has 18 x 12 bits ADC input channels (while the ESP8266 only has 1x 10 bits
ADC). These are the GPIOs that can be used as ADC and respective channels:
ADC2_CH0 (GPIO 4)
ADC2_CH1 (GPIO 0)
ADC2_CH2 (GPIO 2)
Note: ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re
having trouble getting the value from an ADC2 GPIO, you may consider using an ADC1
GPIO instead. That should solve your problem.
The ADC input channels have a 12-bit resolution. This means that you can get analog
readings ranging from 0 to 4095, in which 0 corresponds to 0V and 4095 to 3.3V. You can
also set the resolution of your channels on the code and the ADC range.
The ESP32 ADC pins don’t have a linear behaviour. You’ll probably won’t be able to
distinguish between 0 and 0.1V, or between 3.2 and 3.3V. You need to keep that in mind when
using the ADC pins.
There are 2 x 8 bits DAC channels on the ESP32 to convert digital signals into analog voltage
signal outputs. These are the DAC channels:
DAC1 (GPIO25)
DAC2 (GPIO26)
RTC GPIOs
There is RTC GPIO support on the ESP32. The GPIOs routed to the RTC low-power
subsystem can be used when the ESP32 is in deep sleep. These RTC GPIOs can be used to
wake up the ESP32 from deep sleep when the Ultra Low Power (ULP) co-processor is
running. The following GPIOs can be used as an external wake up source.
RTC_GPIO0 (GPIO36)
RTC_GPIO3 (GPIO39)
RTC_GPIO4 (GPIO34)
RTC_GPIO5 (GPIO35)
RTC_GPIO6 (GPIO25)
RTC_GPIO7 (GPIO26)
RTC_GPIO8 (GPIO33)
RTC_GPIO9 (GPIO32)
RTC_GPIO10 (GPIO4)
RTC_GPIO11 (GPIO0)
RTC_GPIO12 (GPIO2)
RTC_GPIO13 (GPIO15)
RTC_GPIO14 (GPIO13)
RTC_GPIO15 (GPIO12)
RTC_GPIO16 (GPIO14)
RTC_GPIO17 (GPIO27)
PWM
The ESP32 LED PWM controller has 16 independent channels that can be configured
to generate PWM signals with different properties. All pins that can act as outputs can be
used as PWM pins (GPIOs 34 to 39 can’t generate PWM).
To set a PWM signal, you need to define these parameters in the code:
Signal’s frequency;
Duty cycle;
PWM channel;
Learn how to use ESP32 PWM with Arduino IDE: ESP32 PWM with Arduino IDE
I2C
The ESP32 has two I2C channels and any pin can be set as SDA or SCL. When using the
ESP32 with the Arduino IDE, the default I2C pins are:
GPIO 21 (SDA)
GPIO 22 (SCL)
3.1.4 Functions:
ESP32 has many applications when it comes to IoT. Here are just some of the IoT functions
the chip is used for:
Networking: The module’s Wi-Fi antenna and dual core enables embedded devices to
connect to routers and transmit data.
Data processing: Includes processing basic inputs from analog and digital sensors to
far more complex calculations with an RTOS or non-OS software development kit
(SDK). A non-OS SDK refers to one that is designed to run directly on the chip
without a full operating system supporting it.
P2P connectivity: Creates direct communication between different ESPs and other
devices using IoT P2P connectivity.
3.1.4 Applications:
3.2.1 Introduction:
You can either use the digital pin or the analog pin to do this. Simply power the module with
5V and you should notice the power LED on the module to glow and when no gas it detected
the output LED will remain turned off meaning the digital output pin will be 0V. Remember
that these sensors have to be kept on for pre-heating time (mentioned in features above)
before you can actually work with it. Now, introduce the sensor to the gas you want to detect
and you should see the output LED to go high along with the digital pin, if not use
the potentiometer until the output gets high. Now every time your sensor gets introduced to
this gas at this particular concentration the digital pin will go high (5V) else will remain low
(0V). You can also use the analog pin to achieve the same thing. Read the analog values (0-
5V) using a microcontroller, this value will be directly proportional to the concentration of
the gas to which the sensor detects. You can experiment with these values and check how the
sensor reacts to different concentration of gas and develop your program accordingly.
For Module
3 Digital You can also use this sensor to get digital output from this pin,
Out by setting a threshold value using the potentiometer.
4 Analog This pin outputs 0-5V analog voltage based on the intensity of
Out the gas.
3.3.1 Introduction:
The MQ-2 Gas sensor can detect or measure gasses like LPG, Alcohol, Propane,
Hydrogen, CO, and even methane. The module version of this sensor comes with a Digital
Pin which makes this sensor to operate even without a microcontroller and that comes in
handy when you are only trying to detect one particular gas. When it comes to measuring the
gas in ppm the analog pin has to be used, the analog pin also TTL driven and works on 5V
and hence can be used with most common microcontrollers. So if you are looking for a
sensor to detect or measure gasses like LPG, Alcohol, Propane, Hydrogen, CO and even
methane with or without a microcontroller, then this sensor might be the right choice for you.
Using an MQ sensor to detect a gas is very easy. You can either use the digital pin or the
analog pin to accomplish this. Simply power the module with 5V and you should notice the
power LED on the module to glow and when no gas it detected the output LED will remain
turned off meaning the digital output pin will be 0V. Remember that these sensors have to be
kept on for pre-heating time (mentioned in features above) before you can actually work with
it. Now, introduce the sensor to the gas you want to detect and you should see the output LED
to go high along with the digital pin, if not use the potentiometer until the output gets high.
Now every time your sensor gets introduced to this gas at this particular concentration the
digital pin will go high (5V) else will remain low (0V).
You can also use the analog pin to achieve the same thing. Read the analog values (0-
5V) using a microcontroller, this value will be directly proportional to the concentration of
the gas to which the sensor detects. You can experiment with these values and check how the
sensor reacts to different concentration of gas and develop your program accordingly. When it
comes to measuring or detecting a particular Gas, the MQ series Gas sensors are the most
commonly used ones. These sensors can either be purchased as a module or as just the sensor
alone. If you are trying to only detect (not measuring ppm) the presence of a gas then you can
buy it as a module since it comes with an op-amp comparator and a digital out pin. But if you
planning to measure the ppm of a gas, it is recommended to buy the sensor alone (without
module).
Fig MQ2 Smoke Sensor
For Module
1 Vcc This pin powers the module, typically the operating voltage is +5V
3 Digital You can also use this sensor to get digital output from this pin, by setting a
Out threshold value using the potentiometer
4 Analog This pin outputs 0-5V analog voltage based on the intensity of the gas
Out
3.3.3 Features:
Can be used to Measure or detect LPG, Alcohol, Propane, Hydrogen, CO and even
methane
Analog output voltage: 0V to 5V
3.4 MQ 7 CO Sensor
3.4.1 Introduction:
The sensitive material of the MQ-7 carbon monoxide gas sensor is tin dioxide (SnO2).
This is a reactive material to carbon monoxide and the basic working of this sensor depends
on the heating process of this sensing element. Carbon monoxide (CO) is not usually present
in fresh air therefore, the sensor does not provide any signal until it senses any leakage or
danger. Just like other MQ sensors, this one also has a sensitive element made of ceramic that
has a layer outside it. This layer consists of tin dioxide (SnO2) and has the feature of
absorbing the target gas. In the case of MQ7, the tin dioxide absorbs the carbon monoxide
and indicates its presence as soon as it comes in contact. The MQ-7 has electrodes that
facilitate electrical contact with the sensitive material. Usually, it is made with the gold for
the best performance. These allow the measurement of the electrical resistance. There is a
coil in the heart of this sensor. This is made of alloy like Ni-Cr and provides heat to the
sensitive layer to enhance its reaction and sensitivity. The censor has a tubular structure of
ceramic material such as alumina (Al2O3) and it provides support and strength to other
components so that they may be at their place. There is a network of double stainless steel
mesh that performs two functions:
1. It allows the gases to pass through it and retains the presence of any unwanted
particles outside the network.
2. It protects the internal structure because that is very reactive and delicate therefore,
with the help of this network, all the internal structure remains in its place.
Fig: MQ 7 CO Sensor
3.4.2 Pinout:
Pin Pin
Description
Number Name
Analog It is the analogue output pin. It provides the analogue voltage based
4
Out on the concentration of the gas
Table MQ 7 Pinout
3.4.3. Specifications:
Operating voltage: DC 5 V.
The analog output voltage, the higher the concentration the higher the voltage.
The carbon monoxide detection with better sensitivity.
With a long service life and reliable stability.
Rapid response and recovery characteristics.
Range: 10 to 1000 ppm
3.5.1 Introduction:
Gravity: Analog TDS Sensor/Meter for Arduino is an Arduino-compatible TDS Meter Kit for
measuring TDS value of the water, to reflect the cleanliness of the water. TDS meter can be
applied to domestic water, hydroponic and other fields of water quality testing. You may also
check Liquid Senser Selection Guide to get better familiar with our liquid sensor series.
TDS (Total Dissolved Solids) indicates that how many milligrams of soluble solids dissolved
in one liter of water. In general, the higher the TDS value, the more soluble solids dissolved
in water, and the less clean the water is. Therefore, the TDS value can be used as one of the
references for reflecting the cleanliness of water.
TDS pen is widely used equipment to measure TDS value. The price is affordable,
and it is easy to use, but it is not able to transmit data to the control system for online
monitoring to do some water quality analysis. The professional instrument has high accuracy
and can send data to the control system, but the price is expensive for ordinary people. To this
end, we have launched an analog TDS sensor kit that is compatible with Arduino, plug, and
play, easy to use. Matching with Arduino controller, you can build a TDS detector easily to
measure the TDS value of liquid. This DF-Robot Gravity: Analog TDS Sensor/ Meter for
Arduino supports 3.3 ~ 5.5V wide voltage input, and 0 ~ 2.3V analog voltage output, which
makes it compatible with a 5V or 3.3V control system or board. The excitation source is an
AC signal, which can effectively prevent the probe from polarization and prolong the life of
the probe, meanwhile, increase the stability of the output signal. The TDS probe is
waterproof, it can be immersed in water for a long-time measurement.
Fig TDS Sensor
3.4.2 Pinout:
3.4.3 Specifications:
3.5 DC Motor
12V 60 RPM DC motor is a type of direct current (DC) motor that operates on a 12-volt
power supply and has a rated speed of 60 revolutions per minute (RPM). Here's an
introduction to it:
Basic Overview:
Voltage: 12V refers to the operating voltage required to power the motor. It is
commonly used in applications where a 12V DC power source is available, like in
battery-powered systems.
Speed: 60 RPM means the motor completes 60 full rotations per minute when
powered under ideal conditions. This is considered a relatively slow speed, suitable
for applications where torque (rotational force) is more important than high speed.
Working Principle:
The motor has a commutator that reverses the direction of current flow in the coils,
ensuring continuous rotation in one direction.
Applications:
Electric Vehicles (EVs): Small-scale electric vehicles and scooters use these motors
for low-speed operation.
Small Appliances: Fans, small conveyor belts, or other devices that need moderate
speed for operation.
Characteristics:
Low Speed, High Torque: At 60 RPM, the motor is designed for tasks that require
controlled speed and more torque, rather than speed.
Efficiency: Generally, the efficiency of DC motors depends on the design, but 12V
motors are common because they can be powered by easily accessible 12V batteries
or power supplies.
Controlling Speed: The speed of a DC motor can be varied by adjusting the voltage,
or by using pulse-width modulation (PWM) methods.
Advantages:
Simplicity: DC motors are easy to control, especially for simple tasks like controlling
speed and direction.
Low Power Consumption: 12V motors generally consume less power compared to
higher voltage motors.
Compact: These motors are usually smaller in size, making them ideal for compact
and portable applications.
Limitations:
Limited Speed Range: Compared to higher RPM motors, 60 RPM motors are
relatively slow, making them unsuitable for applications requiring high-speed
rotation.
Wear and Tear: The brushes and commutators in DC motors can wear out over time,
requiring maintenance.
3.5.2 Features
4 stage gearbox for optimum high torque operation, which has breaking torque of 100
kgcm.
Stall torque of motor is higher than 200 kgcm, but not recommedned to exceed
75kgcm.
Shaft can be coupled using CNC coupling 6 mm or using fixed coupling as per
requirement
300 gm weight
Supply Voltage: 12 V DC
Battery:
Introduction:
Lead-Acid Battery: This type of battery uses lead plates and sulfuric acid to store and
release electrical energy. It's known for being relatively inexpensive and reliable,
especially in applications where space and weight aren't critical factors.
Voltage (12V):
The 12V rating indicates the nominal voltage output of the battery when it's fully
charged. This voltage is commonly used in many applications, including automotive,
solar power systems, backup power, and small electronics.
12V is a standard voltage for many lead-acid batteries, especially those used in
vehicles, RVs, and other systems that need a moderate amount of power.
Capacity (1.3Ah):
1.3Ah stands for 1.3 amp-hours, which is the battery's capacity. This means that the
battery can supply 1.3 amps of current for one hour, or, for example, 0.65 amps for
two hours, or 2.6 amps for 30 minutes.
Amp-Hours (Ah) is a unit that measures the amount of charge the battery can deliver
over a specified period. The higher the Ah rating, the longer the battery can power a
device before needing to be recharged.
Common Applications:
UPS (Uninterruptible Power Supply): Often used as backup power for small
electronics or computers during short power outages.
Solar Power Systems: Used to store energy in off-grid or backup solar systems.
Electric Vehicles: Small electric scooters, wheelchairs, or toys may use a 12V 1.3Ah
battery for power.
Security Systems: These batteries can be used in alarm systems, cameras, and other
low-power devices to provide backup power.
Lighting Systems: Solar-powered garden lights or emergency lighting.
Charging: Lead-acid batteries require specific charging methods to maintain their life.
Overcharging or undercharging can damage the battery. Typically, a 12V lead-acid
charger is used to recharge these batteries.
Float Charging: For prolonged use, the battery may be kept at a full charge using a
low-maintenance float charger that maintains the charge without overcharging it.
Advantages:
Reliability: They have been used for many years and are known for their robustness
and reliability in various applications.
Availability: 12V lead-acid batteries are widely available and used in a variety of
products.
Limitations:
Weight and Size: Lead-acid batteries are typically heavier and bulkier compared to
newer battery technologies like lithium-ion, which can be a disadvantage in
applications where space or weight is a concern.
Self-Discharge Rate: Lead-acid batteries tend to have a higher self-discharge rate than
lithium-ion batteries, meaning they lose charge more quickly when not in use.
Discharge Characteristics:
Lead-acid batteries generally discharge at a relatively steady voltage until they are
nearing empty. However, they also suffer from voltage sag when heavily loaded,
which may affect the performance of devices drawing power from the battery.
Safety:
Ventilation: Lead-acid batteries can produce hydrogen gas during charging, so they
should be used in well-ventilated areas to prevent the risk of an explosion.
Overheating: Prolonged overcharging can cause excessive heat and even damage the
battery, so using the proper charging method is essential.
3.6.2 Features
Charging Voltage: 13.5V to 14.5V (full charge), 13.2V to 13.5V (float charge)
Applications: Backup power, solar systems, security systems, electric vehicles, toys
Safety: Emits hydrogen gas during charging (ventilation required), sealed versions are
maintenance-free
All relays contain a sensing unit, the electric coil, which is powered by AC or DC
current. When the applied current or voltage exceeds a threshold value, the coil activates the
armature, which operates either to close the open contacts or to open the closed contacts.
When a power is supplied to the coil, it generates a magnetic force that actuates the switch
mechanism. The magnetic force is, in effect, relaying the action from one circuit to another.
The first circuit is called the control circuit; the second is called the load circuit.
Relays have the exact working of a switch. So, the same concept is also applied. A relay is
said to switch one or more poles. Each pole has contacts that can be thrown in mainly three
ways. They are
Normally Open Contact (NO) – NO contact is also called a make contact. It closes the
circuit when the relay is activated. It disconnects the circuit when the relay is inactive.
Normally Closed Contact (NC) – NC contact is also known as break contact. This is
opposite to the NO contact. When the relay is activated, the circuit disconnects. When
the relay is deactivated, the circuit connects.
Change-over (CO) / Double-throw (DT) Contacts – This type of contacts are used to
control two types of circuits. They are used to control a NO contact and also a NC
contact with a common terminal. According to their type they are called by the
names break before make and make before break contacts.
The LM2596 is known for its high current rating of 3A. It is available in many
versions with fixed output voltage like 3.3V, 5V and 12V. But, the most famous one is the
LM2596-ADJ which has variable output voltage. The IC is basically a buck converter that
operates on 150 KHz switching frequency, it takes in an input voltage and uses the internal
switching circuit to regulate a desired output voltage. It has high efficiency and in-built
Thermal shutdown and current limit functionalities. The LM2596 is very easy to use because
it requires very minimum number of components. The unregulated voltage is given to pin 1
(Vin) across a filter capacitor to reduce input noise. The ON/OFF or enable pin (pin 5) should
be connected to ground to enable the IC. If made high the IC will enter shutdown mode and
prevent leakage current. This feature will be useful to save input power when operating via
battery. The feedback pin is the important pin which sets the output voltage. It sense the
output voltage and based on the value of this output voltage the switching frequency of the
internal switch is adjusted to provide the desired output voltage. Finally the output voltage is
obtained through pin 2 through an LC filter. The complete circuit diagram is given below, you
can often find these circuit in the LM2596 DC Converter module.
Fig 3.9 LM2596 DC to DC Converter
The source code for the IDE is released under the GNU General Public License,
version 2. The Arduino IDE supports the languages C and C++ using special rules of code
structuring. The Arduino IDE supplies a software library from the Wiring project, which
provides many common input and output procedures. User-written code only requires two
basic functions, for starting the sketch and the main program loop, that are compiled and
linked with a program stub main() into an executable cyclic executive program with the
GNU toolchain, also included with the IDE distribution. The Arduino IDE employs the
program avrdude to convert the executable code into a text file in hexadecimal encoding that
is loaded into the Arduino board by a loader program in the board's firmware. By default,
avrdude is used as the uploading tool to flash the user code onto official Arduino boards.
Arduino provides a standard form factor that breaks the functions of the micro-
controller into a more accessible package. With Arduino, one can get to know the basics of
micro-controllers and sensors very quickly and can start building prototype with very little
investment. After learning about the main parts of the Arduino UNO board, we are ready to
learn how to set up the Arduino IDE. Once we learn this, we will be ready to upload our
program on the Arduino board.
Step 1 − First you must have your Arduino board (you can choose your favorite board) and a
USB cable. In case you use Arduino UNO, Arduino Duemilanove, Nano, Arduino Mega
2560, or Diecimila, you will need a standard USB cable (A plug to B plug), the kind you
would connect to a USB printer as shown in the following image.
Connect the Arduino board to your computer using the USB cable. The green power LED
(labeled PWR) should glow.
Here, we are selecting just one of the examples with the name Blink. It turns the LED on
and off with some time delay. You can select any other example from the list.
Here, we have selected Arduino Uno board according to our tutorial, but you must select the
name matching the board that you are using.
F − Serial monitor used to receive serial data from the board and send the serial data to the
board.
Now, simply click the "Upload" button in the environment. Wait a few seconds; you will see
the RX and TX LEDs on the board, flashing. If the upload is successful, the message "Done
uploading" will appear in the status bar.
Note − If you have an Arduino Mini, NG, or other board, you need to press the reset button
physically on the board, immediately before clicking the upload button on the Arduino
Software.