Sub topic 1
What I2C Is and When to Use It
Prerequisites
This chapter assumes you know the setup and loop structure of an Arduino sketch, can use the Serial Monitor, understand voltage and resistance, and can read binary and hexadecimal numbers.
What I2C Is
I2C (Inter-Integrated Circuit) is a serial bus that lets one controller talk to several targets over two shared wires. It suits short links between chips on one board, such as sensors, displays, and real-time clocks. Use it when you need several devices but have few spare pins. It is slower than SPI, but it needs fewer wires and can connect many devices to the same pair.
Two Wires: SDA and SCL
SDA carries the data. SCL carries the clock that paces each bit. Both lines are open-drain, which means a device can pull a line low but never drive it high. A pull-up resistor lifts the line high when no device pulls it low. Because no device ever drives a line high, two devices cannot short the supply against each other.
Why the Bus Needs Pull-Up Resistors
Without pull-ups, the lines float and never reach a high level. A value of 4.7 kΩ is common for short wiring. Lower values raise the edge speed on long or heavily loaded buses, but they draw more current.
How Devices Are Addressed
Each target has a 7-bit address, usually written in hexadecimal, such as 0x68. The controller sends that address in the first byte after START. The eighth bit of that byte is the R/W bit: 0 means write, 1 means read. Addresses must be unique on one bus.
Anatomy of a Transaction
- START: the controller pulls SDA low while SCL is high.
- Address byte: the controller sends seven address bits and the R/W bit.
- ACK or NACK: the addressed target pulls SDA low to acknowledge. No pull-down means NACK, so no device answered.
- Data bytes: the receiver acknowledges each byte.
- Repeated START: the controller can switch direction without releasing the bus.
- STOP: the controller releases SDA while SCL is high.
The Wire library runs these steps for you:
#include <Wire.h>
void setup() {
Wire.begin();
Wire.beginTransmission(0x68); // START, address 0x68, write bit
Wire.write(0x6B); // register pointer
Wire.write(0x00); // data byte
Wire.endTransmission(); // STOP
}
void loop() {
}:::callout note :::
Exercise
A target's write byte is 0xD0. What is its 7-bit address?
Show answer
0x68. Shifting 0xD0 right by one bit gives 0x68, and the lowest bit is 0, which means write.