Let's talk! UART, SPI, I2C, 1-Wire - Arduino Concepts for Beginners

acfb Feb 11, 2020

Arduino alone has many capabilities but without external peripherals the possibilities are really limited.
And when we're dealing with other devices we need a common communication strategy, for even if the basic language is the same (1s and 0s) we need an agreement on how to interpret and understand it. That's why engineers developed different ways of "talking" between devices, each with its own advantages and disadvanteges.

UART (Universal Asynchronous Receiver/Transmitter)

This is the most basic type of communication between devices since we just need two pins: a Transmitter and a Receiver (which in turn are connected to the receiver and transmitter of the peripheral, respectively), which communicate Asynchronously, i.e. there's no complementary clock giving the tempo of the exchange of data (a clock exists, but its internal of the two communicating devices and must be set to the same speed).

An UART packet is made of 1 start bit (LOW), 8bit with parity bits (1) for checking and 1 (or 2) stop bit (HIGH), and the approximate speed in which data is trnsferred is called the baud rate and measured in bits per seconds (bps).

CONS:
Communication can only happen between two devices, with a baud rate that must be agreed upon beforehand.

I2C (Inter-Integrated-Circuit)

This type of communication uses a 2-wire system like UART (SDA and SCL), but its synchronous and allows to achieve a faster transfer speed (and distance).

It's used a lot with sensors and modules since we can have one (or more) master device (the microcontroller) to which we can connect multiple (up to 128) devices as slaves - provided each is identified by an unique address.

i2c

CONS:

  • It needs pull-up resistors since it has an open-drain configuration (2)
  • Since there can be multiple masters and multiple slaves circuit complexity may get higher
  • communication is half-duplex, i.e. output/input data cannot be done simultaneously but need to alterne.

SPI (Serial Peripheral Interface)

Serial Peripheral Interface works similar to I2C in some aspects (master-slave architecture, synchronicity, data/clock lines are shared ecc.) but has some key differences:

  • there's no official specs
  • it uses 4 wires to communicate: MOSI (Master Out Slave In), MISO (Master In Slave Out), CLK (Serial Clock) and CS (Chip Select)
  • Having a MOSI and a MISO allows for full-duplex communication (data can be sent and received simultaneously)
  • It's the fastest among the three

CONS:

  • Only one master
  • It needs more pins, especially one CS pin for each device (unless you chain the slaves one to the other)

1-Wire protocol

The 1-Wire bus is a proprietary protocol designed by Dallas Semiconductor (now Maxim Integrated) that provides power, data and signal over just a single conductor.
Like the I2C is a bidirectional and half-duplex communication between a master and one or more slaves on the shared bus.

1-wire protocol


I'll dive a bit deeper into each of the last three types in the next installments of this series, as usual for any question or correction just drop a comment 🙂


(1) We can have a odd parity where the parity bit will be HIGH if data contains an even number of high bits, or even parity where the parity bit will be HIGH if data contians an odd number of high bits; this mechanism is a simple check for transmission errors, f.ex. if the parity bits tells us to expect an odd number of HIGH bits and one got flipped during the transmission we'll know something went wrong.

(2) "Open-drain" is a term used a lot in electronics; it refers to the configuration of many IC output pins, where we need a pull-up resistor for the output to be HIGH (and avoid floating). When the internal N-MOSFET of the IC is off the resistor connected to the output pin (the drain of the N-FET) pulls-up the output to Vcc avoiding leakage currents and pin floating. When the N-FET is on, instead, it pulls-down the output to GND


If you want to suggest something, or find any error, please don't esitate to leave a comment 👇, I'll be glad to hear from you 😉

Other articles in this section: