Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 49

ESP32

• ESP32 is created by Espressif Systems with a series of SoC (System on a


Chip) and modules which are low cost with low power consumption.
• ESP32 is far superior than Arduino UNO and an ESP8266
The specs listed below belong to the ESP32 WROOM 32 variant.
• Integrated Crystal− 40 MHz (Max operating frequency )
• Module Interfaces− UART, SPI, I2C, PWM, ADC, DAC, GPIO, capacitive touch
sensor
• Integrated SPI flash− 4 MB
• ROM− 448 KB (for booting and core functions)
• SRAM− 520 KB( for data and instructions)
• Integrated Connectivity Protocols− WiFi, Bluetooth, BLE
• On−chip sensor− Hall sensor
• Operating temperature range− −40 − 85 degrees Celsius
• Operating Voltage− 3.3V
• Operating Current− 80 mA (average)
• It has two cores, allowing parallel processing, which is a further add-on.
The ESP32 development board has a total of 30 pins that connect it to the outside world
ESP32 Peripherals and I/O
15 ADC channels 15 channels of 12-bit SAR ADC’s.

2 UART interfaces 2 UART interfaces.

25 channels of PWM pins for dimming LEDs or controlling


25 PWM outputs motors.
2 DAC channels 8-bit DACs to produce true analog voltages.
3 SPI & 1 I2C interfa There are 3 SPI and 1 I2C interfaces to hook up all sorts of
ces sensors and peripherals.
9 Touch Pads 9 GPIOs feature capacitive touch sensing.
D34 GPIO34 Input only GPIO, cannot be configured as output

D35 GPIO35 Input only GPIO, cannot be configured as output

VP GPIO36 Input only GPIO, cannot be configured as output

VN GPIO39 Input only GPIO, cannot be configured as output

• The ESP32 has fifteen 12-bit ADC input channels. These are GPIOs that can
be used to convert the analog voltage on the pin to a digital number.
• The ADC on the ESP32 is a 12-bit ADC meaning it has the ability to detect
4096 (212) discrete analog levels.
• In other words, it will map input voltages between 0 and the operating
voltage 3.3V into integer values between 0 and 4095.
• For example, this yields a resolution between readings of: 3.3 volts / 4096
units or, 0.0008 volts (0.8 mV) per unit.
DAC Pins
• The ESP32 features two 8-bit DAC channels that can be used to convert
digital signals into analog voltages. It can be used as a “digital
potentiometer” to control analog devices.
• This dual-DAC on the ESP32 has 8-bit resolution, which means that values
between 0 and 256 will produce an analog voltage somewhere between 0
and 3.3V.
Capacitive touch GPIOs
• 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.
ESP32 function block diagram.
Wireless Connectivity:

• The ESP32 SoC chip has WiFi connectivity, being compatible with 802.11 b / g


/ n in the 2.4GHz band, reaching speeds of up to 150 Mbits/s. It also includes
Bluetooth communication compatible with Bluetooth v4.2 and Bluetooth Low
Energy (BLE).
• The radio block is closely tied to the wireless communication modules . In
fact, this is the one that actually transmits and receives the information.
• That is, it takes the digital data from the WiFi and Bluetooth modules and
converts them into electromagnetic signals that travel through the air to
communicate with your mobile phone or your router .
• It also performs the reverse operation: translate the electromagnetic waves
generated by other devices into digital data that the WiFi and Bluetooth
modules are capable of interpreting.
Features of the ESP32 include the following:

• Processors:
• CPU: Xtensa dual-core (or single-core) 32-bit microprocessor,
operating at 160 or 240 MHz and performing at up to 600 DMIPS
• Ultra low power (ULP) co-processor
Controlling LED
• It comes with a built-in blue LED that is internally connected to GPIO 2.
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}
ESP32 Door Sensor

• The door sensor is also known as entry sensor, contact sensor or


window sensor.
The ESP32 input is low when magnet is near to the reed switch.
The ESP32 input is High when magnet is far from the reed switch.
So it gives LOW when door is closed
HIGH when door is opened.
void setup() {
// put your setup code here, to run once:
pinMode(16,INPUT_PULLUP);
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
int sw_status = digitalRead(16);
if(sw_status == HIGH)
Serial.println("OPEN");
else
Serial.println("CLOSED");
delay(1000);
}
Soil Moisture sensor

