All courses

Arduino C++ · Lesson 33 of 40

Bring up an I2C device systematically

Measurement and Interfaces

Check the electrical bus before the library

For an Uno R3, SDA is A4 and SCL is A5; the dedicated SDA/SCL header duplicates those signals. Other boards use different pins. Connect a shared ground and a device supply that matches the breakout's specification. A board's voltage regulator does not necessarily level-shift its signal pins.

SDA and SCL use pull-ups. Check which breakout already supplies them and to which rail. Multiple pull-ups in parallel reduce effective resistance. Do not connect a 3.3 V-only device to pull-ups at 5 V without appropriate translation.

Confirm the device's 7-bit address from its datasheet and address jumpers. An address scanner finding a device proves an acknowledgement, not that a particular register map or sensor library is correct.

Probe a known address with a timeout

This Uno AVR Wire example probes address 0x76 once per second; change it to the address documented for your device. It is a diagnostic sketch and does not read sensor data.

Arduino C++
#include <Wire.h>
const byte address = 0x76;
unsigned long lastProbe = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
#if defined(WIRE_HAS_TIMEOUT)
  Wire.setWireTimeout(25000, true);
#endif
}

void loop() {
  const unsigned long now = millis();
  if (now - lastProbe < 1000) return;
  lastProbe = now;
  Wire.beginTransmission(address);
  const byte result = Wire.endTransmission();
  Serial.print(F("Probe status: "));
  Serial.println(result);
}

Zero means the address acknowledged; nonzero means an error. Interpret specific status values using your installed Wire core's documentation. Timeout support is core-dependent, hence the compile-time guard. A timeout reduces a hang risk but does not fix incorrect wiring.

Isolation sequence: Remove power, connect one device, verify ground/power/pins, run the probe, then try the library's smallest example. Add a second device only after the first works. If addresses collide, change a supported address jumper, use separate buses if supported, or an I2C multiplexer. A different C++ variable name cannot change a physical device address.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.