0% found this document useful (0 votes)
17 views108 pages

Unit 5 IPC

Uploaded by

riot id
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)
17 views108 pages

Unit 5 IPC

Uploaded by

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

Arduino Interfacing- U5

19BTCS303-Introduction to Microprocessors & Chips

Prasenjeet Damodar Patil

Associate Professor
Department of Computer Science & Engg.
MIT, ADT, University, Pune, Maharashtra

November 11, 2022

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 1 / 103


Table of Contents
1 Analog & Digital sensors to Arduino
Reading Analog input
Voltage divider-Potentiometer
Temperature Sensor - LM35
Reading Digital input
Ultrasonic Sensor
Passive Infrared Sensor

2 Timers, Analog comparators and hardware interrupts


Interrupts
Timer/Counters
Analog Comparator
3 Communication Interfaces
UART/Serial communication
I2C
Serial Peripheral Interface (SPI)
4 Basics of wireless networking
NodeMCU ESP32 & ESP8266

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 2 / 103


Analog & Digital sensors to Arduino

Analog & Digital sensors to Arduino

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 3 / 103


Analog & Digital sensors to Arduino Reading Analog input

Introduction to Analog Sensing


Sensors are those electronic components which convert physical data into elec-
tronic data.
This data is in analog format and is fed to the microcontroller on the Arduino board.
The microcontroller has inbuilt ADCs (Analog-to-Digital Converter) which processes
this data and converts it into digital format.
The things that can be measured in the analog domain are voltage, current, resis-
tance, temperature, light etc.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 4 / 103


Analog & Digital sensors to Arduino Reading Analog input

Reading Analog data

The ATmega controllers used for the Arduino contain an onboard 6 channel (8
channels on the Mini and Nano, 16 on the Mega) analog-to-digital (A/D) converter.
The analogRead() and analogWrite() are the two functions which is used to deal
with the variable value like voltages as same as sensors values.
The things that can be measured in the analog domain are voltage, current, resis-
tance, temperature, light etc.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 5 / 103


Analog & Digital sensors to Arduino Reading Analog input

Reading Analog data

The Arduino has a 10-bit ADC by default, which means that analog signal can be
represented in integers numbers from 0 to 1023 (210 = 1024). While the main
function of the analog pins for most Arduino users is to read analog sensors, the
analog pins also have all the functionality of general purpose input/output (GPIO)
pins (the same as digital pins 0 - 13).
Consequently, if a user needs more general purpose input output pins, and all the
analog pins are not in use, the analog pins may be used for GPIO
The analogRead() returns the value from 0 to 1023, with 0 representing 0 volts,
and 1023 representing 5 volts
In normal analogRead() uses the operating voltage of the board, as a reference
voltage.
For more popular Arduino boards such as the Uno, Mega boards, the operating
voltage of 5V.
So, therefore the reference voltage = operative voltage= 5V, & each unit returned
Ref .Voltage
by analogRead() is valued at 1024
= 0.00488V = 4.88mV

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 6 / 103


Analog & Digital sensors to Arduino Reading Analog input

Simple Analog Sensing

Goto-File-examples-01-Basics- AnalogReadSerial
1
2 // the setup routine runs once when you press
reset:
3 void setup() {
4 // initialize serial communication at 9600
bits per second:
5 Serial.begin(9600);
6 }
7
8 // the loop routine runs over and over again
forever:
9 void loop() {
10 // read the input on analog pin 0:
11 int sensorValue = analogRead(A0);
12 // print out the value you read:
13 Serial.println(sensorValue);
14 delay(1); // delay in between reads
for stability
15 }

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 7 / 103


Analog & Digital sensors to Arduino Reading Analog input

Voltage divider-Potentiometer

A potentiometer is defined as a 3 terminal variable resistor in which the resistance


is varied manually to control the current flow

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 8 / 103


Analog & Digital sensors to Arduino Reading Analog input

Voltage divider-Potentiometer

A potentiometer is defined as a 3 terminal variable resistor in which the resistance


is varied manually to control the current flow

R2
Vout = Vcc x
R1 + R2

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 9 / 103


Analog & Digital sensors to Arduino Reading Analog input

Simple Analog Sensing

The output voltage Vout(5V max) from the potentiometer when read by Arduino
will be converted in the range of 0 to 1023.

The output voltage can be scaled down in the range of 0 and 255. Where “0” value,
will corresponds to 0 volts and 255 will corresponds to 5 volts.
The read values from the potentiometer can be calculated using readvalues =
(255/1024)

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 10 / 103


Analog & Digital sensors to Arduino Reading Analog input

Temperature sensors

There are a wide variety of temperature sensors compatible with Arduino, ESP32,
ESP8266 and other development boards
6 widely used temperature sensors: DHT11, DHT22, LM35, DS18B20, BME280
and BMP180.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 11 / 103


Analog & Digital sensors to Arduino Reading Analog input

LMxx Temperature sensors

The LM35, LM335 and LM34 are linear temperature sensors that output a voltage
proportional to the temperature value.

The LM35 outputs 10 mV per degrees Celsius rise in temperature. In a similar way,
the LM335 outputs 10 mV per degrees Kelvin rise in temperature and the LM34
outputs 10 mV per degrees Fahrenheit rise in temperature.
For example, if the LM35 outputs a voltage of 345 mV, that means we have a
temperature value of 34.50 C.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 12 / 103


Analog & Digital sensors to Arduino Reading Analog input

LM35
LM35 is a temperature measuring device having an analog output voltage propor-
tional to the temperature.
It provides output voltage in Centigrade (Celsius)and does not require any external
calibration circuitry.
The sensitivity of LM35 is 10 mV/degree Celsius. As temperature increases, output
voltage also increases. E.g. 250 mV means 250 C.
It is a 3-terminal sensor used to measure surrounding temperature ranging from
−550 C to 1500 C.
LM35 gives temperature output which is more precise than thermistor output.

VCC: Supply Voltage (4V to 30V)


Out: It gives analog output voltage which is proportional to the temperature (in
degree Celsius).
GND: Ground
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 13 / 103
Analog & Digital sensors to Arduino Reading Analog input

LM35 - features

Calibrated Directly in Celsius (Centigrade)


