DA-3 (Embedded Systems)
DA-3 (Embedded Systems)
DA - 3
Objective:
Components needed:
Step-by-Step Instructions:
1. Connect the SDA pin (A4) of the Master Arduino to the SDA pin
(A4) of the Slave Arduino.
2. Connect the SCL pin (A5) of the Master Arduino to the SCL pin (A5)
of the Slave Arduino.
Shared Ground:
1. Connect the GND pin of the Master Arduino to the GND pin of the
Slave Arduino.
a. This ensures a common reference for both devices.
#include <Wire.h>
void setup() {
Wire.begin(); // Join I2C bus as master
Serial.begin(9600); // Start Serial communication
Serial.println("Enter 0 to turn OFF LED or 1 to
turn ON LED:");
}
void loop() {
if (Serial.available()) {
char command = Serial.read(); // Read user input
if (command == '0' || command == '1') {
Wire.beginTransmission(8); // Address of the
slave
Wire.write(command); // Send the command
Wire.endTransmission();
}
}
}
Slave Code:
C/C++
#include <Wire.h>
#define LED_PIN 13
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as
output
Wire.begin(8); // Join I2C bus with address 8
Wire.onReceive(receiveEvent); // Register event
handler
}
void loop() {
// Nothing to do here; everything is handled in
the receiveEvent
}
void receiveEvent(int bytes) {
char command = Wire.read(); // Read the command
from master
if (command == '0') {
digitalWrite(LED_PIN, LOW); // Turn LED OFF
} else if (command == '1') {
digitalWrite(LED_PIN, HIGH); // Turn LED ON
}
}