IoT - UNIT 3 Completed On 2.2.2023 (7 Classes)

You might also like

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

UNIT 3

FUNDAMENTALS OF
INTERNET OF THINGS

PRUDHVI KIRAN P
Assistant Professor, CSE - IoT Dept.
R. V. R. & J. C. College of Engineering
UNIT-3 Analog
and Digital Communication with Arduino:
Introduction - Serial communication, Parallel communication, Interfacing LCD character display.
UART Communication: UART protocol, Interfacing ESP8266 module.
I2C Communication: I2C protocol, Interfacing ADXL345 module, Interfacing BMP180 module.
SPI Communication: SPI protocol, SD card interfacing, Ethernet module interfacing.

SUBJECT CODE - CO 214


CLASS DETAILS - CSE (IoT) III SEM - OCTOBER 2022
FACULTY DETAILS - PRUDHVI KIRAN P, Asst. Prof., CSE (IoT), RVR&JC College of Engineering
QUESTION BANK [ASSIGNMENT QUESTIONS - 1,6,10]
1. Explain Parallel Communication and Serial Communication in detail with relevant diagrammatical representation.
2. Draw and explain the PIN Diagram of 16x2 LCD Module and write a program to display your NAME in first row and
AGE in second row of the LCD.
3. Explain the Working of UART Communication Protocol in detail with neat diagram of UART Packet.
4. Discuss about ESP8266 SoC with neat Pin Diagram. And write Arduino Sketch with neat diagram, to interface ESP-01
with Arduino UNO.
5. Explain the Working of SPI Communication Protocol in detail with diagrammatical representation.
6. Draw and explain the circuit for interfacing SDCARD Module with Arduino UNO and write a Arduino Sketch for the
same.
7. Draw and explain the circuit for interfacing ETHERNET Module with Arduino UNO and write a Arduino Sketch for the
same.
8. Explain the Working of I2C Communication Protocol with diagrammatical representation of packet transfer from
Master to Slave and Slave to Master.
9. Draw and explain the circuit for interfacing ADXL345 with Arduino UNO and write a Arduino Sketch for the same.
10. Draw and explain the circuit for interfacing BMP180 with Arduino UNO and write a Arduino Sketch for the same.
END
Analog and digital signals are the types
SIGNAL is the information converted to electrical
of signals carrying Information. The
form, suitable for transmission from one system to major difference between both signals is
other system. This signal can be Analog or Digital. that the Analog signals have continuous
electrical signals, while Digital signals
have non-continuous electrical signals.
Analog signal is denoted by sine waves
while Digital signals are denoted by
square waves.
Bandwidth is the maximum quantity of
information/data that can be transmitted
through a channel in a specific period of
time. Throughput is defined as the actual
measurement of data moving across a
medium at any given point of time.
Bandwidth is measured in Bits.
Throughput is measured in Bits per
Second. Bandwidth and throughput both
indicate network performance.
Arduino Uno has 14 digital
In ARDUINO UNO … input/output pins (out of which 6 can be
The Arduino can input and output analog signals as well used as PWM outputs), 6 analog
as digital signals. input pins.

 An analog signal is one that can take on any number  The Arduino does not have a built-
of values, unlike a digital signal which has only two in digital-to-analog converter (DAC),
values: HIGH and LOW. but it can pulse-width modulate
 To measure the value of analog signals, the Arduino (PWM) a digital signal to achieve
has a built-in analog-to-digital converter (ADC). The some of the functions of an analog
ADC turns the analog voltage into a digital value. output, using PWM pins.

 The function that you use to obtain the value of an  The function used to output a PWM
analog signal is analogRead(pin). This function signal is analogWrite(pin, value);
converts the value of the voltage on an analog input pin is the pin number used for the
pin and returns a digital value from 0 to 1023, relative PWM output. value is a number
to the reference value. The default reference voltage is proportional to the signal which
5 V (for 5 V Arduino boards) or 3.3 V (for 3.3 V Arduino ranges from 0 - 255 like in digital
boards). It has one parameter which is the pin representation of analog signal
number. which ranges from 0 to 1023.
SERIAL & PARALLEL COMMUNICATION IN ARDUINO

8 BITS OF DATA

TX or RX
1 Clock Pulse

TX to RX or RX to TX
8 Clock Pulses
Electronic devices talk to each other by sending
bits of data through wires physically connected
between devices. A bit is binary and can only be a
1 or 0. Bits are transferred from one device to
another by quick changes in voltage. In a system
operating at 5 V, a 0 bit is communicated as a
short pulse of 0 V, and a 1 bit is communicated by
a short pulse of 5 V.
In parallel communication, the bits of data are
sent all at the same time, each through a
separate wire. The following diagram shows the PARALLEL COMMUNICATION
parallel transmission of the letter “C” in binary
(01000011).
In serial communication, the bits are sent one by
one through a single wire. The following diagram
shows the serial transmission of the letter “C” in
binary (01000011).
SERIAL COMMUNICATION
SERIAL COMMUNICATION PARALLEL COMMUNICATION
One bit transferred at a time Eight bits transferred at a time
Simple circuit, as we will have one or two connectivity Complex Circuit, as we have 8 wires for 8 bits of
wires. One for transmission of data and the second for data, in which transmission and receiving of data
receiving of data. takes place.
Slow, as it follows the time sharing principal, i.e., bits Fast, as there are 8 different lines available for 8
will be sent one after another over the same line. different bits. All the bits are transferred parallelly.
8 Clock Pulses for 8 bits. Only One Clock Pule for 8 Bits