Linear +10 − mV /0 C Scale Factor
50 C Ensured Accuracy (at 250 C)
Rated for Full −550 C to 1500 C Range
Suitable for Remote Applications
Low-Cost
Operates from 4V to 30V
Less than 60µ A Current Drain
Low Self-Heating, 0.080 C in Still Air
10
Non-Linearity Only ± C Typical
4
Low-Impedance Output, 0.1Ω for 1-mA Load

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 14 / 103


Analog & Digital sensors to Arduino Reading Analog input

LM35 operating modes

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 15 / 103


Analog & Digital sensors to Arduino Reading Analog input

LM35 voltage conversion to temperature

As it is a analog temperature sensor so the output pin of the LM35 will be con-
nected to the Analog pin of Arduino and other pins to the 5V & GND pin.
The analog read value must be changed to corresponding voltage, since the LM35
outputs 10 mV per degree Celsius and the ADC resolution is 1 step per 4.88 mV
(0.00488V calculated earlier),then the analog value can be calculated by ratio and
4.88mV
proportion given by
10mV
Therefore, temperature in Celsius = sensorvalue x this ratio
Alternatively, same thing can be calculated using,
0 5000mV
C = sensorvalue ∗ = sensorvalue ∗ .488
1024 ∗ 10mV

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 16 / 103


Analog & Digital sensors to Arduino Reading Analog input

1 const int sensorPin = A0;


2 float sensorValue;
3 float voltageOut;
4
5 float temperatureC;
6 float temperatureF;
7
8 void setup() {
9 pinMode(sensorPin, INPUT);
10 Serial.begin(9600);
11 }
12
13 void loop() {
14 sensorValue = analogRead(sensorPin);
15 voltageOut = (sensorValue * 5000) / 1024;
16
17 // calculate temperature for LM35
18 temperatureC = voltageOut / 10;
19 temperatureF = (temperatureC * 1.8) + 32;
20
21 Serial.print("Temperature(C): ");
22 Serial.print(temperatureC);
23 Serial.print(" Temperature(F): ");
24 Serial.print(temperatureF);
25 Serial.print(" Voltage(mV): ");
26 Serial.println(voltageOut);
27 delay(1000);
28 }

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 17 / 103


Analog & Digital sensors to Arduino Reading Digital input

Reading Digital input

A digital read is done to tap status of a digital pin.


A push button status can be use to control other events.
A pin is configured as Input Pin to connect switch and another pin is configured
as Output Pin
PULL-UP or PULL-DOWN resistors has to be connected, while interfacing push
button as switch.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 18 / 103


Analog & Digital sensors to Arduino Reading Digital input

Ultrasonic Sensor

Ultrasonic sensors emit short, high-frequency sound pulses at regular intervals.


These propagate in the air at the velocity of sound. If they strike an object, then
they are reflected back as echo signals to the sensor, which itself computes the
distance to the target based on the time-span between emitting the signal and
receiving the echo
As the distance to an object is determined by measuring the time of flight and
not by the intensity of the sound, ultrasonic sensors are excellent at suppressing
background interference.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 19 / 103


Analog & Digital sensors to Arduino Reading Digital input

Ultrasonic Sensor

Virtually all materials which reflect sound can be detected, regardless of their
colour. Even transparent materials or thin foils represent no problem for an ul-
trasonic sensor.
Ultrasonic sensors can see through dust-laden air and ink mists. Even thin de-
posits on the sensor membrane do not impair its function.
Sensors with a blind zone of only 20 mm and an extremely thin beam spread are
making entirely new applications possible today:
Fill level measurement in wells of microtiter plates and test tubes, as well as the
detection of small bottles in the packaging industry, can be implemented with ease.
Even thin wires are reliably detected.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 20 / 103


Analog & Digital sensors to Arduino Reading Digital input

Ultrasonic Sensor - Applications

This sensor is used to measure speed as well as the direction between two objects
This is used to detect objects & avoid obstacles using robots such as biped,
pathfinding, obstacle avoidance, etc.
Depth measurement
This sensor is used to plot the objects nearby the sensor by revolving it
Non-destructive testing
By using this sensor depth of pits, wells can be measured by transmitting the waves
through water.
Embedded system
Burglar alarms

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 21 / 103


Analog & Digital sensors to Arduino Reading Digital input

Ultrasonic Sensor - HC-SR04 Ultrasonic Module

HC-SR04 has an ultrasonic transmitter, receiver and control circuit.


In ultrasonic module HCSR04, we have to give trigger pulse, so that it will gener-
ate ultrasound of frequency 40 kHz.
After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high.
Echo pin remains high until it does not get the echo sound back.
So the width of echo pin will be the time for sound to travel to the object and return
back. Once we get the time we can calculate distance, as we know the speed of
sound.
HC-SR04 can measure up to range from 2 cm - 400 cm.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 22 / 103


Analog & Digital sensors to Arduino Reading Digital input

Ultrasonic Sensor - HC-SR04 Ultrasonic Module

VCC :- +5 V supply
TRIG :- Trigger pin is an Input pin. This pin has to be kept high for 10µ s to initialize
measurement by sending US wave. Microcontroller applies 10µ s trigger pulse to
the HC-SR04 ultrasonic module.
ECHO :- Echo pin is an Output pin. This pin goes high for a period of time which
will be equal to the time taken for the ultrasonic wave to return back to the sensor.
Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.
GND :- Ground

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 23 / 103


Analog & Digital sensors to Arduino Reading Digital input

Specifications - HC-SR04 Ultrasonic Module

Power Supply :+5V DC


Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: less than 15 Deg
Ranging Distance : 2cm -400 cm/1 inch - 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse width: 10µ s
Dimension: 45mm x 20mm x 15mm

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 24 / 103


Analog & Digital sensors to Arduino Reading Digital input

HC-SR04 Ultrasonic Module- Working Principle


To begin measuring the distance, the microcontroller sends a trigger signal to the
ultrasonic sensor.
The duty cycle of this trigger signal is 10µ S for the HC-SR04 ultrasonic sensor.
When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave
bursts and initiates a time counter.
As soon as the reflected (echo) signal is received, the timer stops.
The output of the ultrasonic sensor is a high pulse with the same duration as the
time difference between transmitted ultrasonic bursts and the received echo signal.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 25 / 103


Analog & Digital sensors to Arduino Reading Digital input

HC-SR04 Ultrasonic Module- distance measurement

