Arduino UNO I2C Example Project

Learn what is I2C protocol and how to use it with devices that supports it. In this Arduino UNO I2C Example Project, we show how you can do communication between Arduino and Temperature IC.

In this Arduino UNO I2C Example Project, you will learn what is I2C communication and how to use it with Arduino UNO to transfer data to a Temperature sensor IC. I2C bus is widely used nowadays to interconnect and transfer data between Integrated Circuits(IC). Hence this example tutorial will provide an opportunity to learn about I2C.

I2C Communication

I2C(Inter-Integrated Circuit) protocol is a digital communication protocol that tells how to perform communication between two devices(Master and Slave(s)) for transferring data and how to interface the two devices. It provides robust and high speed two way communication path between two devices using only 2 wires. Hence it is also often called 2 wire digital communication. It was developed by Philips in early 1980s and later standardized in 1990s.

As shown in the figure below, Integrated Circuits(IC) that supports I2S protocols are connected to common I2S bus. The I2S bus has two wires called Serial Data Line (SDA) and Serial Clock Line (SCL). On the bus there is one(or more) master devices which controls one or multiple slave devices. The master device initiates all communication setup with the devices. Also note that in I2C communication, both the bus wires must be pulled up as depicted in the figure below.

Arduino UNO I2C Example Project

As depicted in the picture the Arduino UNO is a master device that can transfer data to slave devices such as DAC. LCD, temperature sensor IC, EEPROM etc which all share the same communication bus. In this case, only Arduino UNO can initiate communication.

Each of the slave have unique 7-bit(or 10-bit) address also called ID number. When the master device, the Arduino UNO want to communicate with one of the slave, it sends our the address of the slave. Only the slave device such as the temperature sensor device whose ID number matches will reply to the communication request.

Many devices or Integrated Circuit(IC) can also have multiple selectable ID number but most have fixed ID number. For example, the TC74 Temperature sensor IC has many selectable I2C ID address which you find in its datasheet.

The basic steps for controlling any I2C device are as follows:
1. Master sends a start bit.
2. Master sends 7-bit slave address of device it wants to talk to.
3. Master sends read (1) or write (0) bit depending on whether it wants to
write data into an I2C device’s register or if it wants to read from one of
the I2C device’s registers.
4. Slave responds with an “acknowledge” or ACK bit (a logic low).
5. In write mode, master sends 1 byte of information at a time, and slave
responds with ACKs. In read mode, master receives 1 of byte information
at a time and sends an ACK to the slave after each byte.
6. When communication has been completed, the master sends a stop bit.

Hardware setup and Interfacing for Arduino UNO I2C Example Project

The temperature sensor IC we will be using is TC74A0-5.0VAT. The pins of this IC is shown below from the datasheet.

Connect this SDA and SCLK pin to the A4 and A5 pins of the Arduino UNO respectively. Note that A4 and A5 pins must be used as they are the SDA and SCLK pins on Arduino UNO. Connect the ground to the ground of the Arduino and VDD to the 5V pin of Arduino. Then connect two 4.7KOhm resistors from the two I2C wires to the 5V supply of the Arduino. This is as shown below.

Arduino UNO I2C Example Project

Understanding the TC74 IC for I2C communication

Before writing program code to communicate with the TC74 IC we should study its datasheet to know how to use it for I2C communication. The datasheet is as you perhaps know can be downloaded for free. For purpose is write program that instruct Arduino UNO to read data from the sensor IC TC74.

From the datasheet, we can see that the TC74 IC can be read and written to. But we are only interested in reading the temperature data. Also the datasheet tell us that there are two registers. One register stores the temperature in Celsius degree and the other register is the configuration register.

The following paragraph from the datasheet tells how the I2C communication works.

“All data transfers have two phases and all bytes are transferred MSB first. Accesses are initiated by a START condition, followed by a device address byte and one or more data bytes. The device address byte includes a Read/Write selection bit. Each access must be terminated by a  STOP condition. A convention called “Acknowledge” (ACK) confirms receipt of each byte. Note that SDA can change only during periods when SCLK is low (SDA changes while SCLK is high are reserved for START and STOP conditions)”.

The “Read Byte Format” section in the datasheet outlines the process of reading the temperature from the TC74:
1. Send to the device’s address in write mode and write a 0 to indicate that you want to read from the data register.
2. Send to the device’s address in read mode and request 8 bits (1 byte) of information from the device.
3. Wait to receive all 8 bits of temperature information.

Lastly, you should note down the I2C address for your device from the datasheet which is 1001000b.

Writing Arduino I2C program code

Our aim in this Arduino UNO I2C Example Project is to read the temperature value stored in the TC74 temperature sensor IC and display on the Arduino IDE serial monitor. To do that we now write the program code to get temperature data via the I2C interface. In regards to the program code, to ease programming we will utilize the Arduino Wire library. The Arduino Wire library is the Arduino’s I2C communication library which makes it easy to use the I2C interface. To use the library we have to include it at the top of the program code.

The following is Arduino program code for this I2C example project.

In the program above, we have first included the Wire Library for I2C communication. Then we created a variable name TC74addr for the TC74 IC address.

In the setup() function we have specified to use serial transmission at 9600 baud rate. Also we have invoked the begin function of the wire object to initialize I2C communication protocol.

In the main loop() function, using Wire.beginTransmission(TC74addr) we have called the slave device for communication. The using Wire.write(0) we have send instruction to set the configuration for using the data register of the TC74 device. After that we used Wire.endTransmission() to tell the device that instruction byte is complete.

At this stage we have made connection to the TC74 slave device and set the proper configuration to read temperature data from it. The next part of the program is to read the temperature data. The statement Wire.requestFrom(TC74addr, 1) is a request to the address specified to read 1 byte of data. Using the while statement, while(Wire.available() == 0), we wait for the device to respond. When it is true, it means the slave device TC74 has data ready and we can read it. The statement  c = reads 1 byte of data that contains the temperature information into arduino and save it in variable c.

Finally using the serial.print() function we send the data read to the serial monitor.

Running the Program

Finally you should first connect USB cable to the Arduino UNO because the temperature value stored in arduino will be sent over the USB to the computer. Then you should compile the above I2C program code and upload it to the Arduino. After running the program you should open the serial monitor in Arduino IDE. You should get similar display of temperature as shown below.

Arduino UNO I2C Example Project

This completes the Arduino UNO I2C Example Project. Want to learn more? Learn how you can use Arduino with Matlab’s Simulink- Arduino Simulink Tutorial 1 – LED blinking.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Arduino Photoresistor Tutorial

Combinational vs Sequential digital VHDL circuits