Soil Moisture sensor ESP32 pin


VCC 3.3V
A0 GPIO34
D0 NC
GND GND

The soil moisture sensor provides a digital as well as a analog output.


We will be using the analog output
• When there is more water,
The soil will conduct more electricity ,
which means that there will be less resistance.
Therefore, the moisture level will be higher.
• When there is less water,
Dry soil conducts electricity poorly,
which means that there will be more resistance.
Therefore, the moisture level will be lower.
Soil Moisture sensor
void setup(){
Serial.begin(115200);
}
void loop(){
int soilmoisture;
soilmoisture = analogRead(34);
Serial.println(soilmoisture);
delay(1000);
}
ESP32 Hardware PWM

• The ESP32 PWM hardware has 16 different channels, not pins.


• We can assign any of these channels to any GPIO pin that we want.
But it has to have an output driver or should be capable of operating
as an output pin(except for 4 pins).
• The ESP32 PWM controller has 8 high-speed channels and 8 low-
speed channels, which gives us a total of 16 channels.
• They’re divided into two groups depending on the speed.
• For each group, there are 4 timers / 8 channels. This means every two
channels share the same timer. Therefore, we can’t independently
control the PWM frequency of each couple of channels.
ESP32 PWM hardware diagram from the
datasheet
• Assign that PWM Ch to the selected GPIO pin using this function.
ledcAttachPin(GPIO_pin, PWM_Ch);
• Decide on the required PWM Resolution for the selected channel
[ 1Bit – 16Bits ]. Setting the resolution to 8Bits, gives you a duty
cycle range [0 – 255]. While setting it to 10Bits, gives you a range of
[ 0 – 1023 ]. 
• Decide on the required PWM Frequency for the selected channel.
• Configure this PWM Channel with the selected frequency &
resolution using this function.
ledcSetup(PWM_Ch, PWM_Freq, PWM_Res);
• We can control this PWM pin by changing the duty cycle using this
function down below.
ledcWrite(PWM_Ch, DutyCycle);
Controlling LED Brightness
  