For distance measurement we can use simple formula Distance = Speed x Time
For example, if the object is 20 cm away from the sensor, and the speed of the
sound is 340 m/s or 0.034 cm/µ s the sound wave will need to travel about 588
microseconds. But what you will get from the Echo pin will be double that number
because the sound wave needs to travel forward and bounce backward. So in
order to get the distance in cm we need to multiply the received travel time value
from the echo pin by 0.034 and divide it by 2.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 26 / 103


Analog & Digital sensors to Arduino Reading Digital input

Advantages of Ultrasonic Sensors

Ultrasonic sensors produce ultrasonic frequencies that humans cannot hear, mak-
ing them ideal for use in environments that require low noise levels.
An ultrasonic sensor response is not dependent upon the surface color or optical
reflectivity of the object e.g., a glass plate or a shiny aluminum plate.
These sensors don’t require much electricity, are simple in design, and are rela-
tively inexpensive.
Ultrasonic sensors with digital (On/Off) outputs have excellent repeat sensing ac-
curacy.
It is possible to ignore immediate background objects, even at long sensing dis-
tances because switching hysteresis (the physical property value lags behind changes
in the causation effect) is relatively low.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 27 / 103


Analog & Digital sensors to Arduino Reading Digital input

Disadvantages of Ultrasonic Sensors

Ultrasonic sensors have a minimum sensing distance.


Changes in the environment, such as temperature, pressure, humidity, air turbu-
lence, and airborne particles affect ultrasonic responses.
Targets with low density, such as foam and cloth, tend to absorb sound energy and
these materials may be difficult to sense at long ranges.
Ultrasonic sensors must be in the direct line of sight of the surface of the object
in order to receive an adequate sound echo. Additionally, the reliability of these
sensors requires a minimum object surface area.
Smooth surfaces reflect sound waves more efficiently than rough surfaces

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 28 / 103


Analog & Digital sensors to Arduino Reading Digital input

HC-SR04 Ultrasonic Module- distance measurement

Ultrasonic sensors can detect objects placed within their range, but they cannot
distinguish between different shapes and sizes. However, one can overcome this
limitation can by using two sensors instead of just one sensor. One can install both
sensors a distance away from each other, or they can be adjacent. By observing
the overlapped shaded region, one can get a better idea of the shape and size of
the target object.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 29 / 103


Analog & Digital sensors to Arduino Reading Digital input

Interfacing with Arduino

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 30 / 103


Analog & Digital sensors to Arduino Reading Digital input

Interfacing with Arduino


1 // defines pins numbers
2 const int trigPin = 9;
3 const int echoPin = 12;
4 // defines variables
5 long duration;
6 int distance;
7 void setup() {
8 pinMode(trigPin, OUTPUT); // Sets the trigPin
as an Output
9 pinMode(echoPin, INPUT); // Sets the echoPin
as an Input
10 Serial.begin(9600); // Starts the serial
communication
11 }
12 void loop() {
13 // Clears the trigPin
14 digitalWrite(trigPin, LOW);
15 delayMicroseconds(2);
16 // Sets the trigPin on HIGH state for 10
micro seconds
17 digitalWrite(trigPin, HIGH);
18 delayMicroseconds(10);
19 digitalWrite(trigPin, LOW);
20 // Reads the echoPin, returns the sound wave
travel time in microseconds
21 duration = pulseIn(echoPin, HIGH);
22 // Calculating the distance
23 distance = duration * 0.034 / 2;
24 // Prints the distance on the Serial Monitor
25 Serial.print("Distance in cm: ");
26 Serial.println(distance);
27 }

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 31 / 103


Analog & Digital sensors to Arduino Reading Digital input

Passive Infrared Sensor


All living objects, whose body temperature is more than 00 C, emit the heat in form
of infrared radiation through their body, also called as thermal radiations.
This Radiated energy is invisible to human eye.
These Signals can be detected by using PIR sensor which is specially designed
for such purpose.
They detect the infrared radiation emitted or reflected from objects.
They are small, inexpensive, low power and easy to use. They are commonly
found at home, medical, factories etc. areas.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 32 / 103


Analog & Digital sensors to Arduino Reading Digital input

PIR Sensor - Working

PIRs are basically made of a pyroelectric sensor, which can detect levels of in-
frared radiation.
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 33 / 103
Analog & Digital sensors to Arduino Reading Digital input

PIR Sensor - Working

The sensor is split in two slots, which are wired up so that they cancel each other
out.
If one half sees more or less IR radiation than the other, the output will swing high
or low.
Input signals from both terminals of PIR element are amplified using amplifier cir-
cuit and compared using comparator circuit.
The PIR element is covered by lens to increase range of operation.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 34 / 103


Analog & Digital sensors to Arduino Reading Digital input

PIR Sensor - Working- At Idle Position

PIR motion sensor uses element RE200B for infrared detection. Both slots of this
sensor are connected to differential amplifier.
When the sensor is idle, both slots detect same amount of IR.
So, there is no error signal between differential inputs. The output of comparator
circuit is zero.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 35 / 103


Analog & Digital sensors to Arduino Reading Digital input

PIR Sensor - Working- Object in Motion


When any warm object passes in front of the sensor, it intercepts one slot of the
PIR sensor. This causes a positive differential change between the two slots. This
change is indicated by Part A in below figure.
When the warm body leaves the sensing area, the sensor generates negative
differential change. This change is indicated by Part B in below figure.
Both these changes in pulse are the detection of warm body which radiate infrared
signals.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 36 / 103


Analog & Digital sensors to Arduino Reading Digital input

PIR Sensor - Pin Description

Pin 1 – GND:- We have to connect this pin to Ground.


Pin 2 – Output :- This pin gives output (3.5V) when the motion is detected.
Pin 3 – VCC :- This pin provides supply voltage(+5v) to PIR element and internal
circuit.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 37 / 103


Analog & Digital sensors to Arduino Reading Digital input

Modes of Operations - Single Trigger Mode


To select Single Trigger mode, the jumper setting on PIR sensor must be set on
LOW.
In case of Single Triggered Mode, Output goes HIGH when motion is detected.
After specific delay (tsel) the output goes to LOW even if the object is in motion.
The output is LOW for some time and again goes HIGH if object remains in motion.
This delay (tsel) is provided by user using the potentiometer. This potentiometer is
on board of PIR sensor module.
In this way, the PIR sensor gives HIGH/LOW pulses if object is in continuous mo-
tion.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 38 / 103


