#include #define SLAVE_ADDRESS 0x02 #define TRIG_PIN 2 #define ECHO_PIN 3 void setup() { pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); Wire.begin(); // Initialize as master } void loop() { // Measure distance long duration, distance; digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH); distance = duration * 0.034 / 2; // Convert to cm // Send distance to slave Wire.beginTransmission(SLAVE_ADDRESS); Wire.write(distance); Wire.endTransmission(); delay(100);