//MASTER `#include <Wire.h>
#define SLAVE_ADDRESS 0x55
void setup() {
Wire.begin(); // Initialize I2C as Master
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("Master ready");
}
void loop() {
// Wait for the slave to respond to a ping message
pingSlave();
delay(3000);
receiveResponse();
delay(1000);
// Send the "Turn On" command
sendCommand("ON");
delay(6000); // Wait for the slave to respond
receiveResponse();
delay(15000);
// Send the "Turn Off" command
sendCommand("OFF");
delay(6000); // Wait for the slave to respond
receiveResponse();
delay(30000);
}
void pingSlave() {
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.print("PING"); // Send PING message
uint8_t error = Wire.endTransmission();
if (error != 0) {
Serial.printf("Ping failed: %u\n", error);
return; // No acknowledgment from slave
}
Serial.println("Send ping request successfully");
delay(100); // Wait briefly for the slave to process
}
void sendCommand(String command) {
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.print(command); // Send command to slave
uint8_t error = Wire.endTransmission(true);
Serial.print("Sent command: ");
Serial.println(command);
Serial.printf("endTransmission: %u\n", error);
Wire.flush(); // Ensure the buffer is cleared
}
void receiveResponse() {
byte response = 0; // Variable to store the received byte
Wire.requestFrom(SLAVE_ADDRESS, 3); // Request 1 byte from the slave
if (Wire.available()) {
response = Wire.read(); // Read the byte sent by the slave
Serial.println("Received response: " + String(response));
if (response == 200) {
Serial.println("Receive ping response");
} else if (response == 1) {
Serial.println("Motor ON");
} else if (response == 0) {
Serial.println("Motor OFF");
} else {
Serial.println("Unknown response");
}
} else {
Serial.println("No response received");
}
} // SLAVE
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SDA_2 27
#define SCL_2 14
#define MOTOR_ON_PIN 25
#define MOTOR_OFF_PIN 26
#define SLAVE_ADDRESS 0x55 // I2C Slave address
bool motorStatus = false; // false means OFF, true means ON
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, -1);
byte responseToSend = 0; // 1 for ON, 0 for OFF
// Callback to handle commands received from Master
void receiveCommand(int numBytes) {
String commandReceived = ""; // Clear previous command
while (Wire.available()) {
char c = Wire.read();
commandReceived += c;
}
Serial.println("Received command: " + commandReceived);
updateDisplay("Received: " + commandReceived);
if (commandReceived == "PING") {
Serial.println("Received PING, preparing PONG");
responseToSend = 200;
} else if (commandReceived == "ON") {
turnMotorOn();
responseToSend = 1; // Set response to 1 for motor ON
} else if (commandReceived == "OFF") {
turnMotorOff();
responseToSend = 0; // Set response to 0 for motor OFF
} else {
updateDisplay("Unknown Command");
Serial.println("Unknown Command");
}
}
// Callback to send responses to Master
void sendResponse() {
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(responseToSend);
Serial.println("Sent response: " + String(responseToSend));
uint8_t error = Wire.endTransmission();
Serial.printf("endTransmission: %u\n", error);
if (error == 0) {
Serial.println("Slave acknowledged communication.");
} else {
Serial.println("Failed to communicate with slave.");
}
}
void setup() {
// Initialize I2C as Slave
Wire.begin((uint8_t)SLAVE_ADDRESS);
Wire.onReceive(receiveCommand); // Set up receive callback
Wire.onRequest(sendResponse); // Set up request callback
Serial.begin(115200);
pinMode(MOTOR_ON_PIN, OUTPUT); // Set the ON relay pin as output
pinMode(MOTOR_OFF_PIN, OUTPUT); // Set the OFF relay pin as output
digitalWrite(MOTOR_ON_PIN, HIGH); // Ensure the ON relay is off initially
digitalWrite(MOTOR_OFF_PIN, HIGH); // Ensure the OFF relay is off initially
Serial.println("I2C Slave initialized successfully.");
Wire1.begin(SDA_2, SCL_2);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
updateDisplay("Slave Initialized");
}
void turnMotorOn() {
digitalWrite(MOTOR_ON_PIN, LOW); // Turn on the motor
motorStatus = true;
Serial.println("Motor turned ON");
updateDisplay("Motor turned ON");
}
void turnMotorOff() {
digitalWrite(MOTOR_OFF_PIN, HIGH); // Ensure the motor-off pin is inactive
motorStatus = false;
Serial.println("Motor turned OFF");
updateDisplay("Motor turned OFF");
}
void updateDisplay(const String& message) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(message);
display.display();
}
void loop() {
}` This is the Master and Slave code part. I didn't connect any motor in the slave, simply I connect master to slave I2C pins and ground pin. Additionly slave side connect OLED display. That all the schematic connections.