Analog & Digital sensors to Arduino Reading Digital input

Modes of Operations - Repeat Trigger Mode


To select Repeat Trigger mode, the jumper setting on PIR sensor must be set on
HIGH.
In case of Repeat Triggered Mode, Output goes HIGH when motion is detected.
The output of PIR sensor is HIGH until the object is in motion.
When object stops motion, or disappears from the sensor area, the PIR continues
its HIGH state up to some specified delay (tsel).
We can provide this delay (tsel) by adjusting the potentiometer. This potentiometer
is on board of PIR sensor module.
In this way, the PIR sensor gives HIGH pulse if object is in continuous motion.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 39 / 103


Analog & Digital sensors to Arduino Reading Digital input

Modes of Operations - Changing Sensitivity and Delay time


There are two potentiometers on PIR motion sensors board: Sensitivity Adjust and
Time delay adjust.
It is possible to make PIR more sensitive or Non-Sensitive Enough. The maximum
sensitivity can be achieved up to 6 meters.
Time Delay Adjust potentiometer is used to adjust the time tsel shown in above
timing diagrams.
Clockwise Movement makes PIR more Sensitive. Two things are important while
manufacturing PIR sensor: Low cost and High Sensitivity. Both these things can
be magically achieved by using Lens cap. The lenses increase range of operation;
increases sensitivity and change pattern of Sensing vary easily

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 40 / 103


Analog & Digital sensors to Arduino Reading Digital input

PIR interfacing with Arduino - Motion detection


There are two potentiometers on PIR motion sensors board: Sensitivity Adjust and
Time delay adjust.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 41 / 103


Analog & Digital sensors to Arduino Reading Digital input

Interfacing with Arduino


1 const int PIR_SENSOR_OUTPUT_PIN = 4; /* PIR
sensor O/P pin */
2 int warm_up;
3
4 void setup() {
5 pinMode(PIR_SENSOR_OUTPUT_PIN, INPUT);
6 Serial.begin(9600); /* Define baud rate
for serial communication */
7 delay(20000); /* Power On Warm Up Delay
*/
8 }
9
10 void loop() {
11 int sensor_output;
12 sensor_output = digitalRead(
PIR_SENSOR_OUTPUT_PIN);
13 if( sensor_output == LOW )
14 {
15 if( warm_up == 1 )
16 {
17 Serial.print("Warming Up\n\n");
18 warm_up = 0;
19 delay(2000);
20 }
21 Serial.print("No object in sight\n\n"
);
22 delay(1000);
23 }
24 else
25 {
26 Serial.print("Object detected\n\n");
27 warm_up = 1;
28 delay(1000);
29 }
30 }

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 42 / 103


Timers, Analog comparators and hardware interrupts

Timers, Analog comparators and hardware interrupts

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 43 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupts

Allow program to respond to events when they occur


Allow program to ignore events until they occur
External events e.g.:
UART ready with/for next character
Signal change on pin :- Action depends on context
# of edges arrived on pin
Internal events e.g. :Power failure, Arithmetic exception, Timer “tick”

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 44 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Need of interrupts

To detect pin changes (eg. rotary encoders, button presses)


Watchdog timer (eg. if nothing happens after 8 seconds, interrupt me)
Timer interrupts – used for comparing/overflowing timers
SPI data transfers
I2C data transfers
USART data transfers
ADC conversions (analog to digital)
EEPROM ready for use
Flash memory ready

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 45 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupts

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 46 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupts

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 47 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupt Model

When an interrupt event occurs:


Processor does an automatic procedure call
CALL automatically done to address for that interrupt :- Push current PC, Jump to
interrupt address
Each event has its own interrupt address
The global interrupt enable bit (in SREG) is automatically cleared
i.e. nested interrupts are disabled
SREG bit can be set to enable nested interrupts if desired
Interrupt procedure, aka “interrupt handler”
Does whatever it needs to, then returns via RETI
The global interrupt enable bit is automatically set on RETI
One program instruction is always executed after RETI

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 48 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupts Types

Type 1 - Event is remembered when interrupt is disabled


If interrupt is not enabled, flag is set
When interrupt is enabled again, interrupt takes place, and flag is reset

Type 2 – Event is not remembered when interrupt is disabled


Signal level causes interrupt
If level occurs when interrupt is enabled, interrupt takes place
If interrupt is not enabled, and level goes away before the interrupt is enabled,
nothing happens

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 49 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupt Model

Interrupt hander is invisible to program


Except through side-effects, e. g. via flags or variables :- Changes program timing
Must be written so they are invisible
Cannot stomp on program state, e. g. registers
Save and restore any registers used :- Including SREG

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 50 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupt Vectors
Table in memory containing the first instruction of each interrupt handler
Typically at program address 0

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 51 / 103


Timers, Analog comparators and hardware interrupts Interrupts

Interrupt Vectors

If interrupts are not used, this memory can be used as part of the program i.e.
nothing special about this part of memory
Example interrupt routine :- RESET: Sets up the stack pointer

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 52 / 103


Timers, Analog comparators and hardware interrupts Interrupts

External Interrupts

Monitors changes in signals on pins


What causes an interrupt can be configured
by setting control registers appropriately
Pins
INT0 and INT1 – range of event options
INT0 – PORT D [2]
INT1 – PORT D [3]
PCINT[23:0] – any signal change (toggle)
PCINT[7:0] – PORT B [7:0]
PCINT[14:8] – PORT C [6:0]
PCINT[23:16] – PORT D [7:0]

Pulses on inputs must be slower than I/O clock rate

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 53 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Timer/Counters
Timer is kind of interrupt. It is like a simple clock which can measure time interval
of an event.
Every microcontroller has a clock (oscillator), say in Arduino Uno it is 16Mhz.
A timer uses counter which counts at certain speed depending upon the clock
frequency.
In Arduino Uno it takes 1/16000000 seconds or 62nano seconds to make a single
count. Meaning Arduino moves from one instruction to another instruction for every
62 nano second.
Timers in Arduino UNO
1 Timer0:- It is an 8-Bit timer and used in timer function such as delay(), millis().
2 Timer1: - It is a 16-Bit timer and used in servo library.
3 Timer2:- It is an 8-Bit Timer and used in tone() function.

