Serial Communication for Arduino
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.
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.
Connection distances range from centimeters for Parallel and SPI devices to kilometer or more for RS-422 which is an evolution of UART protocol.
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.
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.
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.
- 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.
- 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.
- 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.
- 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.
- incorrect cable
- bad cable
- mismatched speed or start/stop/parity
- endianness
- voltage levels
- common ground
- capacitance
- pull up/downs or open drain
- noise/cross talk