Cost efficient, as there are limited connection points in


Costly, due to more connection points.
the circuit.

Talking about Arduino, we will be left with more GPIO 8 pins are used for parallel connectivity and GPIO
pins while we are connected in serial communication, pins shortage can be experienced if you are planning
and we can use them to connect different hardware. to connect other hardware.

Used for short distance connectivity setup, as it will


be easy to maintain more connectivity points with
Used for long distance connectivity setup.
out any issues if they serve in limited size
environment.
Unequal Time Intervals
SYNCHRONOUS AND ASYNCHRONOUS SERIAL COMMUNICATION

Equal Time Intervals


Clock Signal Clock Signal
(SYNCHRONIZED)
Data frame (SYNCHRONIZED)

SYNCHRONOUS ASYNCHRONOUS

 In Synchronous transmission, data is sent in  In Asynchronous transmission, data is sent


form of blocks or frames and the time interval in form of bytes or characters and the time
of transmission is constant, where there is no interval of transmission is not constant, it
gap present between data blocks/frames. is random, resulting in gap present
 Synchronous serial interface always pairs its between data.
data line(s) with a clock signal, and all devices  The start and stop bits here are known as
on a synchronous serial bus share a common synchronization bits as they indicate to the
clock, i.e. sender and receiver are receiving device when the packet begins
synchronized. and ends.
Unequal Time Intervals

Equal Time Intervals


Clock Signal Clock Signal
(SYNCHRONIZED)
Data frame (SYNCHRONIZED)

SYNCHRONOUS ASYNCHRONOUS

 Efficient use of transmission lines is done in  The start bit transitions the data line from
synchronous transmission. a high (1) to a low (0) state. The stop bit
 Synchronous transmission is fast and costly. indicates the end of the data packet by
pulling the data line back to the high (1)
idle state.
 While in Asynchronous transmission, the
transmission line remains empty during a
gap in character transmission, which is not
a efficient use of transmission line. This is
slow and economical.
SIMPLEX AND DUPLEX (FULL & HALF) SERIAL COMMUNICATION
 Simplex mode: In simplex mode,
Sender can send the data but ONLY ONE
the sender can’t receive the
data. It is a unidirectional
communication.
 Half-duplex mode: In half-duplex
mode, Sender can send the data ONE BY ONE
and also can receive the data
one at a time. It is a two way
directional communication but
one at a time.
 Full duplex mode: In full-duplex
mode, Sender can send the data
SIMELTANIOUS
and also can receive the data
simultaneously. It is a two way
directional communication
simultaneously.
SERIAL COMMUNICATION PATTERNS IN ARDUINO

I2C - Inter-Integrated
Circuit

SPI - Serial
2
Peripheral Interface

1
UART stands for Universal
I2C

Asynchronous
Receiver/Transmitter
Selection of Serial or Parallel Communication depends on the following factors
•How many devices the microcontroller has to exchange data with?
•How fast the data exchange has to be?
•What is the distance between these devices?
•Is it necessary to send and receive data simultaneously?

 Around the world, there are many types of protocols that are used in transferring data between
two devices, but all these protocols are based on either Parallel Communication or Serial
Communication.
 When coming to Arduino, the communication between Arduino UNO (or any other board) and
computer is serial communication. The main purpose of this serial communication is to, send
information from Arduino to computer and vice-versa. In Arduino environment, in order to begin
or initiate the serial communication, we need to use a predefined function called “Serial.begin”.
And the communication can be seen using Serial Monitor and Serial Plotter.
 7 Segment display, 16x2 LED and other few Sensors and Actuators connected to Arduino UNO
can be understood as parallel communication scenario.
INTERFACING LCD CHARACTER DISPLAY
 LCD (Liquid Crystal Display)
Character Displays are ideal
for displaying text and it is LCD 16×2
mostly used to represent the
information generated by any
electronic equipment in an
easy way and at low cost.
 Interfacing with Arduino,
these LCD modules provide a
very useful user interface that
helps in displaying status
messages & sensor readings.
They are extremely common
and fast way to add a
readable interface to your
Arduino project.
 This display module has too much use in most of the commercial projects and there is almost a
library in every programming language about it. LiquidCrystal.h is the Arduino’s LCD Library and
this library allows an Arduino board to control liquid crystal displays (LCDs) based on the Hitachi
HD44780 (or a compatible) chipset, which is found on most text-based LCDs.
 These character LCDs come in many sizes 8×1 , 8×2 , 10×2 , 16×1 , 16×2 , 16×4 , 20×2 , 20×4 ,24×2 ,
30×2 , 32×2 , 40×2 etc in which this size indicates rows and columns of the LCD; In LCD 16×2
module, there are 2 rows and 16 columns and can display 32 ASCII characters (8 bit each) in two
rows of 16 characters each. Each of these rectangles is a grid of 5×8 pixels, in which one character
can be hold.

 The backlights of these LCDs are available in various colors including yellow/green, white, red,