The Pins for timers are T0 – GPIO6, T1 – GPIO11, TOSC1 – GPIO9, TOSC2 – GPIO10,
ICP1 – GPIO
ICP1 is an input capture pin which can be used to capture the external pulse at a
specific interval of time. When an input pulse will occur on this pin then it will generate
a timestamp which can tell when the external signal was received.
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 54 / 103
Timers, Analog comparators and hardware interrupts Timer/Counters

ATmega328P Timing Subsystem

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 55 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Timer/Counters applications

Turn on or turn off an external device at a programmed time.


Generate a precision output signal (period, duty cycle, frequency). For example,
generate a complex digital waveform with varying pulse width to control the speed
of a DC motor
Measure the characteristics (period, duty cycle, frequency) of an incoming digital
signal
Count external events

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 56 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Arduino Timer Registers

To change the configuration of the timers, timer registers are used

Timer/Counter Control Registers (TCCRnA/B):


This register holds the main control bits of the timer and used to control the prescalers
of timer. It also allows to control the mode of timer using the WGM bits.

Frame Format

TCCR1A 7 6 5 4 3 2 1 0
COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10

TCCR1B 7 6 5 4 3 2 1 0
ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 57 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Arduino Timer Registers

Prescaler:
The CS12, CS11, CS10 bits in TCCR1B sets the prescaler value. A prescaler is
used to setup the clock speed of the timer. Arduino Uno has prescalers of 1, 8, 64,
256, 1024.
CS12 CS11 CS10 USE
0 0 0 No Clock Timer STOP
0 0 1 CLCK i/o /1 No Prescaling
0 1 0 CLK i/o /8 (From Prescaler)
0 1 1 CLK i/o /64 (From Prescaler)
1 0 0 CLK i/o /256 (From Prescaler)
1 0 1 CLK i/o /1024 (From Prescaler)
1 1 0 External clock source on T1 Pin. Clock on falling edge
1 1 1 External Clock source on T1 pin. Clock on rising edge.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 58 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Arduino Timer Registers

Timer/Counter Register (TCNTn)


This Register is used to control the counter value and to set a preloader value.
Formula for preloader value for required time in second:
TCNTn = 65535 – (16x1010 xTime in sec / Prescaler Value)
To calculate preloader value for timer1 for time of 2 Sec:
TCNT1 = 65535 – (16x1010 x2 / 1024) = 34285

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 59 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Arduino Timer Interrupts


There are various timer interrupts in Arduino which are explained below

Timer Overflow Interrupt


Whenever the timer reaches to its maximum value say for example (16 Bit-65535)
the Timer Overflow Interrupt occurs.
So, an ISR interrupt service routine is called when the Timer Overflow Interrupt bit
enabled in the TOIEx present in timer interrupt mask register TIMSKx.
ISR Format
ISR(TIMERx OVF vect)

Output Compare Register (OCRnA/B)


Here when the Output Compare Match Interrupt occurs then the interrupt service
ISR (TIMERx COMPy vect) is called and also OCFxy flag bit will be set in TIFRx
register.
This ISR is enabled by setting enable bit in OCIExy present in TIMSKx register.
Where TIMSKx is Timer Interrupt Mask Register

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 60 / 103


Timers, Analog comparators and hardware interrupts Timer/Counters

Arduino Timer Registers

Timer Input Capture


Next when the timer Input Capture Interrupt occurs then the interrupt service ISR
(TIMERx CAPT vect) is called and also the ICFx flag bit will be set in TIFRx (Timer
Interrupt Flag Register).
This ISR is enabled by setting the enable bit in ICIEx present in TIMSKx register.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 61 / 103


Timers, Analog comparators and hardware interrupts Analog Comparator

Analog Comparator
The microcontroller has internal comparator modules for analog signal.
Analog comparator module is used to compare the input voltages at the positive
pin AIN0 and the negative pin AIN1.
The output of the comparator, ACO, is set when the voltage reading at the positive
pin is higher than the voltage reading at the negative pin.
This output can then be used to trigger an interrupt including the Timer/Counter1
input capture function or a separate interrupt (triggered at output rise, fall, toggle).
Comparator pins of the microcontroller are listed below:
AIN0 (Positive) – GPIO12
AIN1 (Negative) – GPIO13

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 62 / 103


Communication Interfaces

Communication Interfaces

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 63 / 103


Communication Interfaces

Communication Interfaces

Arduino UNO supports three different types of communication interfaces. They


are:
1 Serial or UART
2 I2 C or I2C
3 SPI
the most common communication interface in the Arduino universe is the Serial
Communication. In fact, the Arduino boards (UNO or Nano or Mega) are
programmed using the serial communication.
Digital IO pins 0 and 1 are used as Serial RX and TX pins to receive and transmit
serial data. These pins are connected to the serial pins of the on-board USB to
Serial Converter IC.
Analog Input Pins A4 and A5 have alternative functions. They can be configured
as SDA (A4) and SCL (A5) to support I2C or I2C or Two Wire Interface (TWI)
communication.
The final communication interface is the SPI. Digital IO Pins 10, 11 12 and 13 can
be configured as SPI pins SS, MOSI, MISO and SCK respectively.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 64 / 103


Communication Interfaces UART/Serial communication

UART/Serial communication

UART stands for Universal Asynchronous Receiver/Transmitter. It is a hardware


device (or circuit) used for serial communication between two devices.
Connecting two UART devices together is simple and straightforward. Figure 1
shows a basic UART connection diagram.

One wire is for transmitting data (called the TX pin) and the other is for receiving
data (called the RX pin). We can only connect two UART devices together.
UART works by converting data into packets for sending or rebuilding data from
packets received.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 65 / 103


Communication Interfaces UART/Serial communication

Sending Serial Data

Before the UART device can send data, the transmitting device converts the data
bytes to bits.
After converting the data into bits, the UART device then splits them into packets
for transmission.
Each packet contains a start bits, a data frame, parity bit, and the stop bits Figure
shows a sample data packet.

After preparing the packet, the UART circuit then sends it out via the TX pin.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 66 / 103


Communication Interfaces UART/Serial communication

Receiving Serial data

