Sub topic 14
Making an Arduino Act as an I2C Target
Assume you know Arduino setup() and loop(), can use the Serial Monitor, and understand basic I2C transactions. This chapter uses target for the device that responds to requests. Older documentation may call it a “slave.” The controller, often a second Arduino, starts every transaction.
An Arduino target listens at a 7-bit address. The controller sends bytes to that address, or asks the target to return bytes. The target does not normally choose when communication begins.
Target events
The Wire library provides two callback functions:
onReceive()runs after the controller sends one or more bytes.onRequest()runs when the controller requests data.
Callbacks are short functions called by the Wire library while it handles the bus. Do not use delay(), print to Serial, or perform long calculations inside them. Store received data quickly, then process it later in loop().
#include <Wire.h>
const uint8_t TARGET_ADDRESS = 8;
volatile uint8_t lastValue = 0;
void receiveEvent(int byteCount) {
while (Wire.available()) {
lastValue = Wire.read();
}
}
void requestEvent() {
Wire.write(lastValue);
}
void setup() {
Wire.begin(TARGET_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
void loop() {
uint8_t value;
noInterrupts();
value = lastValue;
interrupts();
// Use value here. Keep the bus callbacks short.
delay(10);
}Wire.begin(8) gives this Arduino address 8 in decimal, or 0x08 in hexadecimal. The controller must use the same 7-bit address. When the controller sends several bytes, receiveEvent() reads them from the Wire buffer. This example keeps the last byte received.
When the controller requests one byte, requestEvent() places lastValue in the transmit buffer. The controller then reads that byte. The target cannot send data spontaneously through I2C.
Communication sequence
A typical exchange has two separate transactions:
- The controller sends a value to address
8. - Later, the controller requests one byte from address
8. - The target returns the value it saved.
The final NACK tells the target that the controller does not want another byte. If the target must return multiple bytes, call Wire.write() with a buffer or write each byte in order.
Common errors
- Giving both devices the same target address creates a conflict.
- Calling
Wire.begin()without an address makes the Arduino a controller on many Arduino-compatible boards, not a target. - Expecting
onRequest()to run without a controller read request misunderstands the protocol. - Sending too many bytes can overflow the Wire transmit buffer. Keep responses small or define a clear multi-byte format.
- Sharing variables between callbacks and
loop()requires care. Usevolatile, and protect multi-byte values from partial reads when the platform can interrupt between bytes.
Exercise
A controller sends the bytes 10 and 25 to target address 8. The callback saves every received byte in lastValue. What value will the target return during the next one-byte request?
Show answer
It returns 25, because the callback overwrites the stored value with each byte and retains the last one.