blue, green, amber, and RGB LEDs as well as no backlight option.
Pin1 VSS GROUND
Pin2 VDD VOLTAGE INPUT - 5V
Connect to 3 Pin POTENTIOMETER (GND, PIN, VOLTAGE),
Pin3 VE
that helps to control the CONTRAST of pixels/display
REGISTER SELECT pin, helps to toggle the COMMAND/DATA
Pin4 RS REGISTER. RS=0, command code register is selected, RS=1,
the data register is selected.
The signal on Pin5 will decide whether it is going to READ
Pin5 R/W FROM LCD OR WRITE on it. R/W is set 1 (Vcc) while reading
and R/W is set 0 (GND) when writing.
ENABLE pin will help to transfer the instruction from the
Pin6 EN data pins and other command pins to the LCD. It act as
permission to instructions to get processed.
Pin7 D0
Pin8 D1 D0-D1 DATA PINS - the first 4 bits input pins of the 8-bit
Pin9 D2 input data to the LCD.
Pin10 D3
Pin11 D4
Pin12 D5 D4-D7 DATA PINS - D0-D1 DATA PINS - the first 4 bits input
LCD 16×2 pins of the 8-bit input data to the LCD. In some cases, alone
Pin13 D6 usage of the any 4-pins can make the device functional.
Pin14 D7
Pin15 +ve BACKGROUND LED - VOLTAGE
Pin16 -ve BACKGROUND LED - GROUND
INTERFACING WITH ARDUINO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
INTERFACING WITH ARDUINO - WRITING A SKETCH IN IDE
 The sketch begins by installing LiquidCrystal library in your IDE (open SKETCH in menu bar ->
Include Library -> Manage Libraries -> Search for LiquidCrystal and Install the library).
LiquidCrystal is the library of Arduino community, which makes programming of LCD modules
hassle free.
 After having the visual confirmation about library installation in library manager, start writing the
sketch by including the LiquidCrystal library (#include<LiquidCrystal.h>) in the first line of sketch
which allows us to use the functions of this library in our sketch to control the LCD.
 Below are the functions of LiquidCrystal library; which are ready to be used in sketch.

LiquidCrystal() blink() rightToLeft()


begin() noBlink() createChar()
clear() display()
home() noDisplay()
setCursor() scrollDisplayLeft()
write() scrollDisplayRight()
print() autoscroll() ABOUT LCD FUNCTIONS
cursor() noAutoscroll() https://www.arduino.cc/
reference/en/libraries/li
noCursor() leftToRight() quidcrystal/
ARDUINO SKETCH
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 2, 3, 4, 5); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

void setup()
{
lcd.begin(16, 2); // set up the LCD's number of columns and rows
lcd.clear(); // Clears the LCD screen
}

void loop()
{
lcd.print("RVRJCCE"); // Print a message to the LCD
delay(1000);
lcd.clear();
lcd.setCursor(0, 1); // (note: line 1 is the second row, since counting begins with 0)
lcd.print(millis());
delay(1000);
lcd.clear();
}
OBSERVED OUTPUT

RVRJCCE is getting printed in 0th row and Output of millis() is getting printed in 1st row. The
functionality of delay(1000) and lcd.clear() functions can be observed clearly.
DIFFERENCE BETWEEN INTERFACING CHARACTER 16×2 LCD IN 4-BIT AND 8-BIT
 Since character lcd’s has 8 data pins so they
accept only 8-bit data. Character datatype
is 8-bit wide. These 8 bits are sent to lcd
using 8 data pins easily. This mode is know
as 8-bit mode.
 In 4 bit mode only four data pins are used.
DISPLAYING
Character 8-bit ASCII value is divided in to
CHARACTER 'Y' IN
two 4-bit portions. So in theory two strokes LCD
are needed to send a character(8-bit) to lcd IN 8 BIT AND 4 BIT
when we are using lcd in 4-bit mode. MODE.
 8-bit mode is much faster than 4-bit mode The binary value
of Y is 01011001
because it takes half the time compared to (8 bits).
4-bit mode. 4-bit mode is often used to
save I/O pins. However, 8-bit mode is best
used when speed is required in an
application and there are at least 10 I/O
pins available.
UART COMMUNICATION | UART PROTOCOL
 UART stands for Universal Asynchronous Receiver/Transmitter. It is a hardware circuit/protocol
used for serial communication between two devices. Connecting two UART devices together is
simple and straightforward. Below representation shows the 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 1 UART 2

Tx - Transmitter
Rx - Receiver

 A given UART may use just one signal for unidirectional “simplex" communication, or two signals
for bidirectional “duplex” communications. Bidirectional communications can be “full-duplex",
with information flowing in both directions simultaneously, or “half-duplex", where information
only flows in one direction at a time.
 UART works by converting data into packets for sending or rebuilding data from packets
received. UART refers to a protocol in which serial data communication will happen without
clock signal, i.e. in Asynchronous manner, as specified in it’s name, UART.
 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 bit, a data frame, parity bit, and the stop bits.