The receiving UART device checks the received packet (via RX pin) for errors
by calculating the number of 1’s and comparing it with the value of the parity bit
contained in the packet.
If there are no errors in transmission, it will then proceed to strip the start bit, stop
bits, and parity bit to get the data frame. It may need to receive several packets
before it can rebuild the whole data byte from the data frames.
After rebuilding the byte, it is stored in the UART buffer.
The receiving UART device uses the parity bit to determine if there was a data loss
during transmission.
Data loss in transmission happens when a bit changed its state while being trans-
mitted.
Bits can change because of the transmission distance, magnetic radiation, and
mismatch baud rates, among other things.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 67 / 103


Communication Interfaces UART/Serial communication

Arduino UART Interface

Arduino has one or more UART pins depending on the board. For our project, we
will use an Arduino Uno which has only one UART interface found on pin 0 (RX0)
and pin 1 (TX0).
The Arduino pins 0 and 1 are also used for communicating with the Arduino IDE
via the USB.
So if you will upload sketches to your UNO, be sure to first disconnect any wires
on pins 0 and 1. Figure shows the location of the UART TX and RX pins.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 68 / 103


Communication Interfaces UART/Serial communication

Functions Used for Serial Communication


Serial.begin (baud rate) :- This function is used to initiate the serial communica-
tion with baud rate. e.g. Serial.begin(9600)
Serial.print (data, format type (optional)) :- It sends ASCII character on serial port
so human can read it easily. This function converts the data to ascii character and
then send (print) it.
e.g.1 Serial.print (123);
e.g.2 Serial.print (“123”); & e.g.3 Serial.print (12, HEX)
Serial.println (data, format type (optional)) It sends ASCII character on serial port
followed by carriage return.
Serial.write() This function writes binary data to the serial port. It returns number
of byte written.
e.g. Serial.write (65); // it writes actual byte to serial port
Serial.available () This function returns the number of bytes available to read.
Serial.read() This function is used to read data serially.
e.g. received data = Serial.read()
Serial.readString() This function is used to read the received string.
e.g. String received data = Serial.readString()
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 69 / 103
Communication Interfaces I2C

I2C
I2C (Inter-Integrated Circuit) is serial bus interface connection protocol. It is also
called as TWI (two wire interface) since it uses only two wires for communication.
Those two wires are SDA (serial data) and SCL (serial clock).
I2C is an acknowledgment-based communication protocol i.e. transmitter checks
for an acknowledgment from the receiver after transmitting data to know whether
data is received by receiver successfully.
I2Cworks in two modes namely,
1 Master mode
2 Slave mode
SDA (serial data) wire is used for data exchange in between the master and slave
device.
SCL (serial clock) is used for the synchronous clock in between master and slave
device.
Master device initiates communication with a slave device. It requires a slave
device’s address to initiate conversation.
The slave device responds to a master device when it is addressed by a master
device.
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 70 / 103
Communication Interfaces I2C

I2C
The I2C device has 7-bit or 10-bit unique address. So, to access these devices, a
master must address them by the 7-bit or 10-bit unique address.
I2C is used in many applications like reading RTC (Real-time clock), accessing
external EEPROM memory. It is also used in sensor modules like a gyro, magne-
tometer etc.
I2C protocol uses 2 lines for communication:
1 Serial Clock (SCL): It is a clock signal. Data will be sent to other devices on clock
tick event. Only master device has control over this SCL line.
2 Serial Data (SDA): It is a serial data line which is used for exchanging data between
master and slave device

I2C bus is an open drain configuration which means they can pull the corresponding
signal line low but cannot drive it high. Hence the line will go in to unknown state. In
order to avoid this, pull up resistors need to be connected on SCL and SDA pins.
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 71 / 103
Communication Interfaces I2C

Arduino I2C Pins


The Arduino Uno board has only one I2C module, but it provides these SDA and
SCL line at two different locations.
Note: While communicating with devices using I2C communication protocol, pull-
up resistors should be used. The value of pull-up resistors may vary depend upon
devices used.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 72 / 103


Communication Interfaces I2C

I2C functions for Arduino

Wire.write ( data):- It is used to write (transmit) data to the master or slave device.
Parameter
data it can be single byte value, string, array of data.
Returns
No. of bytes written.
e.g. Wire.write(7); //send data byte
Wire.write(“i2c”); //send string to slave device
Wire.write(a, 6); //here a is an array
Wire.available():- This function is used by a master or slave to check the re-
quested data is available or not. It returns the no. of bytes available.
Wire.read() It is used to read the requested data by master from slave or read the
data transmitted from a master to a slave.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 73 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by


Arduino Microcontrollers for communication purposes with one or more peripheral
devices quickly over short distances.
It can also be used for communication between two microcontrollers.
SPI is a full duplex communication which means it can send and read data at same
time.
Among all three communication protocols (USART, SPI and I2C) in Arduino SPI is
fastest.
SPI has applications where high data rate is required such as displaying text on
screens or writing data on SD card.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 74 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI)

SPI works using four lines:


SCK:- Clock signal which synchronize data transfer between master and slave
devices.
MISO:- (Master in Slave Out) or MISO is a data line for slave that can send data
back to master.
MOSI:- (Master Out Slave In) or MOSI is a data line for master to send data to
slave devices and peripherals.
SS: (Slave Select) It is the line used by master to select a specific slave device. It
informs slave device to which data is going to be sent or received.
Master/Slave (OLD) Controller/Peripheral (NEW)
Master In Slave Out (MISO) Controller In, Peripheral Out (CIPO)
Master Out Slave In (MOSI) Controller Out Peripheral In (COPI)
Slave Select pin (SS) Chip Select Pin (CS)

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 75 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

SPI Pinout in Arduino Uno


SPI protocol is supported by multiple Arduino boards. Following are the pins used
by Arduino Uno for serial peripheral communication.
SPI Line GPIO ICSP Header Pin
SCK 13 3
MISO 12 1
MOSI 11 4
SS 10 –

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 76 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI)

With an SPI connection there is always one Controller device (usually a microcontroller)
which controls the peripheral devices. Typically there are three lines common to all the
devices:
CIPO (Controller In Peripheral Out) - The Peripheral line for sending data to the
Controller
COPI (Controller Out Peripheral In) - The Controller line for sending data to the
peripherals
SCK (Serial Clock) - The clock pulses which synchronize data transmission gen-
erated by the Controller and one line specific for every device
CS (Chip Select) - the pin on each device that the Controller can use to enable and
disable specific devices. When a device’s Chip Select pin is low, it communicates
with the Controller. When it’s high, it ignores the Controller. This allows you to
have multiple SPI devices sharing the same CIPO, COPI, and CLK lines.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 77 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI)

