Communication Protocols
Communication Protocols
Protocols
Serial Communication
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
void setup() {
Serial.begin(9600);
void loop() {
counterValue = counterValue + 1;
Serial.write(START_BYTE);
Serial.write(counterValue);
Serial.write(staticValue);
Serial.write(checksum);
delay(250);
}
Receiver
// Receiver Information
void setup() {
Serial.begin(9600);
//Serial2.begin(9600);
void loop() {
if (Serial.available() > 0) {
// If we're waiting for a new packet, check for the sync byte
if (syncByteFound == 0) {
rxByte = Serial.read();
// 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();
Serial.print("[");
Serial.print("S");
Serial.print("]");
SoftwareSerial
void setup()
Serial.begin(9600);
while (!Serial) {}
mySerial.begin(9600);
mySerial.write('H');mySerial.write('e');mySerial.write('l');mySerial.write('l');mySerial.write('o');
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
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
Number of wires 2 2 4