START BIT
*Numbers in packet indicate length
Start bit is a synchronisation bit that is added before the
actual data. Start bit marks the beginning of the data
packet. Usually, an idle data line i.e. when the data
transmission line is not transmitting any data, it is held at
a high voltage level (1). In order to start the data transfer,
the transmitting UART pulls the data line from high
voltage level to low voltage level (from 1 to 0). The
receiving UART detects this change from high to low on
the data line and begins reading the actual data. Usually, Rx Tx
there is only one start bit. DATA TRANSMISSION DIRECTION
DATA BITS OR DATA FRAME
The data bits include the real data being conveyed from the sender to receiver. The data frame
length could be between 5 & 8. If the parity bit is not used when the data frame length could be 9-
bit long. Order of the bits in the data frame is concerned, where the LSB (LEAST-SIGNIFICANT-BIT) of
the data to be transmitted first, where in data 1100000, 1 in first position is MSB (MOST-
SIGNIFICANT-BIT) and 0 in last position is LSB.
PARITY BIT
 Parity allows the receiver to check whether the received data is correct or not. Parity is a low -
level error checking system and comes in two varieties: Even Parity and Odd Parity.
 For example, if the letter a (binary 1100001) is transmitted under even parity, the sending
system adds the number of binary 1s, which in this case is three, and makes the parity bit 1 to
maintain an even number of binary 1s. If the letter A (binary 1000001) is transmitted under the
same circumstances, the parity bit would be a 0, thus keeping the total number of binary 1s an
even number.
 Similarly Odd Parity, operates under the same guidelines as even parity except that the total
number of binary 1s must be an odd number.
 Parity bit is optional and it is actually not that widely used.
STOP BITS
The Stop Bit, as the name suggests, marks the end of the data packet. In order to end the
transmission, the UART maintains the data line at high voltage (1). Stop bits can be one or two, i.e.
this makes shifting of low voltage - 0 to a high voltage - 1 for one to two bit(s) duration. Often only
on bit is used as stop bit. But in earlier systems, It was two bits long, considering that it will give
extra time to get ready for the device to receive further transmitted data.
LSB
UNDERSTANDING UART DATA PACKET WITH EXAMPLE

Line is high
(5V), when
unused
Line is low
(0V), when
used
MSB
Rules of UART
As mentioned earlier, there is no clock signal in UART and the transmitter and receiver must agree
on some rules of serial communication for error free transfer of data. The rules include:
1. Synchronisation Bits (Start and Stop bits)
2. Parity Bit
3. Data Bits and
4. Baud Rate
Advantages & Disadvantages of UART
ADVANTAGES DISADVANTAGES
 Requires only two wires for full duplex  Size of the data in the frame is limited.
data transmission (apart from the power  Speed for data transfer is less compared to
lines).
parallel communication.
 No need for clock or any other timing
 Transmitter and receiver must agree to the
signal.
rules of transmission and appropriate baud
 Parity bit ensures basic error checking is rate must be selected.
integrated in to the data packet frame.
UART IN ARDUINO UNO (USART)
 Arduino Uno has an in-built UART (part of
USART) support which is useful for serial
communication. It can be used to
communicate with another Arduino or PC
or various serial devices like ESP8266,
GSM, GPS, Bluetooth, etc.
 Pins 0 and 1 on the Arduino Uno are used
for serial communication. 0 is for Rx and 1
is for Tx. Using these pins we can connect
to another Arduino (Rx to Tx of other
Arduino/Tx to Rx of other Arduino). Along
with this 0 and 1 pins, Arduino has USB to
Serial converter which allows the Arduino
to do serial communication over USB with UART
a computer. While communicating with
computer using USB, the pins 0 and 1 will
be active, as they are connected to the USB
to Serial converter.
 During an active UART session with PC, if the pins 0 and 1 are used as normal digital pins, i.e.
Connecting any sensor hardware to these pins, can cause interference between the UART
session and whatever other use you are making of them with sensor hardware
 Serial.begin( ) in the Arduino Sketch, tells the serial object to perform initialization steps to send
and receive data on the Rx and Tx.
 Arduino IDE has integrated Serial Monitor, which can be used to watch serial data, sent or
received to or from computer or arduino and Serial Plotter, which can be used to visualize the
data receiving on computer serial port in the form of waveforms.
 The Serial.begin( ) wil also sets the baud rate for serial data communication. The baud rate
signifies the data rate in bits per second. The serial.begin (4800 ) open the serial port and set
the bits per rate to 4800. The messages in Arduino are interchanged with the serial monitor at a
rate of 4800 bits per second. If baud rate mentioned in Serial.begin() of two devices (between
two Arduino boards or between Arduino and Serial Monitor of PC) mismatched, then we will
get nothing or garbage value.
SETTING UP BASIC UART COMMUNICATION ON THE ARDUINO UNO
 Project is about controlling
the built-in LED of an Arduino
remotely via UART. A push-
button wired to the first Uno
board will control the built-in
LED of the second Uno board
and vice versa.
 For this project, both boards
will have identical sketches.
 Please remember to
disconnect the wires
connected to TX0 and RX0
pins before uploading the
sketch. After uploading
successfully, reconnect the
wires on TX0 and RX0 pins.
ARDUINO SKETCH
void setup()
{
pinMode(8, INPUT_PULLUP); // set push button pin as input
pinMode(13, OUTPUT); // set LED pin as output
digitalWrite(13, LOW); // switch off LED pin
Serial.begin(9600); // initialize UART with baud rate of 9600 bps
}

void loop()
{
if (Serial.available()) // returns the number of characters (i.e. bytes of data) which have
arrived in the serial buffer and that are ready to be read.
{
char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd
if (data_rcvd == '1') digitalWrite(13, HIGH); // switch LED On
if (data_rcvd == '0') digitalWrite(13, LOW); // switch LED Off
}
if (digitalRead(8) == HIGH) Serial.write('0'); // send the char '0' to serial if button is not pressed.
else Serial.write('1'); // send the char '1' to serial if button is pressed.
}
INTERFACING ESP8266 MODULE | UART COMMUNICATION
 The ESP8266 is a system on a chip (SOC) Wi-Fi microchip for Internet of
Things (IoT) applications produced by Espressif Systems. Given its low
cost, small size and adaptability with embedded devices, the ESP8266 is
ESP8266 SoC
now used extensively across IoT devices.
 ESP8266 provide internet connectivity to small-scale embedded
systems/projects using TCP/UDP communication protocol to connect
with the server/client.
 As this module is a SOC based single-chip CPU, several third party
manufacturers started manufacturing custom boards and one such
manufacturer is Ai-Thinker. The first board manufactured by Ai-Thinker is
ESP-01 and it became quite popular. Based on the success of the ESP-01
Module, several other modules like ESP-02, ESP-07, ESP-12, etc. were
released by Ai-Thinker. All these boards are based on ESP8266 SoC but
the main difference is the number of GPIO Pins. There are other well
known modules based on this ESP8266 SoC, like NodeMCU, a famous
open source platform and ESP-WROOM by Espressif Systems and many
others. ESP - 01
ESP - 01 | ESP 8266 MOULE ESP 8266 SoC ANTENNA
 The ESP8266 ESP-01 is a TCP/IP protocol based Wi-Fi
FLASH STATUS
module that allows microcontrollers access to a Wi-Fi MEMORY LED
network. 512kB
 ESP-01 module is termed as a system on chip (SOC)
POWER
because it acts as a standalone Microcontroller itself, so
LED
we don't need to interface it with any other
microcontroller (i.e. Arduino, Atmel, PIC Microcontroller, 26 MHz
etc.) in order to use its I/O Pins. GPIO & CRYSTAL
SYSTEM PINS
The ESP8266 ESP-01 module has three operation modes:
HIHLIGHTS:
1. Access Point (AP) o The ESP 01 ESP8266 Serial WIFI Wireless
2. Station (STA) Transceiver Module
o Powered by Integrated low power 32-bit
3. Both CPU
o Its operating temperature range is -40C to
 In AP MODE the Wi-Fi module acts as a Wi-Fi access 125C.
point, allowing other devices to connect to it. It simply o Its wake up and transmit packets is less
establishes a two way communication between the than 2ms.
o Standby power consumption of < 1.0mW
ESP8266 and the device that is connected to it via Wi-Fi.
PIN PIN ALTERNATE
 In STA MODE, the ESP-01 can connect to an AP NUMBER NAME NAME
USED FOR
such as the Wi-Fi network from your house. This Connected to the ground of the
1 Ground -
allows any device connected to that network to circuit
communicate with the module. Connected to Rx pin of
programmer/uC to upload
 The BOTH MODE of operation permits the 2 TX GPIO - 1 program and Can act as a General
purpose Input/output pin when
module to act as both an AP and a STA. not used as TX

3 GPIO-2 - General purpose Input/output pin

4 CH_PD - Chip Enable - Active high

General purpose Input/output pin


and can also be used to take
5 GPIO - 0 Flash module into serial programming
when held low during start up.

6 Reset - Resets the module

General purpose Input/output pin


and Can act as a General purpose
7 RX GPIO - 3 Input/output pin when not used
as RX

ESP - 01 PINS 8 Vcc - Power Supply 3.0 to 3.6V


INTERFACING ESP - 01 WITH ARDUINO UNO

CLICK ON BELOW LINK FOR DETAILS NOTE: The ESP8266 is not compatible with
5V and the ESP-01 Module does not have
https://github.com/RamNishanth/e any voltage regulators on-board. So it
sp01 should be connected only to 3.3V in Arduino
board, otherwise board may get damaged.
SPI COMMUNICATION | SPI PROTOCOL
 Devices communicating via SPI (Serial Peripheral
Interface) are in a master-slave relationship. The
master is the controlling device (usually a
microcontroller), while the slave (usually a sensor,
display, or memory chip) takes instruction from the
master. This is a Synchronous Serial Communication
MASTER SLAVE
Protocol. SPI used 4 Pins/Wires to accomplish the
communication and the max speed achieved by using
The PINS used for SPI are;
SPI is 10 Mbps.
MOSI Master Output/Slave Input) - Line for
 The simplest configuration of SPI is a single master, the master to send data to the slave.
single slave system, but one master can control more MISO Master Input/Slave Output) - Line for
than one slave. Max no. of masters can be 1 and max the slave to send data to the master.
no. of slaves are theoretically unlimited. In practice,
SCLK Clock - Line for the clock signal.
the number of slaves is limited by the load
capacitance of the system, which reduces the ability SS/CS Slave Select/Chip Select - Line for the
master to select which slave to send data
of the master to accurately switch between voltage
to.
levels.
MULTIPLE SLAVES
 There are two ways to connect multiple slaves to the master. If the master has multiple slave
