Sub topic 15
Pins with a second job: UART, I2C, and SPI
This chapter assumes you know how pinMode, digitalRead, and digitalWrite work. It also assumes you have used the Serial Monitor and can read a simple wiring diagram. Many Uno pins do more than one job. This chapter shows which pins have a second job, what each job needs, and how to avoid conflicts between them.
Why pins have a second job
The ATmega328P on the Uno has a small number of physical pins. Its built-in hardware blocks for serial communication share those pins with ordinary digital and analog functions. Each shared pin is connected to one function at a time. Once you start a bus, such as Serial.begin(), the hardware takes control of its pins and your digitalWrite calls on them no longer behave as you expect.
Three bus types matter on the Uno:
- UART (Universal Asynchronous Receiver-Transmitter) is a two-wire serial link with no shared clock. Each side must agree on a speed in advance.
- I2C (Inter-Integrated Circuit) is a two-wire bus that connects many devices. Each device has an address.
- SPI (Serial Peripheral Interface) is a faster bus that uses a clock wire, two data wires, and one select wire per device.
Pin map on the Uno R3
| Bus | Signal | Uno pin | Notes |
|---|---|---|---|
| UART | RX (receive) | D0 | Shared with the USB-to-serial bridge |
| UART | TX (transmit) | D1 | Shared with the USB-to-serial bridge |
| I2C | SDA (data) | A4 | Also broken out near AREF on R3 boards |
| I2C | SCL (clock) | A5 | Also broken out near AREF on R3 boards |
| SPI | SS (slave select) | D10 | Default chip-select pin |
| SPI | MOSI | D11 | Controller out, peripheral in |
| SPI | MISO | D12 | Controller in, peripheral out |
| SPI | SCK (clock) | D13 | Same pin as the built-in LED |
UART: pins 0 and 1
The UART carries the Serial object. The USB-to-serial bridge from earlier chapters connects to D0 and D1 through resistors. When you call Serial.begin(9600) and Serial.println(), the bytes leave the chip on D1 (TX) and reach your computer through the bridge. Bytes from your computer arrive on D0 (RX).
This has practical consequences:
- If you wire a sensor or LED to D0 or D1, it competes with the USB connection. Uploads often fail with a sync error while something drives D0.
- If you do not use
Serial, you can use D0 and D1 as ordinary pins, but disconnect the wiring before you upload. - The two sides must use the same baud rate, which is the number of signal changes per second. A mismatch produces garbage characters in the Serial Monitor.
- TX of one device connects to RX of the other. The lines cross. Connecting TX to TX is a common mistake.
To talk to a second serial device, such as a GPS module, you can use the SoftwareSerial library on other pins. It is slower and less reliable at high speeds than hardware UART, because the processor does the timing in software.
#include <SoftwareSerial.h>
SoftwareSerial gps(8, 7); // RX = pin 8, TX = pin 7
void setup() {
Serial.begin(9600);
gps.begin(9600);
}
void loop() {
if (gps.available()) {
Serial.write(gps.read());
}
}This sketch forwards every byte from the device on pins 8 and 7 to the Serial Monitor. The device's TX wire goes to pin 8, and its RX wire goes to pin 7.
I2C: A4 and A5
I2C uses two wires shared by every device on the bus:
- SDA carries data in both directions.
- SCL carries the clock from the controller.
Each peripheral has a 7-bit address, such as 0x3C for many small OLED displays or 0x68 for some real-time clocks. The controller starts a transaction by sending an address, and only the matching device responds.
Both lines are open-drain. A device can pull a line low but never drives it high. Pull-up resistors bring the lines high when no one pulls them down. Many breakout boards include these resistors. The ATmega328P also enables weak internal pull-ups when you call Wire.begin(), but they are about 20 kΩ to 50 kΩ, which is too weak for longer wires or several devices. Typical external values are 4.7 kΩ.
On the Uno R3, SDA and SCL are wired to A4 and A5, and also to two extra header pins next to AREF. These are the same signals, not additional ones. Once I2C is active, A4 and A5 cannot be used as analog inputs.
The following sketch scans the bus and prints the address of every device that responds. It is the quickest way to check that a wiring connection works.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.println("Scanning I2C bus...");
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print("Found device at 0x");
Serial.println(address, HEX);
}
}
Serial.println("Done.");
}
void loop() {}If the scan prints nothing, check these in order: power and GND to the device, SDA and SCL not swapped, pull-up resistors present, and a device voltage compatible with 5V logic.
Voltage matters on I2C
Many modern sensors are 3.3V devices. Putting the Uno's 5V logic on their SDA and SCL lines can damage them. Use a level shifter, or a breakout board that includes one. Pull-ups should go to the voltage of the device's logic, not blindly to 5V.
SPI: pins 10 to 13
SPI uses four signals:
- SCK is the clock, driven by the controller.
- MOSI carries data from the controller to the peripheral.
- MISO carries data from the peripheral back to the controller.
- SS (also called CS) selects one peripheral. It is active low, so pulling it LOW means "talk to me."
Each additional peripheral shares SCK, MOSI, and MISO, but needs its own select pin. That select pin can be any free digital pin. You choose it in your code.
SPI is faster than I2C and has no addresses. The cost is more wires. SD card modules, many displays, and some radio modules use SPI.
#include <SPI.h>
const int chipSelect = 10;
void setup() {
pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH); // deselect the peripheral
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(chipSelect, LOW); // select
byte reply = SPI.transfer(0x9F); // send a byte, receive a byte
digitalWrite(chipSelect, HIGH); // deselect
SPI.endTransaction();
delay(1000);
}Each SPI.transfer() sends and receives a byte at the same time. The SPISettings call sets the clock speed, the bit order, and the SPI mode. The mode defines the clock polarity and when data is sampled. Your peripheral's datasheet states which mode it needs.
Pin 10 and master mode
On the Uno, pin 10 must stay an OUTPUT while the Uno acts as the SPI controller. If it is configured as an input and goes LOW, the hardware drops out of controller mode. Always set it as an output, even if you use a different select pin for your device.
The LED on pin 13
SCK is on pin 13, which also drives the built-in LED. When SPI is running, the LED flickers with the clock. Avoid using pin 13 as a separate input while SPI is active. An LED and its resistor on that pin can also load the signal slightly.
Finding conflicts
A conflict happens when two functions need the same pin. These are the common ones on the Uno:
| Situation | Conflict | Fix |
|---|---|---|
| Sensor wired to D0 or D1 plus `Serial` | UART and the sensor share the line | Move the sensor to another pin |
| Analog sensor on A4 or A5 plus an I2C device | Both want the same pin | Move the analog sensor to A0 to A3 |
| SD card module plus a button on pin 12 | MISO is shared | Move the button |
| Display on SPI plus pin 10 used as input | Controller mode drops | Keep pin 10 as OUTPUT |
| Shield stacked on the Uno | Shield uses pins you also use | Read the shield's pin table first |
A simple habit prevents most problems: before you wire anything, write a list of the pins every library in your sketch will take.
Choosing between the buses
| Property | UART | I2C | SPI |
|---|---|---|---|
| Wires | 2 plus GND | 2 plus GND | 4 plus GND, plus one select per device |
| Devices on the bus | 2 (point to point) | Many, by address | Many, one select each |
| Clock | None, agreed baud rate | Shared, from controller | Shared, from controller |
| Typical speed on the Uno | 9600 to 115200 baud | 100 kHz or 400 kHz | Up to 4 MHz or more |
| Best for | GPS, Bluetooth modules, the PC | Sensors, small displays | SD cards, fast displays, radios |
When you have a choice, I2C saves pins and SPI saves time. UART suits devices that stream text or data on their own schedule.
Exercise
You wire a temperature sensor to A4 and a small I2C OLED display to the SDA and SCL pins. The display never responds and the sensor readings look strange. What is wrong, and how do you fix it?
Show answer
A4 is also the SDA line. The analog sensor and the I2C bus share the pin, so each disturbs the other. Move the analog sensor to A0, A1, A2, or A3, and keep A4 and A5 for I2C.
Exercise
A sketch that uses Serial.println() works, but uploading fails whenever a Bluetooth module is wired to pins 0 and 1. Why, and what are two ways to avoid it?
Show answer
The module drives D0 and D1, which the USB-to-serial bridge also uses during upload, so the bootloader cannot receive the sketch. Either disconnect the module's wires while uploading, or move the module to other pins and use SoftwareSerial.