Communication
Protocols
Serial Communication
the process of sending/receiving data in one bit at a time.
Parallel Communication
the process of sending/receiving multiple data bits at a time
through parallel channels.
Serial vs Parallel Communication
Synchronous vs Asynchronous
In synchronous, each basic unit of data is transferred in accordance
to a clock COMMUNICATION signal or in other words the data is
transferred at a pre-decided rate. So, for this data transfer method a
clock signal is needed.
In asynchronous, the data can be sent at irregular intervals and
there is no pre-decided data rate of transmission. Special bits such
as Start and stop bits are reserved to detect the start and end of
data transmission in these systems
Types of Communication
Simplex = data transmission in one direction
Half-duplex = data transmission in either direction but not
simultaneously
Full-duplex = data transmission in both directions simultaneously
Common Serial Communication
Protocols
UART
SPI
I2C
Universal Asynchronous Reception
and Transmission (UART)
Allows the host communicates with the auxiliary device
UART supports bi-directional(duplex), asynchronous(no clock) and serial
data transmission.
two data lines, one to transmit (TX) and another to receive (RX)
In the Arduino UNO Dpin0=RX and Dpin1=Tx
TX and RX are connected between two devices. (eg. USB and
computer)
UART can also handle synchronization management issues between
computers and external serial devices
Transmitting UART converts parallel data from the master device (eg.
CPU) into serial form and transmit in serial to receiving UART. It will then
convert the serial data back into parallel data for the receiving device
As UART has no clocks, UART
adds start and stop bits that
are being transferred to
represent the start and end
of a message
UART data transmission
speed is referred to as BAUD
Rate both UARTs must
operate at about the same
baud rate
Connecting two Uno board
int LEDstate=1;
void setup() {
Serial.begin(9600);
Serial.println("Waiting to connect");
while (!Serial) { Serial.print(".");}// wait for serial port to connect. Needed for native USB port only
Serial.println("Serial connected");
pinMode(13,OUTPUT); digitalWrite(13,LEDstate);
}
void loop() {
if (Serial.available()>0){
char c= Serial.read();
if (c=='H') LEDstate=!LEDstate;
Serial.write(c); digitalWrite(13,LEDstate);
delay(1000);
}
}
Connecting two UNO boards
Advanced example
Sender
// Sender Information
unsigned char START_BYTE = 0x53; // ASCII "S"
unsigned char counterValue = 0;
unsigned char staticValue = 5;
unsigned char checksum = 0;
void setup() {
Serial.begin(9600);
void loop() {
// Increment our counter
counterValue = counterValue + 1;
// Check for overflow, and loop
if (counterValue > 250) counterValue = 0;
// Calculate our checksum
checksum = counterValue + staticValue;
// Important: Serial.write must be used, not print
Serial.write(START_BYTE);
Serial.write(counterValue);
Serial.write(staticValue);
Serial.write(checksum);
// We only need to send a packet every 250ms.
// If your code starts to get complicated,
// consider using a timer instead of a delay
delay(250);
}
Receiver
// Receiver Information
unsigned char START_BYTE = 0x53; // ASCII "S"
unsigned char counterValue = 0;
unsigned char staticValue = 0;
unsigned char checksum = 0;
boolean syncByteFound = 0; // Sync Byte flag
void setup() {
Serial.begin(9600);
//Serial2.begin(9600);
void loop() {
unsigned char rxByte = 0;
unsigned char calculatedChecksum = 0;
// Check to see if there's something to read
if (Serial.available() > 0) {
// If we're waiting for a new packet, check for the sync byte
if (syncByteFound == 0) {
rxByte = Serial.read();
if (rxByte == 0x53) syncByteFound = 1;
// If we've found our sync byte, check for expected number of bytes
if (Serial.available() > 2) {
counterValue = Serial.read();
staticValue = Serial.read();
checksum = Serial.read();
calculatedChecksum = counterValue + staticValue;
// Print out our serial information to debug
Serial.print("[");
Serial.print("S");
Serial.print("]");
SoftwareSerial
The Arduino hardware has built-in support for serial communication
on pins 0 and 1. This hardware allows the Atmega chip to receive
serial communication even while working on other tasks, as long as
there room in the 64 byte serial buffer.
The SoftwareSerial library has been developed to allow serial
communication on other digital pins of the Arduino, using software
to replicate the functionality (hence the name "SoftwareSerial"). It is
possible to have multiple software serial ports
Software Serial Example
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9); // RX, TX
void setup()
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {}
Serial.println("Hardware Serial is ready to use");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
Serial.println("Software Serial is ready to use");
mySerial.write('H');mySerial.write('e');mySerial.write('l');mySerial.write('l');mySerial.write('o');
void loop() // run over and over
if (Serial.available()>0)
char c=Serial.read();
Serial.print(c);
}
Serial Peripheral Interface (SPI)
SPI is different from UART in
several key ways:
Synchronous
ollows a master-slave model,
where there is one master
device and multiple slave
devices
More than two lines required
for implementation
MOSI (“Master Out Slave In”): Data transmission line from master to slave
SCK (“Clock”): Clock line defining transmission speed and transmission
start/end characteristics
SS (“Slave Select”): Line for master to select a particular slave to
communicate with
MISO (“Master In Slave Out”): Data transmission line from slave to master
The hardware
diagram for
multiple slaves
SPI is suitable to connect
several sensors(slaves) to
a microcontroller (mater)
SPI implementation on the Arduino
SCK: GPIO 13 or ICSP 3
MOSI: GPIO 11 or ICSP 4
MISO: GPIO 12 or ICSP 1
SS: GPIO 10 or any digital pin
To select the device, this digital
pin must be driven low.
MFRC522 based RFID
https://randomnerdtutorials.com/security-access-using-mfrc522-rfid-
reader-with-arduino/
File > Examples > MFRC522 > DumpInfo
I2C Communication Protocol
i-squared-c, Inter-Integrated Circuit
Key features
the ability to connect multiple masters to multiple slaves
Synchronicity (just like SPI), which means higher speed communication
Simplicity: implementation only requires two wires and some resistors
I2C is unique because it solves the issue of interfacing with multiple
slave devices through addressing
Just like in SPI communication, I2C makes use of a master-slave
model to establish the “hierarchy” of communication.
However, instead of selecting slaves through separate digital lines,
masters select slaves through their unique byte addresses.
On the Arduino, I2C implementation occurs through the Wire library
(Wire.h).
The Arduino can be configured as either an I2C master or slave
device. On the Arduino Uno, the connections are as follows:
SDA: Analog Pin 4
SCL: Analog Pin 5
Protocol UART I2C SPI
Complexity Easy to chain Complex as device
Simple
multiple devices increases
Speed Slowest Faster than UART Fastest
Number of devices Up to 127, but gets Many, but gets
Up to 2 devices
complex complex
Number of wires 2 2 4
Duplex Full Duplex Half Duplex Full Duplex
No. of masters and Multiple slaves and 1 master, multiple
Single to Single
slaves masters slaves