I2C Presentation
I2C Presentation
How It Works
Messages are broken up into two types of frames: an address frame,
where the controller indicates the peripheral to which the message is being
sent, and one or more data frames, which are 8-bit data messages passed
from the controller to the peripheral or vice versa. Data is placed on the
SDA line after SCL goes low, and is sampled after the SCL line goes high.
● Start Condition
To initiate the address frame, the controller device leaves SCL high
and pulls SDA low. This puts all peripheral devices on notice that a
transmission is about to start. If two controllers wish to take
ownership of the bus at one time, whichever device pulls SDA low
first wins the race and gains control of the bus. It is possible to issue
repeated starts, initiating a new communication sequence without
relinquishing control of the bus to other controllers; we'll talk about
that later.
● Address Frame
The address frame is always first in any new communication
sequence. For a 7-bit address, the address is clocked out most
significant bit (MSB) first, followed by an R/W bit indicating whether
this is a read (1) or write (0) operation.
The 9th bit of the frame is the NACK/ACK bit. This is the case for all
frames (data or address). Once the first 8 bits of the frame are sent,
the receiving device is given control over SDA. If the receiving device
does not pull the SDA line low before the 9th clock pulse, it can be
inferred that the receiving device either did not receive the data or did
not know how to parse the message. In that case, the exchange
halts, and it's up to the controller of the system to decide how to
proceed.
● Data Frames
After the address frame has been sent, data can begin being
transmitted. The controller will simply continue generating clock
pulses at a regular interval, and the data will be placed on SDA by
either the controller or the peripheral, depending on whether the R/W
bit indicates a read or write operation. The number of data frames is
arbitrary, and most peripheral devices will auto-increment the internal
register, meaning that subsequent reads or writes will come from the
next register in line.
● Stop condition
Once all the data frames have been sent, the controller will generate
a stop condition. Stop conditions are defined by a 0->1 (low to high)
transition on SDA after a 0->1 transition on SCL, with SCL remaining
high. During normal data writing operation, the value on SDA should
not change when SCL is high, to avoid false stop conditions.
I2C Module
The I2C module soldered on the back of the LCD is crucial for this
system, as it simplifies the wiring of the pins of the LCD. It converts serial
data from the Arduino, into parallel bits that can only be read by the LCD,
as it cannot read serial data.
Pins
● SDA - SDA (Serial Data) is the data signal where data is sent.
● SCL - SCL (Serial Clock) is the clock signal SCL is used to
synchronously clock data in or out of the target device.
● VCC and GND - are used to power the device.
Operation
An I²C-to-parallel converter like the PCF8574 works by receiving
serial data over the I²C bus, storing it in internal registers, and then setting
the states of its GPIO pins according to the received data. This allows a
microcontroller to control devices that require multiple parallel inputs using
a simple two-wire I²C interface, greatly reducing the number of GPIO pins
needed on the microcontroller.
4. Speed: In some cases, I2C can operate at higher speeds than UART,
especially for shorter distances and when using devices that support higher
I2C clock frequencies. However, SPI generally offers higher maximum
speeds compared to both I2C and UART.
However, there are also scenarios where UART or SPI might be preferred:
● Speed: SPI can achieve higher data rates than I2C, making it
suitable for applications that require fast data transfer because it uses
full-duplex transmission, while I2C only uses half-duplex, it does not
use addressing unlike I2C, can operate at higher frequencies, and it
is a simpler protocol to implement.
Implementation of I2C
A 20x4 LCD is used with an I2C module to simplify connections to the
Arduino board. It is used as an output for the Electromyography (EMG)
Sensor that detects muscle contractions, sends it into the Arduino for
conversion, and sends it into the LCD to visually display the readings.
The EMG sensors, which are these pads, sense muscle contractions
through a piezo element, and convert it into voltage through the module.
There is an LED on the module to indicate the changes in contractions. The
OUTPUT pin of the module is connected to the A0 pin of the Arduino as it
outputs different voltage levels, unlike sensors that can sometimes only
output 5V or 0V, which are digital.
The Arduino then converts the corresponding voltages, into serial
data, to be sent to the LCD, which outputs the changes in the heart rate of
the person. The LCD can be also used to map if the value goes up, or
down or if there are no changes.
Code
The operation of the code is simple, but the construction is not.
We start by including the libraries for the LCD with an I2C module, then set
the A0 pin for the EMG sensor as INPUT.
Here, we can use to map where we will put the text on the display,
and state the corresponding symbols if there are changes in the heart rate.