ATmega328P from scratchPart 11 of 12

Build note

I2C: two wires and a roll call

I2C is a classroom. The teacher calls a name, one student answers, and everyone else stays quiet because the rule is that only the addressed device talks.

Two wires carry every conversation, and each device has its own address, so a dozen parts can share those two wires without shouting over each other.

The two wires and the resistors

Wire Job
SCL Serial clock, driven by the master
SDA Serial data, carries addresses and data in both directions

No device ever drives a line high. It pulls the line low, or releases it and lets a resistor pull it high. That is what allows many devices to share one wire without damaging each other, and it is why the pull-ups are not optional.

Pull-up Effect
1.8 k to 2.2 k Fast edges, more current, better for long buses
4.7 k The usual breadboard choice
10 k Works on a tiny bus, edges get slow and errors appear

Most breakout boards include the resistors, which is why people forget they exist until the day they add a second board.

Address, acknowledge, repeat

A transaction starts with an address byte: seven address bits plus one read/write bit. The addressed device pulls the data line low for one clock to acknowledge. No acknowledgement means nobody answered.

Reading a register needs a direction change without giving up the bus:

START, [address + write], [register], repeated START, [address + read], [data], STOP

That repeated START is the step people get wrong. Without it, the device loses its place and returns data from register zero, which looks like a working read of the wrong register.

The registers

Register Page Job
TWBR 198 Bit rate divisor
TWSR 200 Status code and prescaler
TWCR 199 Control: start, stop, enable, acknowledge
TWDR 200 One byte of data

At 16 MHz and 100 kHz with a prescaler of 1:

TWBR = (16,000,000 / 100,000 - 16) / 2 = 72

TWINT behaves in a way that surprises everyone. You clear it by writing a one, and clearing it is what starts the next operation. That is why almost every line in the driver looks like:

TWCR = (1 << TWINT) | (1 << TWEN);

Status codes are the debugging tool

TWSR tells you which step succeeded. Mask off the prescaler bits and compare:

Code Meaning
0x08 START transmitted
0x10 Repeated START transmitted
0x18 Address plus write acknowledged
0x40 Address plus read acknowledged
0x28 Data byte acknowledged

When something fails, the code says where. A 0x20 means the address was not acknowledged, which is a wiring or address problem, not a data problem.

The MPU6050

The sensor has a register map and a default address of 0x68. Pulling the AD0 pin high moves it to 0x69, so two sensors can share the bus.

The first read should always be WHO_AM_I at 0x75. It returns 0x68 on a healthy part. If that byte is wrong, nothing after it will be right.

Two scaling facts make the raw numbers meaningful:

  • Accelerometer at the default plus/minus 2 g range: 16384 counts per g. At rest on a flat surface, Z is about 16384.
  • Gyroscope at the default plus/minus 250 degrees per second: 131 counts per degree per second. At rest, all three axes are near zero.

The example prints accelerometer, gyroscope and temperature over the UART from Part 2, one line every 500 ms.

The failures that cost time

  • No pull-ups. The lines never rise and every transaction fails.
  • Using the 8-bit address. The driver shifts the 7-bit address left by one and adds the read/write bit. Passing 0xD0 when the code expects 0x68 produces no acknowledgement.
  • No repeated START. The register pointer resets and you read register zero.
  • A stuck bus. If a device holds SDA low after a reset, the master cannot start a transaction. Clocking SCL by hand a few times usually frees it.
  • Long wires. I2C is meant for a board, not for a meter of loose jumper.

How this was made

  • Written by: Rupesh, from his own c-11-i2c driver and notes, with AI drafting and editing.
  • Register map and scaling: from the MPU6050 documentation in the folder.
  • AI-assisted sections: structure and copy editing.