Sub topic 15
Pins with a second job: UART, I2C, and SPI
This chapter assumes you understand voltage, current, resistance, breadboard circuits, and the Arduino setup() and loop() structure. It also assumes you can identify the Uno's digital and analog headers.
The Arduino Uno's pins are general-purpose, but several have hardware communication functions. These functions let the microcontroller exchange data with computers, sensors, displays, memory chips, and other controllers.
The three common buses
A communication bus is a set of electrical signals and rules for exchanging data.
| Bus | Uno pins | Typical wiring | Common use |
|---|---|---|---|
| UART | D0/RX and D1/TX | RX, TX, and GND | Computer or GPS connection |
| I2C | A4/SDA and A5/SCL | SDA, SCL, and GND | Sensors and displays |
| SPI | D10 to D13 | SS, MOSI, MISO, SCK, and GND | Fast displays, SD cards, radio modules |
UART means Universal Asynchronous Receiver/Transmitter. The Uno uses D0 to receive data and D1 to transmit data. UART has no shared clock signal, so both devices must use the same baud rate, which is the number of transmitted symbols per second. Connect one device's TX to the other's RX, and connect their grounds.
I2C, commonly pronounced “I-squared-C,” uses a clock line, SCL, and a data line, SDA. Multiple devices can share these two signal wires because each device has an address. The Uno is normally the controller that starts transfers. I2C lines need pull-up resistors, although many sensor modules already include them.
SPI uses a clock, SCK; data from the controller, MOSI; data to the controller, MISO; and a chip-select line, SS. Each peripheral normally gets its own SS line. SPI is usually faster than I2C, but it uses more wires.
Initializing the hardware
The Arduino core provides libraries for these buses. Serial handles the Uno's hardware UART, Wire handles I2C, and SPI handles SPI.
#include <Wire.h>
#include <SPI.h>
const int chipSelectPin = 10;
void setup() {
Serial.begin(9600);
Wire.begin();
SPI.begin();
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
Serial.println("Communication buses initialized");
}
void loop() {
Serial.println("UART is sending this message");
delay(1000);
}Wire.begin() and SPI.begin() configure the hardware but do not communicate with a peripheral by themselves. A device-specific library supplies the commands and data format.
Shared-pin conflicts
The UART pins are also connected to the Uno's USB-to-serial interface. Using D0 or D1 for another circuit can interfere with sketch uploads and Serial Monitor output. Disconnect that circuit while uploading if necessary.
The I2C pins are labeled both by analog number and signal name. A4 is SDA and A5 is SCL on the Uno, but this mapping differs on some boards. SPI also appears on the six-pin ICSP header, which can be useful when a board variant places SPI on different digital pins.
Choosing a bus
Use UART for a simple point-to-point link. Use I2C when several addressed devices should share a small number of wires. Use SPI when speed matters and extra chip-select wires are acceptable. Always match logic voltage levels. A 5 V Uno can damage a 3.3 V-only peripheral unless level conversion is provided.
Exercise
Why can connecting a module to D0 and D1 prevent reliable sketch uploads or Serial Monitor communication?
Show answer
D0 and D1 are the Uno's hardware UART pins, which are shared with the USB-to-serial interface. The module can interfere with the same RX and TX signals.