Sub topic 1
What I2C is and when to use it
I2C (Inter-Integrated Circuit, pronounced "I squared C") is a serial bus that lets one microcontroller talk to many chips over two shared signal wires. Philips (now NXP) created it in 1982 for connecting chips inside consumer electronics. Today it is the usual way to attach sensors, displays, real-time clocks, and memory chips to an Arduino.
This chapter gives you the working model of the bus and the criteria for choosing it over other options. Later chapters cover the electrical details, addressing, and the Wire library calls.
Prerequisites
This chapter assumes you can already do the following:
- Write and upload a basic Arduino sketch with
setup()andloop(). - Print values to the Serial Monitor.
- Wire a simple circuit on a breadboard and read the basics of voltage, current, and resistors.
- Read binary and hexadecimal numbers, for example that
0x68is 104 in decimal.
You do not need to know anything about serial protocols yet.
The working model
An I2C bus has three kinds of connections:
- SDA (serial data) carries the data bits.
- SCL (serial clock) carries the timing signal.
- GND (ground) is shared by every device. It is not part of the protocol, but the bus cannot work without it.
Every device on the bus connects to the same SDA and SCL wires. Nothing else is needed to add a device. You wire the new chip's SDA to the existing SDA line and its SCL to the existing SCL line.
Devices play one of two roles:
- The controller starts every conversation and generates the clock on SCL. Your Arduino is normally the controller. Older documents call this role "master".
- A target listens and answers only when the controller addresses it. Older documents call this role "slave". Sensors, displays, and memory chips are targets.
Targets never speak on their own. If a temperature sensor has a new reading, it cannot tell you. You have to ask for it.
Each target has an address. The controller begins every transaction by sending the address of the device it wants. Every target hears it, but only the one with a matching address responds. The others ignore the rest of the transaction.
Where the pins are
The Wire library uses fixed hardware pins that depend on the board. Check your board's pinout before wiring.
| Board | SDA | SCL |
|---|---|---|
| Uno R3, Nano | A4 | A5 |
| Mega 2560 | 20 | 21 |
| Leonardo | 2 | 3 |
On the Uno R3, the same two signals are also broken out on two header pins near AREF, labeled SDA and SCL. These are connected to A4 and A5, so you can use either pair but not both for different purposes.
What a conversation looks like
Every transaction follows the same outline:
- The controller signals the start of a transaction.
- The controller sends the 7-bit address of the target, plus one bit saying whether it wants to write to the target or read from it.
- The target confirms it is present by pulling the data line low for one bit. This confirmation is called an ACK (acknowledge).
- Data bytes follow, each one confirmed with an ACK.
- The controller signals the end of the transaction.
The bus is half-duplex. Data moves in one direction at a time, and only one device drives SDA at any moment. The controller decides who talks. A later chapter covers each step in detail, including what a NACK (no acknowledge) means.
Most targets also expose registers, which are numbered storage locations inside the chip. To read a sensor you typically write a register number to the chip, then read back the bytes stored there. The register-access chapter covers this pattern.
A first look at the code
The Arduino Wire library hides the signaling. The sketch below asks whether anything answers at a chosen address. It does not read data. It only checks whether the device acknowledges.
#include <Wire.h>
const uint8_t DEVICE_ADDRESS = 0x68; // change to your device's address
void setup() {
Serial.begin(9600);
Wire.begin(); // join the bus as the controller
Wire.beginTransmission(DEVICE_ADDRESS);
byte result = Wire.endTransmission(); // 0 means the device sent an ACK
if (result == 0) {
Serial.println("Device answered.");
} else {
Serial.print("No answer, error code ");
Serial.println(result);
}
}
void loop() {
}Wire.begin() with no argument makes the Arduino a controller. endTransmission() sends the queued transaction and returns a status code. A return value of 0 means success. If you get a nonzero code, the most common cause is a wiring or address problem, covered in the troubleshooting chapter.
The most common address for chips like the DS3231 clock or MPU-6050 motion sensor is 0x68. Your device's datasheet lists its own address.
When I2C is the right choice
I2C is a good fit when:
- You have several low-to-medium speed devices, such as sensors, small displays, and clocks.
- You are short on pins. Two pins serve many devices.
- The devices are close together, on the same board or within roughly a meter of wire.
- You want to add or remove devices without rewiring the controller.
- Breakout boards for the parts you want already speak I2C. This is true of a very large share of hobbyist sensors.
I2C is a poor fit when:
- You need high throughput. Standard mode runs at 100 kHz and Fast mode at 400 kHz, and the Wire library defaults to 100 kHz. Raw clock rate does not equal data rate, because every byte also costs an extra acknowledge bit and each transaction carries address overhead.
- You need long cables. The bus tolerates only limited wire capacitance, which later chapters cover.
- You need timing guarantees. A busy or stuck bus can delay everything else on it.
- Two devices you want to use have the same fixed address and no way to change it. There are workarounds, but they add parts.
Comparing I2C with SPI and UART
| Feature | I2C | SPI | UART |
|---|---|---|---|
| Signal wires | 2 (SDA, SCL) | 3 plus one select line per device | 2 (TX, RX) per link |
| Devices per bus | Many, chosen by address | Many, chosen by select line | Usually 2 |
| Typical speed | 100 kHz to 400 kHz | Several MHz and up | 9600 to 115200 baud common |
| Pins grow with devices | No | Yes, one more per device | Yes, one more pair per device |
| Needs pull-up resistors | Yes | No | No |
The tradeoff is simple. I2C saves pins and wiring at the cost of speed and some electrical care. SPI is faster and simpler electrically but uses more pins as you add devices. UART connects exactly two devices and needs no addressing.
Common mistakes at this stage
- Expecting the Arduino to receive data unprompted. Targets only respond to requests. If you want a push-style notification, you need a separate interrupt line from the sensor.
- Forgetting the shared ground. Every device must share GND with the Arduino, or the signals have no common reference.
- Swapping SDA and SCL. The device simply will not answer. Nothing is damaged, but the symptom looks like a dead chip.
- Assuming any pins will work. The standard Wire library uses the board's fixed hardware I2C pins.
- Ignoring supply voltage. Some breakouts are 3.3 V only. Connecting them directly to a 5 V Arduino bus can damage them. The level-shifting chapter covers this.
Check your understanding
Exercise
You have a 5-device project: a temperature sensor, a light sensor, an OLED display, a real-time clock, and a small EEPROM. Why can an Arduino with only two spare pins connect to all of them using I2C, and what makes each device respond only when it should?
Show answer
All five devices connect to the same two wires, SDA and SCL, so the pin count does not grow with the number of devices. The controller begins each transaction by sending a 7-bit address. Only the target whose address matches acknowledges and takes part. The others ignore the transaction. This only works if the five devices have different addresses.
Exercise
In the sketch above, Wire.endTransmission() returns a nonzero value for address 0x68. Name two likely causes that you can check without changing the code.
Show answer
Likely causes include SDA and SCL being swapped or loose, no shared ground, the device not powered, or the device having a different address than 0x68. A missing pull-up is also possible if the breakout board has none.