Unit 3 Aurdino 2
Unit 3 Aurdino 2
Unit 3 Aurdino 2
UNIT III
2
I2C
3
I2C
• Arduino and Genuino boards to share information with each other. ...
The I2C protocol involves using two lines to send and receive data:
Øa serial clock pin (SCL) that the Arduino or Genuino Master board pulses at a
regular interval, and
Øa serial data pin (SDA) over which data is sent between the two devices.
• I2C Speeds
4
Voltage
• Arduino UNO is the most prolific version of Arduino board, and runs
at 5v DC. It's I2C interface (Analog pins A4 and A5) will drive to 5v.
Raspberry Pi runs at 3.3v. Both of it's I2C interfaces run at 3.3v.
Lines
• The two wires, or lines are called Serial Clock (or SCL) and Serial Data
(or SDA). The SCL line is the clock signal which synchronize the data
transfer between the devices on the I2C bus and it's generated by the
master device. The other line is the SDA line which carries the data.
5
• The I2C communication bus is very popular and broadly used by many
electronic devices because it can be easily implemented in many
electronic designs which require communication between a master
and multiple slave devices or even multiple master devices.
• The easy implementations comes with the fact that only two wires
are required for communication between up to almost 128 (112)
devices when using 7 bits addressing and up to almost 1024 (1008)
devices when using 10 bits addressing.
6
How It Works
7
8
I2C Protocol
• The data signal is transferred in sequences of 8 bits.
• So after a special start condition occurs comes the first 8 bits sequence which
indicates the address of the slave to which the data is being sent. After each 8
bits sequence follows a bit called Acknowledge.
• After the first Acknowledge bit in most cases comes another addressing
sequence but this time for the internal registers of the slave device.
• Right after the addressing sequences follows the data sequences as many
until the data is completely sent and it ends with a special stop condition.
9
• Let’s take even closer look at these events. The start condition occurs
when data line drops low while the clock line is still high. After this
the clock starts and each data bit is transferred during each clock
pulse.
• The device addressing sequence stars with the most significant bit
(MSB) first and ends with the least significant bit (LSB) and it’s
actually composed of 7 bits because the 8th bit is used for indicating
whether the master will write to the slave (logic low) or read from it
(logic high).
10
• The next bit AKC/ NACK is used by the slave device to indicate whether it has successfully
received the previous sequence of bits.
• So at this time the master device hands the control of the SDA line over to the slave device
and if the slave device has successfully received the previous sequence it will pull the SDA
line down to the condition called Acknowledge.
• If the slave does not pull the SDA line down, the condition is called Not Acknowledge, and
means that it didn’t successfully received the previous sequence which can be caused by
several reasons.
• For example, the slave might be busy, might not understand the received data or
command, cannot receive any more data and so on. In such a case the master device
decides how it will proceed.
11
• Next is the internal registers addressing.
• The internal registers are locations in the slave’s memory containing various
information or data.
• For example the ADX345 Accelerometer has a unique device address and
addition internal registers addresses for the X, Y and Z axis.
• So if we want to read the data of the X-axis, first we need to send the device
address and then the particular internal register address for the X-axis.
• These addresses can be found from datasheet of the sensor.
• After the addressing, the data transfer sequences begin either from the master
or the slave depending of the selected mode at the R/W bit.
• After the data is completely sent, the transfer will end with a stop condition
which occurs when the SDA line goes from low to high while the SCL line is high.
12
• WHAT IS I2C?
• I2C is an acronym for Inter-Integrated
Circuit.
• I2C is a low-speed serial
communication protocol used for
transferring data over short distances.
• If you need to transfer data over a large
distance, this protocol is not
recommended.
• An example of a simple I2C network is
shown below:
13
• As you can see in the diagram above, the advantage of using I2C is that you
only need two wires to communicate with multiple devices.
• All data passes through the two wires to and from the master and slave
devices.
• Since the Arduino has a limited number of input/output pins, I2C can allow
you to connect more devices.
• Many Arduino sensors and modules are enabled for I2C communication.
• THE I2C NETWORK
An I2C network consists of a master device and a slave device. The
master and slave devices are connected by a bus. I2C networks can have
multiple master devices and slave devices.
14
SLAVE DEVICES
Each slave device has an I2C address that is used to identify the device. The I2C address makes it
possible for a master device to send data to a particular slave device on the bus.
MASTER DEVICES
Master devices can send and receive data. Slave devices respond to whatever a master device sends.
When sending data on the bus, only one device can send data at a time.
THE BUS
An I2C bus is simply two wires that connect all of the I2C devices in the network. The two wires are
called SDA and SCL. The SDA wire is used for sending the actual data back and forth between the master and
slave devices. The SCL line carries the clock signal used for communication timing. Pull-up resistors are used to
keep both wires in a HIGH state by default.
LOGIC LEVELS
The Arduino outputs I2C signals at a 5V logic level. But I2C devices can operate at a range of different
logic level voltages. An I2C device that operates at 3.3V could be damaged if connected to the Arduino. The
device’s datasheet should tell you it’s logic level voltage. 15
MAKING THE ARDUINO TALK I2C
• To demonstrate how to use I2C on the Arduino, let’s
build a project that sends data back and forth between
two Arduinos. This project will read the position of a
potentiometer connected to a master Arduino, send
the information over I2C, and change the blink rate of
the LED on the slave Arduino.
ARDUINO I2C PINS
• The Arduino has dedicated pins for I2C, which have
built-in pull-up resistors as required by the I2C protocol.
• For Arduino Uno boards, these are pins A4 and A5. Pin
A4 is the SDA pin, and pin A5 is the SCL pin. In the
Arduino Uno R3 version, there is another set of I2C pins
near the USB socket:
16
HARDWARE
COMPONENTS
• To build this project,
you’ll need the following
parts:
WIRING DIAGRAM
• After you gather the parts, it’s
time to assemble the project.
Follow the wiring diagram
below to connect everything: 17
• We don’t need pull-up resistors
on the SDA and SCL lines,
because they’re built into the
Arduino’s I2C pins already.
SKETCH FOR THE ARDUINO
MASTER DEVICE
• We have two Arduinos in our
I2C network, so we have two
sets of sketches. One is for the
master Arduino, and the other
is for the slave Arduino.
• Open the Arduino IDE and
upload the code below to the
master Arduino:
18
EXPLANATION OF THE CODE
The basic part of the code for both the
master and slave devices is what I call THE WIRE LIBRARY
the blink logic code. To use the Arduino’s built-in
To blink the pin 13 LED on the Arduinos, we I2C interface, we will use the
need to do the following: Wire library. This library is
•Add global variables byte i2c_rcv,int included with the Arduino
time_start, stat_LED and byte value_pot at IDE, so there’s no need to
the top of our sketch install it.
•Initialize values of the global variables inside The Wire library has ready-
the setup() section
made I2C functions to make
•Initialize pin 13 of the Arduino as an output
the programming easier for
pin using pinMode()
•Add the blink logic code inside the loop()
us. To use the functions in
the Wire library, we first
need to add it to our sketch.
In the sketch above, we do
that with #include <Wire.h>. 19
• After including the library, the next thing SENDING DATA OVER I2C
to do is to join the device on the I2C bus. • After saving the value from pin A0 in the
• The syntax for this is Wire.begin(address). variable value_pot, we can send the value
• The address is optional for master devices. over I2C. Sending data over I2C involves
three functions:
• So, for the master Arduino sketch, we just
add the code Wire.begin(); inside
the setup() function.
• Now in the loop() section, the code will
make the Arduino read the potentiometer
value connected to pin A0, and save that
value in the variable value_pot.
20
Wire.endTransmission().
22
• After issuing the command WIRE.READ();
write.requestFrom(0x08,1); it should • To get the data available, we use the
be followed by a read command to get function Wire.read() and save the
the response from the I2C bus. return value to the variable i2c_rcv.
WRITE.AVAILABLE() • Each call to the function Wire.read()
• First, we check if there is data gets only one byte of data from the
available on the bus. I2C bus.
• We do this by using the
function write.available() inside a
conditional if() statement.
• The function write.available() returns
the number of bytes waiting to be
read.
23
SKETCH FOR THE ARDUINO SLAVE DEVICE
Now upload this code to the slave Arduino:
24
EXPLANATION OF THE CODE • We will join the I2C network as a slave
• For the slave device, there is a slight device by adding the code
difference in the code. Wire.begin(0x08); inside the setup()
section.
• The first difference is with EVENT HANDLERS
Wire.begin(address).
• For slave devices, the address is a • The next task is to add event handlers to
requirement. our code to manage the data received
from other devices in the I2C network.
• For our project, the address for the • Event handlers are pieces of code that
slave device will be 0x08. manage events that our device will likely
• It can be any address you want, but encounter while running.
make sure it is unique in the I2C
network.
• Some I2C slave devices also have their
I2C addresses defined, so check the
datasheet first.
25
WIRE.ONRECEIVE() WIRE.ONREQUEST()
• In the setup() part of the sketch, we add • The next event handler that we will use is
the function Wire.onReceive(handler) to Wire.onRequest(handler).
register a function (the handler) that will • This function is used on slave devices and
manage the data received. we’ll call our works similarly to Wire.onReceive()
handler function dataRcv().
• Take note that the function name can be • The only difference is that it handles data
anything you want. In the sketch above, request events.
Wire.onReceive(dataRcv) in called in the • Data requests come from master devices.
setup() section. So in the setup() section we add the
• At the end of the sketch is the code for the code Wire.onRequest(datRqust);.
handler function. It is initialized as void • At the end of our sketch, we add the
dataRcv(int numBytes). function void dataRqst().
• The parameter int numBytes contains the
number of bytes of data received.
26
• Note that Wire.onRequest() handlers do OPERATION
not accept any parameters. The function • Adjust the potentiometer on the master
dataRqst() only contains Wire.write(). device to control the blink rate of the
• We don’t need Wire.beginTransmission() slave device LED.
and Wire.endTransmission() because the • Adjust the potentiometer on the slave
Wire library already handles the responses device to control the blink rate of the
from the slave devices. master device LED.
TESTING OUR ARDUINO I2C • Our code takes the master’s
COMMUNICATION PROJECT potentiometer position and sends it to
• Here comes the most exciting part – the slave device over I2C.
power-up and testing! • The slave device then uses the received
• Using the Arduino IDE, upload the master value to adjust the blink delay time of the
Arduino sketch to one of the Arduinos. LED.
• Then upload the slave Arduino sketch to • The same thing happens with the slave’s
the other Arduino. potentiometer position.
27
SPI Protocol
28
• When you connect a microcontroller to a sensor, display, or other
module, do you ever think about how the two devices talk to each
other? What exactly are they saying? How are they able
to understand each other?
• Communication between electronic devices is like communication
between humans. Both sides need to speak the same language.
• In electronics, these languages are called communication protocols.
• Luckily for us, there are only a few communication protocols we need
to know when building most DIY electronics projects.
• In this series of articles, we will discuss the basics of the three most
common protocols: Serial Peripheral Interface (SPI), Inter-Integrated
Circuit (I2C), and Universal Asynchronous Receiver/Transmitter
(UART) driven communication.
29
• SPI, I2C, and UART are quite a bit slower than protocols like USB,
ethernet, Bluetooth, and WiFi, but they’re a lot more simple and use
less hardware and system resources.
• SPI, I2C, and UART are ideal for communication between
microcontrollers and between microcontrollers and sensors where
large amounts of high speed data don’t need to be transferred.
30
SERIAL VS. PARALLEL COMMUNICATION
• Electronic devices talk to each other by sending bits of data through wires
physically connected between devices. A bit is like a letter in a word, except
instead of the 26 letters (in the English alphabet), a bit is binary and can
only be a 1 or 0. Bits are transferred from one device to another by quick
changes in voltage. In a system operating at 5 V, a 0 bit is communicated as
a short pulse of 0 V, and a 1 bit is communicated by a short pulse of 5 V.
• The bits of data can be transmitted either in parallel or serial form. In
parallel communication, the bits of data are sent all at the same time, each
through a separate wire. The following diagram shows the parallel
transmission of the letter “C” in binary (01000011):
31
In serial communication, the bits are sent
one by one through a single wire. The
following diagram shows the serial
transmission of the letter “C” in binary
(01000011):
32
INTRODUCTION TO SPI COMMUNICATION
• SPI is a common communication protocol used by many different devices. For
example, SD card modules, RFID card reader modules, and 2.4 GHz wireless
transmitter/receivers all use SPI to communicate with microcontrollers.
• One unique benefit of SPI is the fact that data can be transferred without
interruption. Any number of bits can be sent or received in a continuous stream.
With I2C and UART, data is sent in packets, limited to a specific number of bits.
Start and stop conditions define the beginning and end of each packet, so the
data is interrupted during transmission.
• Devices communicating via SPI are in a master-slave relationship. The master is
the controlling device (usually a microcontroller), while the slave (usually a sensor,
display, or memory chip) takes instruction from the master. The simplest
configuration of SPI is a single master, single slave system, but one master can
control more than one slave (more on this below).
33
• MOSI (Master Output/Slave Input) –
Line for the master to send data to the
slave.
• MISO (Master Input/Slave Output) –
Line for the slave to send data to the
master.
• SCLK (Clock) – Line for the clock signal.
• SS/CS (Slave Select/Chip Select) –
Line for the master to select which
slave to send data to.
• *In practice, the number of slaves
is limited by the load capacitance of
the system, which reduces the ability
of the master to accurately switch
between voltage levels.
34
HOW SPI WORKS
THE CLOCK
• The clock signal synchronizes the output of data bits from the master to the sampling of
bits by the slave. One bit of data is transferred in each clock cycle, so the speed of data
transfer is determined by the frequency of the clock signal. SPI communication is always
initiated by the master since the master configures and generates the clock signal. Any
communication protocol where devices share a clock signal is known as synchronous. SPI
is a synchronous communication protocol. There are also asynchronous methods that
don’t use a clock signal. For example, in UART communication, both sides are set to a
pre-configured baud rate that dictates the speed and timing of data transmission.
• The clock signal in SPI can be modified using the properties of clock polarity and clock
phase. These two properties work together to define when the bits are output and when
they are sampled. Clock polarity can be set by the master to allow for bits to be output
and sampled on either the rising or falling edge of the clock cycle. Clock phase can be
set for output and sampling to occur on either the first edge or second edge of the clock
cycle, regardless of whether it is rising or falling.
35
SLAVE SELECT
• The master can choose which slave it wants to talk to by setting the slave’s
CS/SS line to a low voltage level. In the idle, non-transmitting state, the
slave select line is kept at a high voltage level. Multiple CS/SS pins may be
available on the master, which allows for multiple slaves to be wired in
parallel. If only one CS/SS pin is present, multiple slaves can be wired to the
master by daisy-chaining.
MULTIPLE SLAVES
• SPI can be set up to operate with a single master and a single slave, and it
can be set up with multiple slaves controlled by a single master. There are
two ways to connect multiple slaves to the master. If the master has
multiple slave select pins, the slaves can be wired in parallel like this:
36
• If only one slave select pin is available, the
slaves can be daisy-chained like this:
37
MOSI AND MISO
• The master sends data to the slave bit by bit, in serial through the
MOSI line. The slave receives the data sent from the master at the
MOSI pin. Data sent from the master to the slave is usually sent with
the most significant bit first.
• The slave can also send data back to the master through the MISO
line in serial. The data sent from the slave back to the master is
usually sent with the least significant bit first.
38
STEPS OF SPI DATA TRANSMISSION
1. The master outputs the clock 2. The master switches the SS/CS
signal: pin to a low voltage state, which
activates the slave:
39
3. The master sends the data one 4. If a response is needed, the
bit at a time to the slave along the slave returns data one bit at a
MOSI line. The slave reads the time to the master along the
bits as they are received: MISO line. The master reads the
bits as they are received:
40
ADVANTAGES AND DISADVANTAGES OF SPI
There are some advantages and disadvantages to using SPI, and if given
the choice between different communication protocols, you should know
when to use SPI according to the requirements of your project:
ADVANTAGES
• No start and stop bits, so the data can be DISADVANTAGES
streamed continuously without interruption • Usestwo)
four wires (I2C and UARTs use
• No complicated slave addressing system like I2C
• No acknowledgement that the data
• Higher data transfer rate than I2C has been successfully received (I2C
(almost twice as fast) has this)
• Separate MISO and MOSI lines, • No form of error checking like the
parity bit in UART
so data can be sent and received at the same time
• Only allows for a single master
41
Interfacing with Sensors
42
How sensors are connected to Arduino Uno?
• The connections for the IR sensor with the Arduino are as
follows: Connect the negative wire on the IR sensor to GND on
the Arduino. Connect the middle of the IR sensor which is the VCC to
5V on the Arduino. Connect the signal pin on the IR sensor to pin 8
on the Arduino.
43
What is interfacing in Arduino?
• Arduino senses the environment by receiving inputs from add-on
devices such as sensors, and can control the world around it by
adjusting lights, motors, and other actuators. ... You'll also learn about
the use of Arduino-specific shields and the shields software libraries
to interface with the real world.
44
What is a sensor interface?
• Sensor interface ICs enable a system to read out information from the
input signal generated by complex sensors, providing a suitable
output signal that is easy for a host system to display or process.
How many sensors can you connect to Arduino?
• 3 Answers. If the sensors use I2C then the limit is 128 devices on the
bus, but they each have to have a unique address which are
sometimes "hard-coded" on the device by the manufacturer.
The Arduino Wired library is used to read/write I2C and it only takes
two pins.
45
How do sensors get data?
• With a sensor, a machine observes the environment and information
can be collected. A sensor measures a physical quantity and converts
it into a signal. Sensors translate measurements from the real world
into data for the digital domain.
What is an example of a sensor?
• The simplest example of a sensor is an LDR or a Light Dependent
Resistor. It is a device, whose resistance varies according to intensity
of light it is subjected to. ... This voltage can be calibrated to the
amount of light falling on the LDR. Hence, a Light Sensor.
46
How many sensors can Arduino Uno handle?
• four
• In total we have four 5v sensors that will draw a total of 260mA (or
0.26A). Next look up your specific arduino model's maximum current
output for the 5v pins. I'll use the Uno because it's an extremely
common board. The Uno can output a maximum of 200mA from its
Vcc (5v out pin).
47
• In this project, we are
going to control LEDs
using an IR sensor and a
remote. The IR sensor is a
1838B IR receiver.
Whenever a button on
the remote is pressed, it
will send an infrared
signal to the IR sensor in
the coded form. The IR
sensor will then receive
this signal and will give it
to the Arduino.
48
How Does It Work?
49
Circuit Diagram
• First, connect the four LEDs to the Arduino. Connect the positives of the four LEDs
to the pins 7, 6, 5, and 4. Connect the negative of the four LEDs to GND on the
Arduino through the 220 ohm resistors. The longer wires on the LEDs are positive
and the shorter wires are negative.
• Then connect the IR sensor to the Arduino. The connections for the IR sensor
with the Arduino are as follows:
• Connect the negative wire on the IR sensor to GND on the Arduino.
• Connect the middle of the IR sensor which is the VCC to 5V on the Arduino.
52
53
54
Code Explanation
55
• Next, we defined the pins where
we have connected the LEDs.
• We have connected the LEDs at
pins 7, 6, 5, and 4.
• So, we defined these pins as the
LED pins.
56
• In the setup function, we
defined the LED pins as the
output pins, because we are
giving the output to the LEDs
through those pins.
57
• In the loop function, first, we check
if any key has been pressed.
• If any key has been pressed, then
we compare that key with the keys
that we have defined in our code.
• If the key matches, then the LED
connected to that pin will light up.
• If the LED connected to that pin is
already lit up, then it will go down.
58
59
References:
• https://learn.sparkfun.com/tutorials/serial-communication
• D.Dale.Wheat, “Arduino Intrnals”, TIA Publication, 5th edition, 2011.
• https://www.sciencedirect.com/topics/engineering/analog-
communication
• https://ecedmans.files.wordpress.com/2014/03/analog-
communication-prabhakar-kapula.pdf
• https://howtomechatronics.com/tutorials/arduino/how-i2c-
communication-works-and-how-to-use-it-with-arduino/
• https://www.circuitbasics.com/how-to-set-up-i2c-communication-
for-arduino/
60
• https://www.circuitbasics.com/basics-of-the-spi-communication-
protocol/
• https://maker.pro/arduino/tutorial/how-to-control-leds-with-arduino-
ir-sensor-and-remote
61