#define LED_GPIO   5
#define PWM1_Ch    0
#define PWM1_Res   8 while(PWM1_DutyCycle > 0)
#define PWM1_Freq  1000
 int PWM1_DutyCycle = 0;   {
 void setup()     ledcWrite(PWM1_Ch, PWM1_DutyCycle--);
{     delay(10);
  ledcAttachPin(LED_GPIO, PWM1_Ch);
  ledcSetup(PWM1_Ch, PWM1_Freq,   }
PWM1_Res); }
}
 void loop()
{
  while(PWM1_DutyCycle < 255)
  {
    ledcWrite(PWM1_Ch, PWM1_DutyCycle++);
    delay(10);
  }
 
Firebase 
• Google Firebase is a Google-backed application development software used
for creating, managing, and modifying data generated from any android/IOS
application, web services, IoT sensors & Hardware. 
• The Firebase Realtime Database is a cloud-hosted database.
• When we build cross-platform apps with our iOS, Android, and JavaScript
SDKs, all of your clients share one Realtime Database instance and
automatically receive updates with the newest data.
• Firebase is a Backend As A Service or BAAS, i.e., it is a NoSQL data store that
is in the cloud that your clients can access directly in realtime. Firebase has its
own Realtime Database, Cloud Storage, Authentication, Hosting and many other
services.
• Unlike SQL there is no schema for the database, no tables, no columns, its
just a combination of key/value pairs. Firebase is based on a data structure
used by the NoSQL database is vastly different from those used in a relational
database. Some operations are faster in NoSQL than relational databases
like MySQL.
• We will create a Firebase project with a real time database, use the ESP32 to
store and read data from the database. The ESP32 can interact with the
database from anywhere in the world as long as it is connected to the internet.
• This means that you can have two ESP32 boards in different networks, with
one board storing data and the other board reading the most recent data, for
example.
Setting up Google Firebase Console
1. type https://firebase.google.com/ in your browser search
tab and press enter.
2. This will open the Firebase main page. Click ‘Go to Console’
as highlighted in the red rectangular box.
Click the button ‘Create a Project’ as shown below.
• Step 1: Write the name of your project. Remember to tick the
Firebase term agreement. Now click ‘Continue’.
• Step 2: Now enable ‘Google analytics for this project’ by
swiping the sliding button. Click ‘Continue’ to proceed
further.
• Step 3: After choosing your location and ticking the required
boxes click ‘Create Project’.
Obtaining Authorization key and Firebase host

• go to the settings icon and click ‘Project Settings’.


• Go to ‘Service Accounts’ and click ‘Generate new private key’.
• Then go to ‘Database secrets.’ You will be able to view a secret key associated with your
project. This is the unique authorization key that you will save and use later on in the
program code. Keep it a secret and do not share it with anyone or your project security
will be compromised.
• Next, Under the Build tab go to ‘Realtime Database.’ Then
click ‘Create Database.’
• After setting your location, check ‘Start in Test mode’ and
then click the Enable button.
• Include Libraries
Include WiFi.h which help in establishing the connection between the ESP32 module to a wireless
network. 
Include FirebaseESP32.h for the Firebase functionality.
#include <WiFi.h>
#include "FirebaseESP32.h“
• Defining Firebase credentials
Define the Google firebase host and authorization key which we accessed and saved previously.

#define FIREBASE_HOST "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" //Do not include https://


#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxx“
Example:
#define FIREBASE_HOST "edgedht-70e41-default-rtdb.firebaseio.com/" //Do not include https://
#define FIREBASE_AUTH "P6DosHJfB7yGN5U2a5YPQiWAWPj19lJsSWZOJbyn"
• Defining Network credentials
• Define the network credentials One for the SSID and the other for
the password. These will be the network credentials which will be
used to connect to our wireless network.

#define WIFI_SSID "xxxxxxxxxx"


#define WIFI_PASSWORD "xxxxxxxxxx“

//Define Firebase Data object


FirebaseData firebaseData;
In the setup() we need to write,
WiFi.begin(WIFI_SSID, WIFI_PASSWORD)
Initializes the WiFi library's network settings and provides the current status.
ssid: the SSID (Service Set Identifier) is the name of the WiFi network you want to connect to.
pass: WPA encrypted networks use a password in the form of a string for security.
Returns
•WL_CONNECTED when connected to a network
•WL_IDLE_STATUS when not connected to a network, but powered on

while (WiFi.status() != WL_CONNECTED)


{
Serial.print(".");
delay(300);
}
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
WiFi.localIP());- Gets the WiFi shield's IP address
• Connect to Firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
• Set Float value to database
Firebase.setFloat(firebaseData,tag,val);
• Set integer value to database
Firebase.setInt(firebaseData,tag,val);
• Set String value to database
Firebase.setString(firebaseData,tag,val);
void setup() {
#include <WiFi.h> Serial.begin(115200);
#include "FirebaseESP32.h" WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#define FIREBASE_HOST Serial.print("Connecting to Wi-Fi");
"xxxxxxxxxxxxxxxxxxxxxxxxxxxx" //Do while (WiFi.status() != WL_CONNECTED)
not include https:// {
#define FIREBASE_AUTH Serial.print(".");
"xxxxxxxxxxxxxxxxxxxxxxxx" delay(300);
#define WIFI_SSID "xxxxxxxxxx" }
#define WIFI_PASSWORD "xxxxxxxxxx" Serial.println();
Serial.print("Connected with IP: ");
//Define Firebase Data object
Serial.println(WiFi.localIP());
FirebaseData firebaseData; Serial.println();
Firebase.begin(FIREBASE_HOST,
float val = 0.0; FIREBASE_AUTH);
String tag = Firebase.reconnectWiFi(true);
"IOTLAB/My_App/Sensor_Value"; }
void loop() {
FirebaseWrite();
Serial.println(val);
val = val + 1.54;
if(val > 10.0) val = 0.0;
delay(1000);
}
void FirebaseWrite(){
Firebase.setFloat(firebaseData,tag,val);

}
Get the Values from Firebase
Steps:
1. call getInt() function
Syntax: Firebase.getInt (firebaseData,"IOTLAB/LEDCONTROL")
•These functions return a boolean value indicating the success of the operation, if the data
types matched between request and response.
2. Then, check if the data type is an integer with firebaseData.dataType()
3. Finally, the firebaseData.intData() gets the value stored in that node.
EX:
if ( Firebase.getInt(firebaseData,"IOTLAB/LEDCONTROL"))
{
if (firebaseData.dataType()=="int")
{
finalCmd = firebaseData.intData();
}
}
#include <WiFi.h> //Connecting to Wi-Fi network
#include "FirebaseESP32.h" WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
#define FIREBASE_HOST “xxxxxxxx" //Do not
include https:// in FIREBASE_HOST while (WiFi.status() != WL_CONNECTED)
{
#define FIREBASE_AUTH “xxxxxxxx"
Serial.print(".");
#define WIFI_SSID "Bhargavi" delay(300);
#define WIFI_PASSWORD “xxxxx" }
//Define Firebase Data object Serial.println();
FirebaseData firebaseData; Serial.print("Connected with IP: ");
int delayTime = 2000; Serial.println(WiFi.localIP());
Serial.println();
void setup() {
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
pinMode(2,OUTPUT);
Serial.begin(115200); }
void loop() { if(finalCmd == 1)
// put your main code here, to run repeatedly: {
digitalWrite(2,HIGH);
Serial.println("LED ON");
int finalCmd;
Firebase.setString(firebaseData,"IOTLAB/LED STATUS","LED
if ( ON");
}
Firebase.getInt(firebaseData,"IOTLAB/LEDCON else
TROL")) {
{ digitalWrite(2,LOW);
Serial.println("LED OFF");
if (firebaseData.dataType()=="int")
Firebase.setString(firebaseData,"IOTLAB/LED
{ STATUS","LED OFF");
finalCmd = firebaseData.intData(); }
}
delay(2000);
} }
#include "SSD1306.h" void setup() {
SSD1306 display(0x3c,21,22); pinMode(trigPin, OUTPUT); // Sets the
// defines pins numbers trigPin as an Output
const int trigPin = 25; pinMode(echoPin, INPUT); // Sets the
echoPin as an Input
const int echoPin = 26;
Serial.begin(115200); // Starts the
// defines variables serial communication
long duration; OLEDInit();
int distance; }
void OLEDInit();
void OLEDUpdate();
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
void loop() { Serial.println(distance);
// Clears the trigPin OLEDUpdate();
digitalWrite(trigPin, LOW);
delay(1000);
delayMicroseconds(2);
}
// Sets the trigPin on HIGH state for 10 micro seconds void OLEDInit(){
digitalWrite(trigPin, HIGH); display.init();
delayMicroseconds(10);
digitalWrite(trigPin, LOW); display.setFont(ArialMT_Plain_24);
}
// Reads the echoPin, returns the sound wave travel void OLEDUpdate(){
time in microseconds
String d = String(distance) + " cm";
duration = pulseIn(echoPin, HIGH);
display.clear();
// Calculating the distance display.drawString(30,0,d);
distance= duration*0.034/2; display.display();
}
Ultrasonic Sensor -Firebase

