In this article we will look at what an I2C interface (ay-tu-si, i-two-tse) is, what are its features and how to work with it.
It is necessary
- - Arduino;
- - digital potentiometer AD5171;
- - Light-emitting diode;
- - 220 ohm resistor;
- - 2 resistors for 4.7 kOhm;
- - connecting wires.
Instructions
Step 1
The IIC serial communication protocol (also called I2C - Inter-Integrated Circuits) uses two bidirectional communication lines to transfer data, called the SDA (Serial Data) bus and the SCL (Serial Clock) bus. There are also two power lines. The SDA and SCL buses are pulled up to the power bus through resistors.
There is at least one Master in the network that initiates data transmission and generates synchronization signals. The network also has slaves that transmit data at the request of the master. Each slave device has a unique address at which the master addresses it. The device address is indicated in the passport (datasheet). Up to 127 devices can be connected to one I2C bus, including several masters. Devices can be connected to the bus during operation, i.e. it supports hot plugging.
Step 2
Arduino uses two ports to work on the I2C interface. For example, in Arduino UNO and Arduino Nano, analog port A4 corresponds to SDA, analog port A5 corresponds to SCL.
For other board models:
Arduino Pro and Pro Mini - A4 (SDA), A5 (SCL)
Arduino Mega - 20 (SDA), 21 (SCL)
Arduino Leonardo - 2 (SDA), 3 (SCL)
Arduino Due - 20 (SDA), 21 (SCL), SDA1, SCL1
Step 3
To facilitate the exchange of data with devices via the I2C bus, a standard "Wire" library has been written for the Arduino. It has the following functions:
begin (address) - initialization of the library and connection to the I2C bus; if no address is specified, then the connected device is considered the master; 7-bit addressing is used;
requestFrom () - used by the master to request a certain number of bytes from the slave;
beginTransmission (address) - the beginning of data transfer to the slave device at a specific address;
endTransmission () - termination of data transmission to the slave;
write () - writing data from the slave in response to a request;
available () - returns the number of bytes of information available for receiving from the slave;
read () - read a byte transferred from the slave to the master or from the master to the slave;
onReceive () - indicates the function to be called when the slave device receives a transmission from the master;
onRequest () - Indicates a function to be called when the master receives a transmission from the slave.
Step 4
Let's see how to work with the I2C bus using Arduino.
First, we will assemble the circuit, as shown in the figure. We will control the brightness of the LED using the AD5171 64-position digital potentiometer, which connects to the I2C bus. The address at which we will refer to the potentiometer is 0x2c (44 in decimal).
Step 5
Now let's open a sketch from the "Wire" library examples:
File -> Samples -> Wire -> digital_potentiometer. Let's load it into Arduino memory. Let's turn it on.
You see, the brightness of the LED rises cyclically, and then suddenly goes out. In this case, we control the potentiometer using the Arduino via the I2C bus.