- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
I2C (Inter-Integrated Circuit) communication is a protocol widely used for connecting microcontrollers and peripherals. This tutorial demonstrates how to implement I2C communication between two Arduino Uno boards in Proteus. We'll configure one Arduino as the Master and the other as the Slave. The Master sends data to the Slave, and the Slave responds by displaying the data on its Serial Monitor and blinking an LED.
Hardware Requirements
2 Arduino Uno boards
A computer with Proteus simulation software
Virtual connections in Proteus for the I2C communication (SDA and SCL)
I2C Overview
The I2C protocol uses two wires for communication:
SDA (Serial Data Line): Transfers data between devices.
SCL (Serial Clock Line): Synchronizes the communication.
Each device in the network has a unique address. In our example, the Slave Arduino has been assigned the address 6
.
The Master Code
Below is the code for the Master Arduino, which transmits data to the Slave.
#include <Wire.h>
int LED = 13; // Initialize pin 13 to drive LED
void setup() {
Wire.begin(); // Join I2C bus (address optional for Master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(6); // Transmit to device #6
Wire.write("x is"); // Sends five bytes
Wire.write(x); // Sends one byte
Wire.endTransmission(); // Stop transmitting
x++;
delay(500);
}
Explanation:
The
Wire.begin()
function initializes the I2C bus in Master mode.The
Wire.beginTransmission(6)
function specifies the Slave address (6).Wire.write()
sends a message and an incrementing value (x
) to the Slave.The
delay(500)
function provides a half-second interval between transmissions.
The Slave Code
Below is the code for the Slave Arduino, which receives and processes data from the Master.
#include <Wire.h>
int LED = 13;
void setup() {
pinMode(LED, OUTPUT); // Set LED pin as output
Wire.begin(6); // Join I2C bus with address #6
Wire.onReceive(receiveEvent); // Register receive event
Serial.begin(9600); // Start Serial Monitor for output
}
void loop() {
delay(100); // Wait for data from Master
}
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // Loop through all but the last byte
char c = Wire.read(); // Receive byte as a character
Serial.print(c); // Print the character to Serial Monitor
digitalWrite(LED, HIGH); // Turn LED on
delay(1000);
digitalWrite(LED, LOW); // Turn LED off
delay(500);
}
int x = Wire.read(); // Receive the last byte as an integer
Serial.println(x); // Print the integer to Serial Monitor
}
Explanation:
The
Wire.begin(6)
function assigns address6
to the Slave.The
Wire.onReceive(receiveEvent)
function registersreceiveEvent
as the callback for received data.The
receiveEvent
function processes incoming data:Characters are printed to the Serial Monitor.
The LED blinks with each received message.
The last byte (an integer) is printed separately.
Simulation in Proteus
Set Up the Components:
Place two Arduino Uno boards in Proteus.
Connect the SDA (A4) and SCL (A5) pins of both boards.
Add a virtual terminal to display Serial Monitor output by rightclicking and selecting PLace > Virtual instrument > Virtul terminal
Connect an LED to pin 13 of the Slave Arduino.
Load the Codes:
Load the Master and Slave codes into the respective Arduinos.
Run the Simulation:
Start the simulation in Proteus.
Observe the data being sent from the Master to the Slave.
Watch the LED blink on the Slave and verify the output in the Serial Monitor.
https://drive.google.com/file/d/1Kks9b--2dEbAKdhZacAFlNccbe-49aVk/view?usp=drive_link
Comments
Post a Comment