Master is ESP 32 S3 board (not Xiao), 4.7k pullup resistors.
Slave MG24 code:
#include <Wire.h>
#define I2C_ADDRESS 0x42
uint8_t buffer[12]; // Dummy 12-byte data
void onRequestHandler() {
Wire.write(buffer, sizeof(buffer));
Serial.println("buffer sent");
}
void setup() {
Serial.begin(115200);
delay(100);
// Fill buffer with dummy data for testing
for (int i = 0; i < sizeof(buffer); i++) {
buffer[i] = i + 1;
}
Wire.begin(I2C_ADDRESS); // Start as I2C slave
Wire.onRequest(onRequestHandler);
Serial.println("MG24 I2C Slave ready");
}
void loop() {
delay(100);
}
Master Code:
#include <Arduino.h>
#include <Wire.h>
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Starting I2C scan on GPIO16 (SDA) and GPIO21 (SCL)");
Wire.begin(16, 21);
}
void loop() {
byte error, address;
int nDevices = 0;
Serial.println("Scanning...");
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at 0x");
Serial.println(address, HEX);
nDevices++;
}
delay(50); // Small delay to allow for I2C bus to stabilize
}
if (nDevices == 0) {
Serial.println("No I2C devices found.");
}
delay(5000);
}
The result of scanning:
I2C device found at 0x1
I2C device found at 0x2
I2C device found at 0x3
… (all addresses)
I2C device found at 0x7D
I2C device found at 0x7E