haker
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Serial Communication for Arduino

I could not have said it better

The video below is an excellent introduction into serial communication. Rather than (badly) repeating what Electronoobs said so well, I suggest you watch the short video, and then we will build on top of this information. See you in 10 mins.


Quick Comparison

While there is significant overlap in functionality between these protocols, indeed each has strengths and weaknesses, and you should pick the one that best suits your use-case.

Protocol Speed Distance Wires Duplex Bus vs. P2P Complexity Sync/Async
Parallel/TTL very high, GHz+ short many half bus* very simple sync
SPI high, MHz+ short 4 full bus* simple sync
I2C medium, 0.1-5 MHz medium 2 half bus medium sync
UART slow, 0.003-1 MHz long 2 full p2p complex async

Most commonly, your choice will be restricted by the device you want to interface with, e.g. most NVRAM has only I2C interface. If your target device supports multiple interfaces, then your choice will be most likely influenced by the length of the wire/connection you will use as well as the speed you need to achieve.

Distance

Connection distances range from centimeters for Parallel and SPI devices to kilometer or more for RS-422 which is an evolution of UART protocol.

Complexity

Don’t be surprised by inclusion of the complexity column. Manufacturers are trying to lower the cost of manufacture, so the simpler the protocol the cheaper they can implement it.

Wires

Number of pins or wires is also big part of the cost. You might be surprised that adding pins to a chip is often more expensive than adding functionality on the silicon. This is why most of the pins on the Arduino and other micro-controllers are multiplexed, i.e. have multiple functions.

Bus vs P2P

Protocols that work on a bus also allow connecting many devices together using the same wire, i.e. bus. For example, I2 C controller can address 127 target devices. Devices marked as bus* are bus protocols, but unlike I2 C they require additional control lines to address target devices.


Parallel vs Serial

  • No standard. Every parallel bus connection can be different.
  • Minimum of 1 wire per bit, i.e. 8 wires to transfer 1 byte, plus clock.
  • Need for additional control pins, chip select, read/write, interrupt, even in a p2p connection.
  • Very high speed, limited by system clock.

SPI

  • Very common for cheap but high speed communication, e.g. flash memory, displays.
  • Additional control pins needed in one to many configuration.
  • Possible to bitbang.
SPI Wiring Diagram

References


I2 C

  • Very common for cheap/simple devices that still need decent connection speed.
  • Full bus, supporting up to 127 target devices without additional wires.
  • More advanced implementations support multiple controllers.
  • Variable, master controlled, speed.
  • Clock stretching.
  • Possible to bitbang.
  • Arduino support via Wire library.
    • 32 byte buffers only.
    • Rudimentary support for timeouts.
    • Support for both controller and target mode.
I2C Wiring Diagram
I2C Data Transfer Signals
I2C Oscilloscope Trace
I2C Oscilloscope Trace

References


UART

  • Very common protocol. Used for modems, mice and more in the past, and now mostly in embedded applications.
  • Often used for debugging, console output etc.
  • Many variants, RS-232, RS-422, RS-485, that further standardize basic specification.
    • RS-232 specifies voltage levels, and has range of up to 300m
    • RS-422 uses differential signaling over twisted pair to reach range up to 1200m.
    • RS-485 builds on top of RS-422 to support multiple point connections.
  • Arduino support via Serial library.
    • 64 byte buffers only.
RS-232 Signal Trace

References


Common Issues with Serial Protocols

Obvious Problems

  • incorrect cable
  • bad cable
  • mismatched speed or start/stop/parity
  • endianness
  • voltage levels
  • common ground

Signal Quality

  • capacitance
  • pull up/downs or open drain
  • noise/cross talk
Signal Rise Time
Signal Rise Time
I2C Glitch
I2C Glitch
I2C Cross Talk
I2C Cross Talk