select pins, the slaves can be wired in parallel as shown in the below figure.

MASTER SLAVE - 1

SLAVE - 2

SLAVE - n
DATA TRANSMISSION STEPS
Data sent from the master to the slave is usually 3. THE MASTER SENDS THE DATA ONE BIT AT A TIME TO
sent with the most significant bit first. The data THE SLAVE ALONG THE MOSI LINE. THE SLAVE READS THE
BITS AS THEY ARE RECEIVED
sent from the slave back to the master is usually
MSB FIRST
sent with the least significant bit first. SLAVE
MASTER
1. THE MASTER OUTPUTS THE CLOCK SIGNAL
MASTER SLAVE

4. IF A RESPONSE IS NEEDED, THE SLAVE RETURNS DATA


2. THE MASTER SWITCHES THE SS/CS PIN TO A LOW ONE BIT AT A TIME TO THE MASTER ALONG THE MISO
VOLTAGE STATE, WHICH ACTIVATES THE SLAVE. DURING LINE. THE MASTER READS THE BITS AS THEY ARE RECEIVED
MULTIPLE SLAVES, ONLY CONCERNED SS WILL BE IN WORK
AND REMAINING SS PINS WILL BE INACTIVE. LSB FIRST
MASTER SLAVE MASTER SLAVE
 One unique benefit of SPI is the fact that data can be transferred without interruption. Any
number of bits can be sent or received in a continuous stream. With I2C and UART, data is sent
in packets, limited to a specific number of bits. Start and stop conditions define the beginning
and end of each packet, so the data is interrupted during transmission. Where as, SPI Uses four
wires (I2C and UARTs use two), which may be considered as disadvantage of SPI.
 SPI is a common communication protocol used by many different devices. For example, SD
card reader modules, RFID card reader modules, etc.
I2C COMMUNICATION | I2C PROTOCOL
 I2C (Inter-Integrated Circuit) combines the best features of SPI
and UARTs. With I2C, you can connect multiple slaves to a single
master (like SPI) and you can have multiple masters controlling MASTER SLAVE
single, or multiple slaves. Like UART, I2C uses the special data
packets format to ensure hassle free reliable communication.
I2C is useful when you want to have more than one
microcontroller logging data to a single memory card or
displaying text to a single LCD. At maximum, unlimited no. of
masters and 1008 no. of slaves can be connected.
 Like SPI, I2C is synchronous, so the output of bits is synchronized
to the sampling of bits by a clock signal shared between the
master and the slave. The clock signal is always controlled by the
master.
Like UART communication, I2C communication only uses two wires to transmit data between devices.
1. SDA (Serial Data) - The line for the master and slave to send and receive data.
2. SCL (Serial Clock) - The line that carries the clock signal.
WORKING OF I2C
With I2C, data is transferred in messages. Messages are broken up into frames of data. Each message
has an address frame that contains the binary address of the slave, and one or more data frames
that contain the data being transmitted.
The message also includes start and stop conditions, read/write bits, and ACK/NACK bits between
each data frame:

Start Condition: The SDA line switches from a high voltage level to a low voltage level before the SCL
line switches from high to low.
Stop Condition: The SDA line switches from a low voltage level to a high voltage level after the SCL
line switches from low to high.
Address Frame: A 7 or 10 bit sequence unique to each slave that identifies the slave when the master
wants to talk to it.
Read/Write Bit: A single bit specifying whether the
master is sending data to the slave (low voltage
level - 0) or requesting data from it (high voltage
level - 1).
ACK/NACK Bit: Each frame in a message is followed
by an acknowledge/no-acknowledge bit. If
an address frame or data frame was successfully
received,
received an ACK bit is returned to the sender from the receiving device. If address not found then
NACK will be received by Master.
ADDRESSING PROCESS
 I2C doesn’t have slave select pin like SPI, so it needs another way to let the slave know that data is
being sent to it, and not another slave. It does this by addressing. The address frame is always the
first frame after the start bit in a new message.
 The master sends the address of the slave it wants to communicate with to every slave connected
to it. Each slave then compares the address sent from the master to its own address. If the address
matches, it sends a low voltage ACK bit back to the master. If the address doesn’t match, the slave
does nothing and the SDA line remains high. It is NACK received by Master.
THE DATA FRAME
 After the master detects the ACK bit from the slave, the first data frame is ready to be sent.
 The data frame is always 8 bits long, and sent with the most significant bit first. Each data frame is
immediately followed by an ACK/NACK bit to verify that the frame has been received
successfully. The ACK bit must be received by either the master or the slave (depending on who
is sending the data) before the next data frame can be sent.
 After all of the data frames have been sent, the master can send a stop condition to the slave to
halt the transmission. The stop condition is a voltage transition from low to high on the SDA line
after a low to high transition on the SCL line, with the SCL line remaining high.
WORKING OF I2C
SENDING DATA TO A SLAVE DEVICE
WORKING OF I2C
READING DATA FROM A SLAVE DEVICE
SDA

INTERFACING BMP 180 MODULE BMP180 SCL


by
 It’s a basic sensor that is designed specifically
