0% found this document useful (0 votes)
16 views11 pages

Embedded Systems Report Lab 7

Embedded systems report

Uploaded by

SOPHIA JERARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Embedded Systems Report Lab 7

Embedded systems report

Uploaded by

SOPHIA JERARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Embedded Systems Report Lab 7

Made by: Essoufia EL JERARI Grp A1


Supervisored by: Adnane ADDAIM

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

What is I2C Communication Protocol?


The term IIC stands for “Inter Integrated Circuits”. It is normally denoted as I2C or I squared
C or even as 2-wire interface protocol (TWI) at some places but it all means the same. I2C is a
synchronous communication protocol meaning, both the devices that are sharing the information
must share a common clock signal. It has only two wires to share information out of which one is
used for the cock signal and the other is used for sending and receiving data.

How I2C Communication works?


I2C communication was first introduced by Phillips. As said earlier it has two wires, these two
wires will be connected across two devices. Here one device is called a master and the other
device is called as slave. Communication should and will always occur between two a Master
and a Slave. The advantage of I2C communication is that more than one slave can be connected
to a Master.

The complete communication takes place through


these two wires namely, Serial Clock (SCL) and
Serial Data (SDA).

Serial Clock (SCL): Shares the clock signal


generated by the master with the slave

Serial Data (SDA): Sends the data to and from


between the Master and slave.

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.

As shown in the above diagram above,

The falling edge of SDA is the hardware trigger


for the START condition. After this all devices on
the same bus go into listening mode.

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.

R/W bit indicates the direction of


transmission of following bytes, if it is
HIGH means the slave will transmit and
if it is low means the master will
transmit.

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

I2C Line Pin in Arduino

SDA A4

SCL A5

I2C communication between two Arduino UNO boards


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:

Parts we will need


Arduino Uno Rev3 x2

USB Cable A-B x1

Jumper Wires Male-Male x3

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.

Arduino Sketch Code

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:

void receiveEvent(int bytes) {


x = Wire.read();
}

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

I2C Serial Adapter

It is also known as I2C Module. It has total of 20 male pins. 16


pins are faced to rear side and 4 pins faced towards front side.
The 16 pins for connect to 16x2 LCD and the 2 pins out of 4
pins are SDA and SCL. SDA is the serial data pin and SCL is
the clock pin. The rest 2 pins for power supply (Vcc and
ground).There is a POT on the I2C Module. We can control the
contrast of the LCD display by rotating this POT. And there is a
jumber fixed on the module. When we remove the jumber, the
backlight of the LCD display will go OFF.
Address of I2C 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

You might also like