ATmega328P/Talking to other parts/31. I2C on A4 and A5
Talking to other parts · 31 of 33

I2C on A4 and A5

I2C is two wires, SDA and SCL, shared by every device on them. They are A4 and A5, which the UNO also prints as SDA and SCL, with no pull-up resistors fitted on either board. A scanner sketch shows who answers, and its error codes say why nobody did.

Your board

Two pins: A4 is SDA, A5 is SCL

A4 and A5 are the chip's two-wire interface: A4 carries SDA, the data, and A5 carries SCL, the clock. The top-left strip also starts with SCL and SDA, and the net list joins them to A5 and A4: the same two wires, so a module can plug into either pair. A sketch that uses I2C has lost A4 and A5 as analogue inputs.

The net list also shows what is not there. Each line runs from the header to the chip and nowhere else: no pull-up resistors. I2C needs them, because every device on the bus only ever pulls a line LOW. The datasheet calls the pins open drain, and something else has to bring them back HIGH.

The pull-ups Wire turns on

Wire.begin() turns on the chip's internal pull-ups on both lines. The Arduino core does it in twi.c, with a comment that says so, by writing each pin HIGH while it is an input. That is enough to hold an empty bus HIGH, and enough for one module on short wires at the default 100 kHz.

It is not much. The internal pull-ups are 20 to 50 kΩ, and the pull-up article explains why I2C wants something several times stronger: each rising edge is a capacitor charging through that resistor, and a weak one makes slow edges. Most modules carry their own pull-ups, often marked on the board; with one of those on the bus the internal ones no longer matter.

What the scanner prints

What the scanner prints
nothing found
On the bus
Answered
none
Other codes
2: no device answered its address
A 2 in every cell is a working bus with nobody on it. The chip sent each address and nothing pulled SDA low to say yes. The pull-ups Wire turns on inside the chip are enough to hold an empty bus HIGH.

For each address the sketch calls Wire.beginTransmission(addr) and Wire.endTransmission(), which sends the address and nothing else. A device that recognises its address pulls SDA LOW on the ninth clock and the call returns 0. The other codes come from the core's own list:

CodeMeans
0a device answered
2the address was sent and nobody answered
3a data byte was refused
4another bus error, such as losing the bus
5timeout, only after setWireTimeout()

On the bare board every cell is 2. The bus works, the addresses went out, and there is nobody there.

A stuck bus, on purpose

Upload the sketch first. Then put one jumper wire from SDA (A4) to the GND hole two places along the same strip. This is safe: with Wire running, SDA is an open-drain pin that never drives HIGH, and the 20 to 50 kΩ pull-up lets only a fraction of a milliamp through the wire.

With SDA held LOW the chip cannot send an address, and the grid fills with 4s and 5s instead of 2s. The exact mix depends on the core version. What matters is that there are no 2s: a grid with no 2s is a bus that is broken, not a bus that is empty. Without setWireTimeout() in setup(), the same fault can stop the sketch inside the first call and never print at all. Take the wire out and the next scan is all 2s again.

The code

I2C_Scanner_Codes.ino

Upload it to the bare board and open the Serial Monitor at 9600 baud. Every five seconds it asks each address from 0x08 to 0x77 and prints one cell per address: the address where a device answered, or the code Wire.endTransmission() returned.

// I2C_Scanner_Codes.ino — asks every I2C address, prints each answer
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none for the bare board. A module goes on SDA (A4),
//         SCL (A5), 5V and GND, with the board unplugged.
// Each cell: the address in hex if a device answered (code 0),
// otherwise endTransmission's code. 2 = nobody at that address.
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();                      // also turns on the pull-ups
  Wire.setWireTimeout(25000, true);  // give up after 25 ms
}

void loop() {
  Serial.println(F("      0  1  2  3  4  5  6  7"
                   "  8  9  A  B  C  D  E  F"));
  byte found = 0;
  for (byte row = 0; row < 8; row++) {
    Serial.print(row, HEX);
    Serial.print(F("0: "));
    for (byte col = 0; col < 16; col++) {
      byte addr = row * 16 + col;
      if (addr < 0x08 || addr > 0x77) {  // reserved addresses
        Serial.print(F("   "));
        continue;
      }
      Wire.beginTransmission(addr);
      byte code = Wire.endTransmission();
      Serial.print(' ');
      if (code == 0) {
        if (addr < 0x10) Serial.print('0');
        Serial.print(addr, HEX);
        found++;
      } else {
        Serial.print(' ');
        Serial.print(code);
      }
    }
    Serial.println();
  }
  Serial.print(found);
  Serial.println(F(" device(s) answered."));
  Serial.println();
  delay(5000);
}

On a board with nothing connected, every cell is a 2. That is the correct answer, and it is worth seeing once before a module goes on, so a 2 later means something.

When it does not work

The scanner prints 2 in every cell with a module connected

Nothing answered. Check that SDA goes to SDA and SCL to SCL (swapping them is the commonest mistake), that the module has power and ground, and that its address is in the 0x08 to 0x77 range the sketch asks.

The module appears on some scans and not others

The bus is marginal, usually because the only pull-ups are the chip's own 20 to 50 kohm ones and the wires are long. A module with its own pull-ups, or a pair of resistors from SDA and SCL to 5 V, makes it steady.

The scan stops partway and never prints again

The bus is stuck, often with SDA held LOW by a module that lost track mid-byte, and the sketch is waiting for it. setWireTimeout() lets the sketch give up and print 5; unplugging the module's power clears the module.

There is no output at all

Check that the Serial Monitor is at 9600 baud and the board is selected as Arduino Uno or Arduino Nano. The first grid appears straight after the reset that opening the Serial Monitor causes.

Where this goes next

Four wires, a clock you drive, and a test with one jumper.

SPI and the ICSP header →

Edit this page — content/books/atmega328p/i2c-on-a4-and-a5.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →