Embedded Systems Report Lab 7
Embedded Systems Report Lab 7
1
Table of Contents
Introduction......................................................................................................................3
Theory...............................................................................................................................3
What is I2C Communication Protocol?........................................................................................3
How I2C Communication works?................................................................................................4
I2C in Arduino............................................................................................................................5
I2C communication between two Arduino UNO boards.....................................................6
Parts we will need......................................................................................................................6
BreadBoard Layout.....................................................................................................................6
Schematic..................................................................................................................................7
Arduino Sketch Code..................................................................................................................7
Slave Sketch................................................................................................................................................. 7
Master Sketch............................................................................................................................8
Code explanation.......................................................................................................................9
Proteus 8 simulation................................................................................................................10
I2C communication between Arduino and LCD.................................................................10
I2C Serial Adapter.....................................................................................................................10
Address of I2C LCD...................................................................................................................11
Proteus 8 Simulation................................................................................................................12
2
Introduction
This lab experience is about an interesting serial communication protocol, which is I2C ( Inter
Integrated Circuit).
It's usually used to communicate between components on motherboards in cameras and in any
embedded electronic system. Here, we will make an I2C bus using two Arduinos. We will
program one master Arduino to command the other slave Arduino to blink its built-in LED once
or twice depending on the received value. We will use I2C communication between two
arduino boards and send (0 to 127) values to each other. Values will be displayed on the 16x2
LCD connected to each of the Arduino. Here one Arduino will act as Master and another one
will act as Slave. So let’s start with the introduction about I2C communication.
Theory
3
At any given time only the master will be able to initiate the communication. Since there is more
than one slave in the bus, the master has to refer to each slave using a different address. When
addressed only the slave with that particular address will reply back with the information while
the others keep quit. This way we can use the same bus to communicate with multiple devices.
The voltage levels of I2C are not predefined. I2C communication is flexible, means the device
which is powered by 5v volt, can use 5v for I2C and the 3.3v devices can use 3v for I2C
communication. But what if two devices
which are running on different voltages, need
to communicate using I2C? A 5V I2C bus
can’t be connected with 3.3V device. In this
case voltage shifters are used to match the
voltage levels between two I2C buses.
There are some set of conditions which frame a transaction. Initialization of transmission begins
with a falling edge of SDA, which is defined as ‘START’ condition in below diagram where
master leaves SCL high while setting SDA low.
In the same manner, rising edge of SDA stops the transmission which is shown as ‘STOP’
condition in above diagram, where the master leaves SCL high and also releases SDA to go
HIGH. So rising edge of SDA stops the transmission.
Each bit is transmitted on each clock cycle, so it takes 8 clock cycles to transmit a byte. After
each byte either sent or received, ninth clock cycle is held for the ACK/NACK
(acknowledged/not acknowledged). This ACK bit is generated by either slave or master
depending upon the situation. For ACK bit, SDA is set to low by master or slave at 9 th clock
cycle. So it is low it considered as ACK otherwise NACK.
4
I2C in Arduino
SDA A4
SCL A5
5
BreadBoard Layout
Schematic
6
Follow these steps to connect two Arduino UNOs using I2C:
1. Connect pins A4 and A5 on one Arduino to the same pins on the other one.
2. The GND line has to be common for both Arduinos. Connect it with a jumper.
We also have a schematic and a "breadboard" implementation, both easy to follow. Luckily, it's a
simple implementation.
Slave Sketch
#include <Wire.h>
int Led=7;
int x=0;
void setup () {
pinMode(Led, OUTPUT);
Serial.begin(9600);
Wire.begin(9);
Wire.onReceive(receiveEvent);
}
void receiveEvent (int bytes) {
x=Wire.read (); }
void loop() {
if (x==0) {
digitalWrite(Led, HIGH);
Serial.println(x);
delay(200);
}
else if (x==1)
{
digitalWrite (Led, LOW);
Serial.println(x);
delay(200); }
}
7
Master Sketch
#include <Wire.h>
int x=0;
int slaveAddress=9;
int pushButton=6;
int ButtonState;
void setup () {
pinMode(pushButton, INPUT);
Serial.begin(9600);
Wire.begin();
}
void loop () {
ButtonState=digitalRead(pushButton);
if (ButtonState==1) {
x=1; }
else {
x=0; }
Wire.beginTransmission(slaveAddress);
Wire.write(x);
Serial.println(x);
Wire.endTransmission();
delay(200);
}
Code explanation
First, let's look at the master. We need to include the required Wire.h library:
Then, in the setup function, we begin the I2C bus using the Wire.begin() function. If no argument
is provided in the function, Arduino will start as a master. Lastly, we send a character x. We use
the following functions to
begin a transmission to the device with the address 9, write the character, and then stop the
transmission:
Now let's explore the slave Arduino code. We also include the Wire.h library here, but now we
start the I2C bus using Wire.begin(9). The number in the argument is the address we want to use
for the Arduino. All devices with address 9 will receive the transmission.
Now we need to react somehow when we receive an I2C transmission. The following function
appends a trigger function whenever a character is received. Better said, whenever the Arduino
receives a character on I2C, it will run the function we tell it to run:
8
Wire.onReceive(receiveEvent);
And this is the function. Here, we simply store the value of the received character:
If loop(), we simply interpret that character to blink the built-in LED at different
speeds depending on the received character.
Proteus 8 simulation
9
I2C communication between Arduino and LCD
Before starting we need to know about addressing of I2C devices. Every device which can
attached to MCU have an address. We need to know this address for communicate with that
particular device.
You can see three solder pads on the I2C module. which is labeled as A0, A1 and A2. This
is Address selectors. ie, each solder pads have one upper potion and a one lower potion. if,
there is a connection between upper potion with lower connection it is called "Connected"
otherwise it is called "Not connected". When A0, A1, A2 are in "Not Connected" condition (
A0 = 0, A1 = 0, A2 = 0) the address would be 0x27. In default the A0, A1, A2 are in "Not
connected" condition. And some time default address is 0x3F. There is no need to change
the address of the I2C module when we use only one LCD. But when we use more than one
LCD, need to change the address. Because two or more different device can't communicate
with the same address. For more address see the table given below.
10
Proteus 8 Simulation
11