2 Arduino Uno Communicating using I2C | Proteus 8 + Arduino IDE

 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.

The schematic of I2C



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:

  1. SDA (Serial Data Line): Transfers data between devices.

  2. 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:

  1. The Wire.begin() function initializes the I2C bus in Master mode.

  2. The Wire.beginTransmission(6) function specifies the Slave address (6).

  3. Wire.write() sends a message and an incrementing value (x) to the Slave.

  4. 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:

  1. The Wire.begin(6) function assigns address 6 to the Slave.

  2. The Wire.onReceive(receiveEvent) function registers receiveEvent as the callback for received data.

  3. 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

  1. Set Up the Components:

    • Place two Arduino Uno boards in Proteus.


Schematic


    • 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

      Place

Virtual terminal



    • Connect an LED to pin 13 of the Slave Arduino.

  1. Load the Codes:

    • Load the Master and Slave codes into the respective Arduinos.

  2. 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.

The data snipet from the virtual instrument

Download the full project from here, subscribe to my channel in youtube, and get some products from amazon using my links!

https://drive.google.com/file/d/1Kks9b--2dEbAKdhZacAFlNccbe-49aVk/view?usp=drive_link



Comments