BOSCH GND
for measuring atmospheric pressure, which is
also useful for two things; VCC
1. As we travel from sea level to a mountain 3.3v
peak, the air pressure gets lower. That LDA
means by measuring the pressure we can Rregulator
determine the altitude. So, we can use this
sensor as an Altimeter. The BMP180 consumes less than 1mA power during
measurements and only 5μA power during idle. This low
2. Because the atmospheric pressure changes power consumption allows the implementation in battery
with the weather, we can use it to monitor driven devices. (mA - Milliampere; μA - Microampere)
changes in the weather. BMP180 CAN MEASURE …
PROPERTY RANGE
 These sensors are fairly simple to use, pre- BAROMETRIC PRESSURE 300 hPa to 1100 hPa
calibrated and don’t require extra TEMPERATURE -40°C to 85°C
components so you can start measuring
ALTITUDE 0 to 30,000 Ft
barometric pressure, altitude and
temperature in no time. 1 Hectopascal(hPa) to Pascal(Pa) | degree Celsius (symbol: °C) | Ft - Foot
CONNECTING TO ARDUINO UNO

Note that each Arduino


Board has different I2C
pins which should be
connected accordingly.
Arduino Nano A5 A4
Arduino Mega 21 20  VCC is the power supply for the module which can be anywhere between 3.3V to 5V,
Arduino Uno A5 A4 so it is connected to 5V of Arduino. GND connected to the ground of Arduino.
UNO got And another  SCL is a serial clock pin for I2C interface, connected to A5 and SDA is a serial data pin
set of unnamed pins
on top of AREF for I2C interface, connected to A4 of UNO.
SKETCH IN ARDUINO IDE
Sketch >
Include
Library >
Manage
Libraries…
Wait for
Library
Manager to
download
libraries
index >
Search for
BMP 180C

INSTALLING NECESSARY LIBRARIES


 Calculating the altitude and barometric pressure with BMP180 module needs a lot of math.
Fortunately, Adafruit BMP180 Library was written to hide away all the complexities so that we
can issue simple commands to read the temperature, barometric pressure and altitude data.
 Filter your search by typing ‘bmp180’. There should be a couple entries. Look for Adafruit
BMP085 Library by Adafruit. Click on that entry, and then select Install.
CODE
#include <Wire.h>
#include <Adafruit_BMP085.h>
#define seaLevelPressure_hPa 1013.25

Adafruit_BMP085 bmp;

void setup()
{
Serial.begin(9600);
if (!bmp.begin())
{
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1)
{
}
}
}
void loop()
{
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");

Serial.print("Pressure at sealevel (calculated) = ");


Serial.print(bmp.readSealevelPressure());
Serial.println(" Pa");
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(seaLevelPressure_hPa * 100));
Serial.println(" meters");

Serial.println();
delay(500);
}
OUTPUT
INTERFACING ADXL345 MODULE (Accelerometer)
 ADXL345 Accelerometer is used to measure the acceleration or change in velocity in x, y, and z-
axis. This module can be used in both SPI and I2C Communication Modes.
 Accelerometers are widely used in low-power, low-cost motion and tilt sensing applications such
as Mobile Devices (Auto Rotation), Disk Drive Protection, image stabilization, Car Crash Detection
and various sports and health devices.
Voltage
 It can be interface with 3.3V or 5V Regulator
Microcontroller.
 Ultra-Low Power: 40uA in measurement ADXL 345
mode, 0.1uA in standby
 Free-Fall Detection
 SPI and I2C interfaces
 Measuring Range: ±16g
 Measuring Values (-16g to +16g):
Level
 X: -235 to +270
Shiter
 Y: -240 to +260
 Z: -240 to +270
ADXL 345 PIN DETAILS
 GND - Connect this to the GND pin on your development board to make sure they are sharing a
common GND connection.
 VCC - Power Supply pin (3V to 6V)
 SCL - The clock line on the I2C bus. This also functions as SCK in SPI mode.
 SDA - The data line on the I2C bus. This also functions as MOSI in SPI mode.
 SDO - This pin can be used as MISO in SPI mode.
 SDO - This pin can be used as MISO in SPI mode.
 INT1 and INT2: There are two optional interrupt output pins on this sensor, which can be
configured to change their state when one or more 'events' occur.
ADXL 345 WORKING
 The basic structure of the accelerometer
consists of fixed plates and moving plates.
When the acceleration is applied on an axis,
capacitance between fixed plates and moving
plates is changed. This results in a sensor
output voltage amplitude, which is
ACCELEROMETER’s MICRO ELECTRO MECHANICAL SYSTEM
proportional to the acceleration. (MEM) MECHANISM
CAPACITANCE
CONNECTING TO ARDUINO UNO

Note that each Arduino


Board has different I2C
pins which should be
connected accordingly.
Arduino Nano A5 A4
Arduino Mega 21 20  VCC is the power supply for the module which can be anywhere between 3.3V to 5V.
Arduino Uno A5 A4  GND should be connected to the ground of Arduino.
UNO got And another  SCL is a serial clock pin for I2C interface, A5 of UNO.
set of unnamed pins
on top of AREF
 SDA is a serial data pin for I2C interface, A4 of UNO.
SKETCH- IN
SKECH ARDUINO
ARDUINO IDE
UNO
INSTALLING NECESSARY LIBRARIES
Before starting to write the code, Install libraries. Sketch > Include Library > Manage Libraries… Wait for Library Manager
to download libraries index > Adafruit ADXL345 and install it. Similarly, search for Adafruit Unified sensor and install.
CODE
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