#include <WiFi.h> void setup() {


#include "FirebaseESP32.h" pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
#define FIREBASE_HOST "esp32-led-control-c72eb-default- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
rtdb.firebaseio.com" Serial.begin(115200); // Starts the serial communication
#define FIREBASE_AUTH
"CEhXuApfcJvstD9sFH1Ueruy7yyxg4PkIdzlMi1p"
//Connecting to Wi-Fi network
#define WIFI_SSID "Bhargavi"
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#define WIFI_PASSWORD "bxxxxxxx"
Serial.print("Connecting to Wi-Fi");
//Define Firebase Data object
while (WiFi.status() != WL_CONNECTED)
FirebaseData firebaseData;
int delayTime = 2000; {
// defines pins numbers Serial.print(".");
const int trigPin = 25; delay(300);
const int echoPin = 26; }
Serial.println();
// defines variables Serial.print("Connected with IP: ");
long duration; Serial.println(WiFi.localIP());
int distance; Serial.println();
String tag = "IOTLAB/Range_Meter/Range"; Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void loop() {
// Calculating the distance
// Clears the trigPin
distance= duration*0.034/2;
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Prints the distance on the Serial Monitor
Serial.print("Distance: ");
// Sets the trigPin on HIGH state for 10 micro Serial.println(distance);
seconds
digitalWrite(trigPin, HIGH); Firebase.setInt(firebaseData,tag,distance);

delayMicroseconds(10);
delay(1000);
digitalWrite(trigPin, LOW); }

// Reads the echoPin, returns the sound wave


travel time in microseconds
duration = pulseIn(echoPin, HIGH);

You might also like