Communication Protocols
Practical
Vimukthi Pathirana
MEng. (AIT), BSc (Hons) Eng. (AIT)
Content
UART
I2C
SPI
What is UART?
• Universal Asynchronous Receiver/Transmitter
• Hardware device used for serial communication between two devices
How UART works?
• One wire is for transmitting data (Tx pin)
• Other is for receiving data (Rx pin)
• UART convert data into packets for sending or rebuilding data from
packets received
How UART works?
• Data Packet
• What is the use of Parity Bit?
• What is the use of UART buffer?
• Can data loss occur? If so, how?
ARDUINO UART Interface
ARDUINO UART Interface
• Serial.begin(VALUE); - initialize UART
• VALUE – baud rate (generally 9600 bps)
• Serial.available(); - check if data waiting to read in buffer
• Serial.read(); - read data waiting in buffer
• Serial.write(VALUE); - send data via Tx pin
• VALUE – byte to be sent
ARDUINO UART Interface Example
ARDUINO UART Interface Example
What is I2C?
• Inter-Integrated Circuit
• Low speed serial communication protocol
used for transferring data over short
distances
• Consists of a master device and slave device
• Can have multiple master and slave devices?
How I2C works?
• Each slave device has an I2C address – used to identify
• Can master device send and receive data?
• Slave device responds to whatever master device sends
• Can only one device send data at a time?
ARDUINO I2C Network
ARDUINO I2C Network
• Sending data over I2C
• Wire.beginTransmission()
• Wire.write()
• Wire.endTransmission()
• Receiving data over I2C
• Wire.requestForm()
• Wire.available()
• Wire.read()
ARDUINO I2C Example
• Follow the example given in the link bellow
https://www.circuitbasics.com/how-to-set-up-i2c-communication-for-arduino/
What is SPI?
• Serial Parallel Interface
• Type of synchronous serial communication – data transfer is timed
with cock pulses.
• Short range – less than 10 meters
How SPI works?
• Consists of one master and one or more slave devices
• Each master and slave device has four connections
• MOSI – Master out Slave In
• MISO – Master In Slave Out
• SCLK - Serial Clock
• SS/CS – Slave Select/Chip Select
SPI Configurations
Independent Slave Configuration DAISY Chain Configuration
ARDUINO SPI Network
ARDUINO SPI Network
• Using SPI protocol in Arduino - <SPI.h>
• SPI.begin()
• SPI.setClockDivider(divider)
• Divider – 2, 4, 8, 16, 32, 64, 128
• SPI.attachInterrupt(handler)
• SPI.transfer(val)
ARDUINO SPI Network
• Follow the example given in the link bellow
https://circuitdigest.com/microcontroller-projects/arduino-spi-communication-tutorial