One Master / one slave using I2C Protocol control six LED arduino uno :
- To control three LEDs on a slave Arduino using I2C communication from a master Arduino, where ‘1’ turns on LED1, ‘2’ turns on LED2, ‘3’ turns on LED3, ‘4’ turns off LED1, ‘5’ turns off LED2, and ‘6’ turns off LED3, you can use the following code.
باللغة العربية
Codes for this Project:
Master Code:
#include <Wire.h>
void setup() {
Wire.begin(); // Initialize I2C communication visit osief.com
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
char command; // Command received from the Serial Monitor
Serial.println(“Enter 1 to turn on LED1, 2 for LED2, 3 for LED3, 4 to turn off LED1, 5 to turn off LED2, 6 to turn off LED3:”);
while (!Serial.available()) {} // Wait for user input
command = Serial.read(); // Read the command osief.com
Wire.beginTransmission(9); // Slave address is 9 (you can change it if needed)
Wire.write(command); // Send the command to the slave
Wire.endTransmission();
delay(100); // Give some time for the slave to process osief.com
}
// ██████╗ ███████╗██╗███████╗███████╗ ██████╗ ██████╗ ███╗ ███╗ //██╔═████╗██╔════╝██║██╔════╝██╔════╝ ██╔════╝██╔═══██╗████╗ ████║ //██║██╔██║███████╗██║█████╗ █████╗ ██║ ██║ ██║██╔████╔██║ //████╔╝██║╚════██║██║██╔══╝ ██╔══╝ ██║ ██║ ██║██║╚██╔╝██║ //╚██████╔╝███████║██║███████╗██║ ██╗ ╚██████╗╚██████╔╝██║ ╚═╝ ██║ // ╚═════╝ ╚══════╝╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝
Slave Code:
#include <Wire.h>
int ledPins[] = {9, 10, 11}; // LEDs connected to digital pins 9, 10, and 11 osief.com
void setup() {
Wire.begin(9); // Initialize I2C communication with address 9
Wire.onReceive(receiveEvent); // Set up the receive event
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Initialize all LEDs to off state osief.com
}
Serial.begin(9600); // Initialize serial communication for debugging
}
// ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄▄ // ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌ ▐░░▌ //▐░█░█▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌ ▐░▐░▌ //▐░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌▐░▌ ▐░▌▐░▌ //▐░▌ ▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▐░▌ ▐░▌ //▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ //▐░▌ ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌ ▐░▌ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌ ▐░▌▐░▌ ▀ ▐░▌ //▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ //▐░█▄▄▄▄▄█░█░▌ ▄▄▄▄▄▄▄▄▄█░▌ ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌ // ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌ // ▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀
void loop() {
// Add any additional tasks for the slave if needed
}
void receiveEvent(int byteCount) {
char command = Wire.read(); // Read the command sent by the master osief.com
if (command == ‘1’) {
digitalWrite(ledPins[0], HIGH); // Turn on LED1 osief.com
Serial.println(“LED1 turned ON”);
} else if (command == ‘2’) {
digitalWrite(ledPins[1], HIGH); // Turn on LED2 osief.com
// ██████ ███████ ██ ███████ ███████ ██████ ██████ ███ ███ //██ ████ ██ ██ ██ ██ ██ ██ ██ ████ ████ //██ ██ ██ ███████ ██ █████ █████ ██ ██ ██ ██ ████ ██ //████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ // ██████ ███████ ██ ███████ ██ ██ ██████ ██████ ██ ██
Serial.println(“LED2 turned ON”);
} else if (command == ‘3’) {
digitalWrite(ledPins[2], HIGH); // Turn on LED3
Serial.println(“LED3 turned ON”);
} else if (command == ‘4’) {
digitalWrite(ledPins[0], LOW); // Turn off LED1 osief.com
Serial.println(“LED1 turned OFF”);
} else if (command == ‘5’) {
digitalWrite(ledPins[1], LOW); // Turn off LED2 osief.com
Serial.println(“LED2 turned OFF”);
} else if (command == ‘6’) {
digitalWrite(ledPins[2], LOW); // Turn off LED3 osief.com
Serial.println(“LED3 turned OFF”);
}
}
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
osief.com
0sief