To write code for a new SPI device you need to note a few things:
What is the maximum SPI speed your device can use? This is controlled by the first
parameter in SPISettings. If you are using a chip rated at 15 MHz, use 15000000.
Arduino will automatically use the best speed that is equal to or less than the
number you use with SPISettings.
Is data shifted in Most Significant Bit (MSB) or Least Significant Bit (LSB) first?
This is controlled by second SPISettings parameter, either MSBFIRST or LSB-
FIRST. Most SPI chips use MSB first data order.
Is the data clock idle when high or low? Are samples on the rising or falling edge of
clock pulses? These modes are controlled by the third parameter in SPISettings.
The SPI standard is loose and each device implements it a little differently. This
means you have to pay special attention to the device’s datasheet when writing
your code.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 78 / 103


Communication Interfaces Serial Peripheral Interface (SPI)

Serial Peripheral Interface (SPI)

Generally speaking, there are four modes of transmission.


These modes control whether data is shifted in and out on the rising or falling edge
of the data clock signal (called the clock phase), and whether the clock is idle when
high or low (called the clock polarity).
The four modes combine polarity and phase according to this table
Mode Clock Polarity (CPOL) Clock Phase(CPHA) Output Edge Data Capture
SPI MODE0 0 0 Falling Rising
SPI MODE1 0 1 Rising Falling
SPI MODE2 1 0 Rising Falling
SPI MODE3 1 1 Falling Rising

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 79 / 103


Basics of wireless networking

Basics of wireless networking

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 80 / 103


Basics of wireless networking

Basics of wireless networking


Computer networks that are connected without wires are called wireless networks.
They generally use radio waves for communication between the network nodes.
They allow devices to be connected to the network while roaming around within
the network coverage
Computer wireless network includes Wireless LANs, MANs & WANS

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 81 / 103


Basics of wireless networking

Key components of wireless networks


Access point :- An access point is the device that allows multiple wireless devices
to connect with each other. Just like a HUB or switch connects multiple devices
together in a single or multiple wired LAN networks, an access point connects mul-
tiple wireless devices together in a single wireless or multiple wireless networks.
An access point can also be used to extend the wired network to the wireless de-
vices.

Based on the functionalities, we can categorize the access point in three types;
standalone access point, multifunction access point and controlled access point
Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 82 / 103
Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32

A low-cost, low-power system from a chip (SoC) series that has been created by
Espressif Systems, ESP32 comes with Wi-Fi & dual-mode Bluetooth capabilities.
One of the key features is its dual-core or single-core Tensilica Xtensa LX6 micro-
processor with a clock rate of up to 240 MHz.
Highly integrated with built-in antenna switches, RF balun, power amplifier, low-
noise receive amplifier, filters, power management modules, touch sensitive pins,
built-in hall effect sensor and temperature sensor, ESP32 has been engineered for
mobile devices, wearable electronics, and IoT applications.
ESP32-WROOM-32 is a very popular chip used for the internet of things appli-
cations. The main part of this module is ESP32-D0WDQ6 chip.
It consists of an on-chip WiFi module, Bluetooth low energy module, and Bluetooth
module.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 83 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32
It is a very low-cost board and can be purchased around 300 Rs.
It consists of two cores and each core can be controlled separately.
It can operate at the variable frequency range from 80 MHz to 240 MHz.
It has a special ultra-low power co-processor. A user can power off processors and
can use a low power co-processor to monitor peripherals at low power like GPIO
pins.
Figure below shows the complete diagram of D0WDQ6 chip.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 84 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 85 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 86 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 Supply Pins: The module has two 5V


and 3.3V power supply pins. You can
use these two pins to supply other
devices and modules.
2 GND Pin:The module has 3 pins for
its ground
3 ADC: You can use the 16 ADC pins
on this module to convert analog
voltages (output of some sensors) to
digital. Some of these converters are
connected to the internal amplifier
and are able to measure small
voltages with high precision.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 87 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 Supply Pins: The module has two 5V


and 3.3V power supply pins. You can
use these two pins to supply other
devices and modules.
2 GND Pin:The module has 3 pins for
its ground
3 ADC: You can use the 16 ADC pins
on this module to convert analog
voltages (output of some sensors) to
digital. Some of these converters are
connected to the internal amplifier
and are able to measure small
voltages with high precision.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 87 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 Supply Pins: The module has two 5V


and 3.3V power supply pins. You can
use these two pins to supply other
devices and modules.
2 GND Pin:The module has 3 pins for
its ground
3 ADC: You can use the 16 ADC pins
on this module to convert analog
voltages (output of some sensors) to
digital. Some of these converters are
connected to the internal amplifier
and are able to measure small
voltages with high precision.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 87 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 DAC: The ESP32 module has two


digital to analog converters with 8
bits accuracy.
2 Touchpads: There are 10 pins on the
ESP32 module that are sensitive to
capacitor changes. You can connect
these pins to some pads (the pads
on the PCB) and use them as touch
switches.
3 SPI: There are two SPI interfaces on
this module that you can use to
connect the display, the SD /
microSD memory card module,
external flash memory, and more.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 88 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 DAC: The ESP32 module has two


digital to analog converters with 8
bits accuracy.
2 Touchpads: There are 10 pins on the
ESP32 module that are sensitive to
capacitor changes. You can connect
these pins to some pads (the pads
on the PCB) and use them as touch
switches.
3 SPI: There are two SPI interfaces on
this module that you can use to
connect the display, the SD /
microSD memory card module,
external flash memory, and more.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 88 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 DAC: The ESP32 module has two


digital to analog converters with 8
bits accuracy.
2 Touchpads: There are 10 pins on the
ESP32 module that are sensitive to
capacitor changes. You can connect
these pins to some pads (the pads
on the PCB) and use them as touch
switches.
3 SPI: There are two SPI interfaces on
this module that you can use to
connect the display, the SD /
microSD memory card module,
external flash memory, and more.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 88 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 I2C: SDA and SCL pins are used for


I2C communication.
2 Serial Communication (UART): There
are two UART serial interfaces on
this module. Using these pins, you
can transfer information up to 5Mbps,
between two devices. UART0 has
also CTS and RTS bases.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 89 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

