Mastering Serial Communication
with Arduino
Welcome to an in-depth exploration of serial communication protocols essential
for Arduino and embedded development. This presentation will guide you through
the fundamental differences, applications, and practical wiring of UART, I2C, and
SPI, empowering you to connect your projects effectively.
Whether you're a seasoned hobbyist or just starting, understanding these
protocols is crucial for expanding your hardware interaction capabilities.
The Foundation: Understanding Serial
Communication
Serial communication is a method for transferring data one bit at a time,
sequentially, over a communication channel or computer bus. This contrasts with
parallel communication, where multiple bits are sent simultaneously.
In the world of microcontrollers like Arduino, serial communication is the backbone
for interacting with sensors, displays, other microcontrollers, and even computers.
It's about sending and receiving information in an organized, byte-by-byte fashion.
Each protocol we discuss offers unique advantages in terms of speed, complexity,
and number of devices supported, making the choice dependent on your specific
project needs.
UART: The Universal Asynchronous Receiver/Transmitter
Simple, Direct, and Widely Used
UART (Universal Asynchronous Receiver/Transmitter) is perhaps the most common
and straightforward serial communication method. It uses two dedicated pins: RX
(Receive) and TX (Transmit). Data flows in one direction on each line, meaning it's
full-duplex by default if both lines are connected.
Simplicity: Requires only two wires (plus ground) for communication.
Asynchronous: No shared clock signal; sender and receiver agree on a baud
rate.
Direct Connection: Ideal for one-to-one device communication.
Error Checking: Often includes parity bits for basic error detection.
While simple, UART can be prone to timing issues if baud rates are not perfectly
matched between devices. Modern Arduino boards often have multiple hardware
UART ports, and software serial libraries can emulate additional ones.
Wiring for UART Communication
Arduino to Arduino Arduino to Sensor/Module
Connect the TX pin of the first Many sensors or modules (like
Arduino to the RX pin of the Bluetooth modules, GPS units, or
second, and vice-versa. Ensure a some environmental sensors) use
common ground connection. This UART. Connect the module's TX to
creates a direct, two-way Arduino's RX, and module's RX to
communication link. Arduino's TX. Don't forget the
common ground!
Arduino to Computer
The Arduino's built-in USB port
typically acts as a UART-to-USB
converter, allowing seamless
communication with your
computer's serial monitor. This is
how you upload code and view
debugging messages.
I2C: The Inter-Integrated Circuit Bus
Master-Slave Architecture, Many Devices
I2C (Inter-Integrated Circuit), also known as TWI (Two-Wire Interface), is a
synchronous, multi-master, multi-slave serial computer bus. It uses only two
bidirectional lines: SDA (Serial Data Line) and SCL (Serial Clock Line).
Two Wires: Minimal wiring complexity, even for multiple devices.
Synchronous: Uses a shared clock signal (SCL) to synchronize data transfer.
Address-Based: Each device on the bus has a unique 7-bit or 10-bit address.
Master/Slave: A master initiates communication with a specific slave device.
I2C is perfect for connecting multiple low-speed peripheral devices (like EEPROMs,
real-time clocks, and some sensors) to a single microcontroller. Remember to
include pull-up resistors on both SDA and SCL lines for proper operation.
Wiring for I2C Communication
SDA and SCL Lines
Connect all SDA pins together and all SCL pins together. The Arduino
will act as the master, communicating with various slave devices.
Pull-up Resistors
Mandatory for I2C! Typically, two 4.7k« resistors are connected from
SDA to VCC and SCL to VCC. Some modules have these built-in, so
check your device's datasheet.
Device Addressing
Each slave device must have a unique address. If two devices have the
same address, you'll need an I2C multiplexer or alternative addressing
options.
SPI: Serial Peripheral Interface
High Speed, Dedicated Lines
SPI (Serial Peripheral Interface) is a synchronous serial communication interface
specification used for short-distance communication, primarily in embedded
systems. It's often used for communicating with flash memory, ADC/DAC
converters, and SD card readers.
Four Wires: MOSI (Master Out Slave In), MISO (Master In Slave Out), SCK (Serial
Clock), SS (Slave Select).
Full Duplex: Data can be sent and received simultaneously.
High Speed: Generally faster than I2C due to dedicated data lines and no
addressing overhead.
Multiple Slaves: Each slave requires its own dedicated SS line from the master.
SPI is a go-to choice when speed and continuous data streams are critical, but it
comes at the cost of more wiring, especially with multiple slave devices.
Wiring for SPI Communication
01
Common SCK, MOSI, MISO
The SCK (clock), MOSI (Master Out Slave In), and MISO (Master In Slave Out) pins
are typically shared among all slave devices on the bus.
02
Dedicated Slave Select (SS)
Each slave device requires its own unique SS (Slave Select) pin connected to the
master. The master pulls the corresponding SS line LOW to activate a specific
slave.
03
Ground Connection
As with all communication protocols, ensure a common ground connection
between the master and all slave devices.
04
No Pull-ups Needed
Unlike I2C, SPI does not typically require external pull-up resistors on its lines.
Comparing the Protocols: UART vs. I2C vs. SPI
1 2 3
UART I2C SPI
Simplicity & Point-to-Point Multi-Slave & Short Distance High Speed & Full Duplex
2 wires (RX, TX) 2 wires (SDA, SCL) + Pull-ups 4 wires (MOSI, MISO, SCK, SS per
Asynchronous (no shared clock) Synchronous (shared clock) slave)
1 master, 1 slave (or 1 PC) 1 master, multiple slaves (address- Synchronous (shared clock)
Medium speed based) 1 master, multiple slaves (SS line per
Low to medium speed slave)
High speed
The choice of protocol hinges on your project's specific requirements: number of devices, communication speed, wiring complexity, and
whether you need simultaneous send/receive capabilities.
Expanding Your Arduino's Communication Horizons
By understanding UART, I2C, and SPI, you've unlocked a vast potential for your Arduino projects. Each protocol serves distinct purposes and
offers unique advantages:
UART for simple, direct serial console interaction or module control.
I2C for connecting multiple low-speed sensors or displays with minimal wiring.
SPI for high-speed data transfer with memory, displays, or other data-intensive peripherals.
Experiment with these protocols, delve into their libraries (like SoftwareSerial, Wire, and SPI in Arduino), and start building more complex and
interactive embedded systems. The world of interconnected electronics awaits!
Happy tinkering!