CSD513: Internet of Things
Serial Communication in Microcontroller
W -> 3 and L- > 3
1 External
Introduction to Microcontroller Communication
What is Communication in Microcontrollers?
Transfer of data between microcontroller and
peripherals or other microcontrollers.
Ensures proper functioning of embedded
systems.
Importance:
Seamless integration of sensors, actuators, and
modules.
Data sharing in IoT systems
2 External
Types of Communication Protocols
Serial Communication:
Data transmitted one bit at a time (e.g., SPI, I2C,
UART).
Parallel Communication:
Multiple data bits sent simultaneously.
Synchronous vs. Asynchronous:
Synchronous: Uses a clock signal (e.g., SPI, I2C).
Asynchronous: No clock signal (e.g., UART).
3 External
Key Protocols in Microcontrollers
SPI (Serial Peripheral Interface)
I2C (Inter-Integrated Circuit)
UART (Universal Asynchronous Receiver
Transmitter)
CAN (Controller Area Network)
USB (Universal Serial Bus)
One-Wire Protocol
4 External
Introduction to SPI
What is SPI?
High-speed,
synchronous, full-duplex
communication protocol.
Key Features:
Master-slave
architecture.
Supports multiple slaves
using SS (Slave Select)
lines.
Clock-driven data
exchange. 5 External
SPI Components
Master: Initiates communication and provides the
clock signal.
Slave: Responds to the master’s commands.
Wires:
MOSI (Master Out Slave In)
MISO (Master In Slave Out)
SCLK (Serial Clock)
SS (Slave Select)
6 External
How SPI Works
Clock Signal (SCLK): Synchronizes data transfer.
Data Transmission:
Master sends data to slave via MOSI.
Slave responds via MISO.
Full-Duplex: Simultaneous data sending and
receiving.
7 External
SPI Advantages and Disadvantages
Advantages:
High-speed data transfer.
Simple hardware implementation.
Disadvantages:
Requires more pins (SS for each slave).
No acknowledgment mechanism.
8 External
Applications of SPI
SD card interfaces.
Flash memory.
OLED and LCD displays.
High-speed ADCs and DACs.
9 External
Introduction to I2C
What is I2C?
Multi-master, multi-
slave, synchronous,
serial communication
protocol.
Key Features:
Two-wire interface:
SDA (Data Line), SCL
(Clock Line).
Addressing mechanism
for multiple devices.
Use Cases: 10 External
I2C Components
Master: Initiates communication and generates the
clock.
Slave: Responds to master’s requests.
Wires:
SDA (Serial Data)
SCL (Serial Clock)
11 External
How I2C Works
Start Condition: Master pulls SDA low while SCL
remains high.
Addressing: Master sends 7-bit/10-bit address
followed by R/W bit.
Data Transfer: Data transmitted in 8-bit chunks
with acknowledgment (ACK/NAK).
Stop Condition: SDA goes high while SCL is high.
12 External
I2C Advantages and Disadvantages
Advantages:
Requires only two wires.
Supports multiple devices on the same bus.
Disadvantages:
Slower compared to SPI.
Limited cable length due to capacitance.
13 External
Applications of I2C
Applications of I2C
Temperature and pressure sensors.
EEPROM storage.
Real-Time Clocks (RTCs).
Battery management systems.
14 External
SPI and I2C with ESP8266
SPI and I2C with ESP8266
The ESP8266 microcontroller supports both SPI
and I2C communication protocols, which makes it
suitable for connecting with various peripherals
such as sensors, displays, and memory devices.
15 External
SPI (Serial Peripheral Interface) with ESP8266
Overview
SPI is a high-speed, full-duplex communication protocol.
The ESP8266 operates as both a master and a slave in SPI
communication.
It supports multiple devices using individual Slave Select
(SS) lines.
ESP8266 SPI Pins
MOSI (Master Out Slave In): GPIO13 (D7 on NodeMCU)
MISO (Master In Slave Out): GPIO12 (D6 on NodeMCU)
SCLK (Clock): GPIO14 (D5 on NodeMCU)
SS (Slave Select): Can be configured on any GPIO (typically
GPIO15)
16 External
SPI (Serial Peripheral Interface) with ESP8266
ESP8266 SPI Pins
MOSI (Master Out Slave In):
GPIO13 (D7 on NodeMCU)
MISO (Master In Slave Out):
GPIO12 (D6 on NodeMCU)
SCLK (Clock): GPIO14 (D5 on
NodeMCU)
SS (Slave Select): Can be
configured on any GPIO
(typically GPIO15)
17 External
SPI (Serial Peripheral Interface) with ESP8266
Configuration Steps
Initialize SPI:
Set the SPI clock frequency.
Configure SPI mode (Mode 0, 1, 2, or 3 based
on the clock polarity and phase).
Connect Devices:
Assign each slave a unique SS pin.
Transmit/Receive Data:
Use SPI write/read commands
18 External
SPI (Serial Peripheral Interface) with ESP8266
SPI (Serial Peripheral Interface) with ESP8266
Applications
Connecting SD cards, RFID modules, and TFT
displays.
Fast data transfer requirements.
19 External
I2C (Inter-Integrated Circuit) with ESP8266
I2C (Inter-Integrated Circuit) with ESP8266
Overview
I2C is a synchronous, multi-master, multi-slave protocol.
It uses only two wires: SDA (Data) and SCL (Clock),
making it simpler than SPI for multiple device
communication.
ESP8266 I2C Pins
SDA: GPIO4 (D2 on NodeMCU)
SCL: GPIO5 (D1 on NodeMCU)
These pins can be reassigned to other GPIOs in the code.
20 External
ESP8266 I2C Pins
ESP8266 I2C Pins
SDA: GPIO4 (D2 on
NodeMCU)
SCL: GPIO5 (D1 on
NodeMCU)
These pins can be
reassigned to other
GPIOs in the code.
21 External
I2C (Inter-Integrated Circuit) with ESP8266
Configuration Steps
Initialize I2C:
Configure the SDA and SCL pins.
Set the I2C clock speed (typically 100 kHz or
400 kHz).
Address Devices:
Assign a unique 7-bit or 10-bit address to each
slave.
Transmit/Receive Data:
Use start, address, read/write commands, and
stop conditions. 22 External
I2C (Inter-Integrated Circuit) with ESP8266
I2C (Inter-Integrated Circuit) with ESP8266
Applications
Connecting sensors like BMP180, DHT12, or OLED
displays.
Systems requiring multiple devices on the same
bus.
23 External
SPI vs. I2C on ESP8266
Feature SPI I2C
Pins Required 4 (MOSI, MISO, SCLK, SS) 2 (SDA, SCL)
Speed Up to 80 MHz Up to 400 kHz
Number of Devices Limited by SS pins Limited by I2C addresses
Complexity More complex (more pins) Simpler (fewer pins)
High-speed, dedicated Low-speed, multi-device
Use Cases
devices setups
24 External
SPI and I2C with Arduino Uno Rev3
SPI and I2C with Arduino Uno Rev3
The Arduino Uno Rev3 supports both SPI and I2C
communication protocols, which makes it
versatile for connecting peripherals such as
sensors, displays, and external memory modules.
25 External
SPI (Serial Peripheral Interface) with Arduino Uno Rev3
Overview
SPI is a high-speed, full-duplex,
synchronous communication
protocol.
Arduino Uno Rev3 acts as a master or
a slave in SPI communication.
Arduino Uno SPI Pins
MOSI (Master Out Slave In): Pin 11
MISO (Master In Slave Out): Pin 12
SCLK (Clock): Pin 13
SS (Slave Select): Pin 10 (used to
enable communication with a specific
slave)
26 External
SPI (Serial Peripheral Interface) with Arduino Uno Rev3
Configuration Steps
Initialize SPI:
Configure the SPI settings (clock speed,
data order, and SPI mode).
Connect Devices:
Assign a unique SS line for each slave
device.
Transmit/Receive Data:
Use SPI.transfer() to send and receive
data.
27 External
I2C (Inter-Integrated Circuit) with Arduino Uno Rev3
Overview
I2C is a synchronous, multi-master, multi-slave
communication protocol.
It uses two lines: SDA (Data) and SCL (Clock).
Arduino Uno I2C Pins
SDA (Data Line): Pin A4
SCL (Clock Line): Pin A5
Note: These pins are hardware-defined and
cannot be changed without using software
emulation libraries.
28 External
I2C (Inter-Integrated Circuit) with Arduino Uno Rev3
Configuration Steps
Initialize I2C:
Use the Wire library to set up the SDA and
SCL pins.
Address Devices:
Each device on the bus is identified by a
unique 7-bit or 10-bit address.
Transmit/Receive Data:
Use start, address, read/write, and stop
commands.
29 External
SPI vs. I2C on Arduino Uno Rev3
Feature SPI I2C
4 (MOSI, MISO, SCLK,
Pins Required 2 (SDA, SCL)
SS)
Standard: 100 kHz;
Speed Up to 8 MHz
Fast: 400 kHz
Limited by I2C
Number of Devices Limited by SS lines
addresses
More complex (more
Complexity Simpler (fewer pins)
pins)
High-speed Low-speed, multi-
Use Cases
communication device communication
30 External
SPI vs. I2C
SPI:
Faster, supports full-duplex.
Requires more pins.
I2C:
Slower, supports more devices.
Acknowledgment mechanism.
31 External
Protocol Selection Criteria
Protocol Selection Criteria
Speed requirements.
Number of devices.
Hardware complexity.
Power consumption.
32 External
Configuring SPI
Configuring SPI
Steps:
Set SPI mode (clock polarity and phase).
Configure master/slave mode.
Enable SPI peripheral.
Example Code:
Basic SPI initialization snippet.
33 External
Configuring I2C
Configuring I2C
Steps:
Set I2C clock speed.
Configure master/slave address.
Enable I2C peripheral.
Example Code:
Basic I2C initialization snippet.
34 External
Real-Time Data Exchange Example
Real-Time Data Exchange Example
Scenario: Combining SPI and I2C.
Example: Reading sensor data via I2C and
transmitting via SPI.
35 External
Clock Stretching in I2C
Clock Stretching in I2C
What is Clock Stretching?
Slave holds SCL low to delay communication.
Purpose:
Allows slower devices to keep up.
36 External
Error Handling in Communication Protocols
Error Handling in Communication Protocols
Common Errors:
Bus contention, data corruption.
Strategies:
Timeout mechanisms.
Error detection and retry.
37 External
Thank You