Unit 5 IPC
Unit 5 IPC
Associate Professor
Department of Computer Science & Engg.
MIT, ADT, University, Pune, Maharashtra
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.
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
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 }
Voltage divider-Potentiometer
Voltage divider-Potentiometer
R2
Vout = Vcc x
R1 + R2
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)
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.
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.
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.
LM35 - features
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
Ultrasonic Sensor
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.
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
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
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.
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.
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.
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
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.
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.
Interrupts
Need of interrupts
Interrupts
Interrupts
Interrupt Model
Interrupts Types
Interrupt Model
Interrupt Vectors
Table in memory containing the first instruction of each interrupt handler
Typically at program address 0
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
External Interrupts
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
Timer/Counters applications
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
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.
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
Communication Interfaces
Communication Interfaces
UART/Serial communication
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
ESP32
ESP32
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
ESP32 pinouts
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
ESP32 Features
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.
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.
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.
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
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.
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.
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
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