1 I2C: SDA and SCL pins are used for


I2C communication.
2 Serial Communication (UART): There
are two UART serial interfaces on
this module. Using these pins, you
can transfer information up to 5Mbps,
between two devices. UART0 has
also CTS and RTS bases.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 89 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 pinouts

Enable Pin (EN): This pin is used to enable and disable the module. It should be
HIGH to enable the module and must be LOW to disable it.
Input/Output Pins (GPIO): You can use the 32 GPIO pins to communicate with the
LEDs, switches, and other input/output devices. You can pull-up or pull-down
these pins internally.
The GPIO6 to GPIO11 pins which are SCK / CLK, SDO / SD0, SDI / SD1, SHD /
SD2, SWP / SD3, and SCS / CMD pins, are used for SPI communication of the
internal flash memory of the module and we do not recommend you to use them.
PWM: Almost all of the ESP32 input/output pins can be used for PWM (Pulse
Width Modulation). Using these pins you can control the motors, LEDs light and
color and so on

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 90 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32 Features

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 91 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

The ESP32 Module Modes

Active mode:
In this case, all parts of the Wi-Fi and Bluetooth transmitter and receiver are active.
In this case, the current consumption is between 80 and 260 mA.

Modem-sleep mode:
The processor is still active, but the Wi–Fi and Bluetooth are disabled. The current
consumption is between 3 and 20 mA, in this case

Light-sleep mode:
The main processor stops working, but the RTC unit and the ULP processor unit
are still active. The current consumption is about 0.8 mA.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 92 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

The ESP32 Module Modes

Deep-sleep mode:
Only the RTC unit is active. In this case, the data of Wi-Fi and Bluetooth communi-
cations are stored in the RTC’s memory. The current consumption is between 10
and 150 µ A in this mode.

Hibernation mode:
All units are disabled, except for an RTC timer for the clock and some I / O pins
connected to the RTC. The RTC timer or the connected pins can wake the chip up
from this state. The current consumption is about 2.5 µ A in this case.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 93 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP32

Advantages:- Low cost, ultra-low power consumption, power saving features such
as fine resolution clock gating, multiple power modes, and dynamic power scaling,
many more GPIOs than the ESP8266.
Disadvantages:- Costs slightly more than the ESP8266
Projects :- smart home projects, sensor-based projects- All-in-one ESP32 weather
station shield, barometric sensor, data logging projects like recording the temper-
ature to MicroSD card and web-based projects such as setting an ESP32 Access
Point (AP) for the web server.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 94 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-12E/8266 Module
The development board equips the ESP-12E module containing ESP8266 chip
having Tensilica Xtensa 32-bit LX106 RISC microprocessor which operates at 80
to 160 MHz adjustable clock frequency and supports RTOS

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 95 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-8266 Module

There’s also 128 KB RAM and 4MB of Flash memory (for program and data
storage) just enough to cope with the large strings that make up web pages,
JSON/XML data, and everything we throw at IoT devices nowadays
The ESP8266 Integrates 802.11b/g/n HT40 Wi-Fi transceiver, so it can not only
connect to a WiFi network and interact with the Internet, but it can also set up a
network of its own, allowing other devices to connect directly to it. This makes the
ESP8266 NodeMCU even more versatile.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 96 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-8266 Module Power Requirement


As the operating voltage range of ESP8266 is 3V to 3.6V, the board comes with a
LDO voltage regulator to keep the voltage steady at 3.3V. It can reliably supply up
to 600mA, which should be more than enough when ESP8266 pulls as much as
80mA during RF transmissions. The output of the regulator is also broken out to
one of the sides of the board and labeled as 3.3V. This pin can be used to supply
power to external components.
Power to the ESP8266 NodeMCU is supplied via the on-board MicroB USB con-
nector. Alternatively, if you have a regulated 5V voltage source, the VIN pin can be
used to directly supply the ESP8266 and its peripherals.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 97 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-8266 Module Peripherals and I/O


The ESP8266 NodeMCU has total 17 GPIO pins broken out to the pin headers on
both sides of the development board. These pins can be assigned to all sorts of
peripheral duties, including:
ADC channel – A 10-bit ADC channel.
UART interface – UART interface is used to load code serially.
PWM outputs – PWM pins for dimming LEDs or controlling motors.
SPI, I2C & I2S interface – SPI and I2C interface to hook up all sorts of sensors
and peripherals.
I2S interface – I2S interface if you want to add sound to your project.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 98 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-8266 Module On-board Switches & LED Indicator

The ESP8266 NodeMCU features two buttons. One marked as RST located on
the top left corner is the Reset button, used of course to reset the ESP8266 chip.
The other FLASH button on the bottom left corner is the download button used
while upgrading firmware
The board also has a LED indicator which is user programmable and is connected
to the D0 pin of the board.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 99 / 103


Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP-8266 Module Serial Communication

The board includes CP2102 USB-to-UART Bridge Controller from Silicon Labs,
which converts USB signal to serial and allows your computer to program and
communicate with the ESP8266 chip.

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 100 / 103
Basics of wireless networking NodeMCU ESP32 & ESP8266

ESP8266

Advantages:-The advantages of the ESP8266 include its low cost, reliability and
easily availability in the market.
Disadvantages:- Until a while ago, most of its documentation was in Mandarin,
although the emergence of the ESP8266 community in recent days has helped
translate several details about programming.
ESP8266 projects:- Examples of ideal projects include tracking geolocation, build-
ing a wireless web server, putting pressure sensors on railway tracks to detect
animal presence and set off an alarm (thus avoiding animal deaths on tracks),
building smart plugs, humidity and temperature monitoring, and even making a
personal assistant of your own (think along the lines of SIRI, Google Assistant,
Alexa!).

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 101 / 103
Basics of wireless networking NodeMCU ESP32 & ESP8266

Comparison ESP32 Vs ESP8266

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 102 / 103
Basics of wireless networking NodeMCU ESP32 & ESP8266

References

1 arxterra
2 NodeMCU ESP8266 Communication Methods and Protocols : Programming with
Arduino IDE - Manoj R. Thakur
3 MicroPython for ESP8266 Development Workshop - Agus Kurniawan
4 ESP8266 SDK API Guide - Russ Mathis

Prasenjeet Damodar Patil Arduino Interfacing- U5 November 11, 2022 103 / 103

You might also like