void setup(void)
{
Serial.begin(9600);
if(!accel.begin())
{
Serial.println("No valid sensor found");
while(1);
}
}
void loop(void)
{
sensors_event_t event;
accel.getEvent(&event);
Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");
Serial.println("m/s^2 ");
delay(500);
}
OUTPUT
INTERFACING SD CARD MODULE
 Secure Digital (SD) is a non-volatile memory card
format developed by the SD Card Association (SDA)
for use in portable devices like mobile phones,
cameras etc.
 This is a cheap and easy-to-use module that can be
used for many different applications. This module can
be used to read/write data from/to SD Card with The Micro SD Card module has 6 pins; those are
microcontroller. This is a way to store a lot of log data GND, VCC, MISO, MOSI, SCK, and CS. All the pins of
this sensor module are digital, except VCC and
and other information, like a GPS logger or a Ground.
temperature logger.
 A standard microSD card module has an operating
voltage of 3.3 V. As a result, we cannot connect it
directly to circuits that use 5V logic; in fact, any
voltages above 3.6V may permanently damage the
microSD card. That is why the module includes an
onboard ultra-low dropout voltage regulator capable
of regulating voltage to 3.3V.
CONNECTING TO ARDUINO UNO
 Connecting the Micro SD Card module to the
Arduino UNO is very simple we just need to
connect the module’s SCK, MISO, and MOSI, to
the Arduinos SPI pins, that is SCK(D13),
MOSI(D12), and MISO(D11). We are using
digital pin 4 for CS (Chip Select).
Preparing the Micro SD Card Module
Before inserting the SD Card into the SD card
reader module, you need to properly format the
card before you can actually work with it,
otherwise, you would have problems because the
SD card reader module can only read FAT16 or
FAT32 file systems. If your sd card is new then
chances are that the card is factory formatted, that
may also not work because a pre-formatted card
comes with a FAT file system, either way, it's
better to format the old card to reduce issues
during operation.
 It is recommended that you use the official SD card formatter utility developed by the SD
association. It can solve a lot of problems caused by bad formatting! Download and run the
formatter on your computer; simply select the appropriate drive and click Format. You may also
use the normal format option in the PC.
SKETCH IN ARDUINO IDE
INSTALLING NECESSARY LIBRARIES
Before starting to write the code, Install libraries. Sketch > Include Library > Manage Libraries… Wait for Library Manager
to download libraries index > Search for SPI and next SD and install them.

 Communicating with an SD card is a lot of work, but luckily for us, the Arduino IDE already
includes a very useful library called SD that makes reading and writing SD cards easier.
Important SD Module Library Commands
Description Command
Initializes the SD library and card. Enter the pin connected to the SS
pin as a function’s argument. SD.begin(#sspin)

Tests whether a file or directory exists on the SD card. SD.exists(filename)


Opens a file on the SD card in reading or writing mode. (If you leave
the mode section blank, the file will open in reading mode by default) SD.open(filepath, mode)
If the file is opened for writing, it will be created a file with this name
if it doesn’t already exist.
Close the file and ensure that any data written to it is physically saved file.close()*
to the SD card.
Description Command
Remove a file from the SD card. SD.remove(filename)
Create a directory on the SD card SD.mkdir(filename)
Remove a directory from the SD card. SD.rmdir(filename
Returns the file name file.name()*
Print data to the file file.print(data)
Print data, followed by a carriage return and newline file.println(data
Read from the file. file.read()
Check if there are any bytes available for reading from the file. file.available()
CODE - CARD INFO
 Let’s start with a simple CardInfo example sketch. This sketch doesn’t write any data to the card.
Instead, it tells you if the card is recognized and shows you some information about it. This can
be very useful when determining whether or not an SD card is supported. It is therefore
recommended that you run this sketch once before trying out a new card. To open the CardInfo
example sketch, navigate to File > Examples > SD > CardInfo.
 Upload the code to your Arduino board. Make sure you have the right board and COM port
selected. Open the Serial Monitor at a baud rate of 9600 and you should see your SD card
information. If everything is working properly, you’ll see a similar message on the serial monitor.
CODE - WRITING DATA ON SD CARD
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
// Open serial communications and wait for port to open:

Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only

}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,

// so you have to close this one before opening another.

myFile = SD.open("test.txt", FILE_WRITE);


// if the file opened okay, write to it:

if (myFile)
{
Serial.print("Writing to test.txt...");
myFile.println("This is a test file :)");
myFile.println("testing 1, 2, 3.");
for (int i = 0; i < 20; i++)
{
myFile.println(i);
}
// close the file:

myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:

Serial.println("error opening test.txt");


}
}
void loop(){
} // nothing happens after setup
OUTPUT
CODE - READING DATA FROM SD CARD
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
// Open serial communications and wait for port to open:

Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only

}
Serial.print("Initializing SD card...");
if (!SD.begin(10))
{
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file for reading:

myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:

while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:

myFile.close();
} else
{
// if the file didn't open, print an error:

Serial.println("error opening test.txt");


}
}
void loop()
{
// nothing happens after setup

}
OUTPUT

You might also like