Try Embedded

Two Arduinos over I²C (master and slave)

One Uno is the I²C master, the other a slave at address 8: the master sends a brightness and asks for the slave's button.

Communication · Intermediate

In a real product: Modular synthesizer (Consumer electronics): Modules talk to a controller over I2C.

I²C is not only for sensors: an Arduino can be a slave itself, with its own address. Here the first Uno (the master, sketch.ino) talks to the second (the slave at address 8, uno2.ino) over the same two wires a sensor would use.

What you'll learn

  • Wire.begin(address) makes a board a slave; Wire.begin() makes it the master
  • onReceive runs when the master writes, onRequest when it reads, both in an interrupt
  • Why the handlers must be short, and why their variables are volatile

How it works

Every 100 ms the master reads its knob and writes one byte, the brightness, to address 8. The slave's onReceive stores it and loop() writes it to the LED on pin 9. Then the master asks for one byte with requestFrom(8, 1); the slave's onRequest answers with its button's state, and the master lights its own LED on pin 13 while it is pressed. A4 (SDA) and A5 (SCL) join the two boards, as do their grounds; the Uno's pull-ups hold the lines high.

endTransmission() returns 0 when the slave acknowledged, 2 when nothing answered at the address: try changing the slave's address and watch the master's serial monitor.

Try this

  • Press the slave's button while it runs: the master's L LED follows.
  • Give the slave address 9 and see endTransmission() return 2.
  • Send two bytes (brightness and a blink rate) and read them in onReceive with Wire.available().

sketch.ino

// The I2C master: every 100 ms writes the knob (A0, as 0-255) to the slave at address 8, then reads one byte back,
// the slave's button, and lights the L LED (13) while it is pressed.
#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin();
  pinMode(13, OUTPUT);
}

void loop() {
  byte brightness = analogRead(A0) / 4;
  Wire.beginTransmission(8);
  Wire.write(brightness);
  byte error = Wire.endTransmission();
  if (error) {
    Serial.print("No answer at 8, error ");
    Serial.println(error);
  } else if (Wire.requestFrom(8, 1) == 1) {
    bool pressed = Wire.read();
    digitalWrite(13, pressed);
    Serial.print("brightness ");
    Serial.print(brightness);
    Serial.println(pressed ? ", button pressed" : "");
  }
  delay(100);
}

uno2.ino

// The I2C slave at address 8: sets the LED on pin 9 to the brightness the master writes, and answers a read with
// its button's state (pin 2, pressed = 1).
#include <Wire.h>

volatile byte brightness = 0;

void onReceive(int count) {
  while (Wire.available()) brightness = Wire.read();
}

void onRequest() {
  Wire.write(digitalRead(2) == LOW ? 1 : 0);
}

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(9, OUTPUT);
  Wire.begin(8);
  Wire.onReceive(onReceive);
  Wire.onRequest(onRequest);
}

void loop() {
  analogWrite(9, brightness);
}

Lessons behind it