Connecting multiple I2C device on Arduino Uno R3




Hello every one ! welcome back ! It’s nice to show you my new tutorial after long time .Today we will learn how to connect multiple I2C device (typically for demo temperature sensor ) to the Arduino Uno .I recommend download datasheet of that I2C device you are going to interface with Arduino before jump into coding.

I2C communication Protocal

I²C (also referred as IIC or TWI ) pronounced as I square C is widely used communication protocal . If we wish to use multiple device with serially communicating device then I2C is the best way to communication between a microcprocessor and multiple device like motors , LCD , LED , EEPROM , etc . In this method we can control ( communicate ) up to 128 device which is almost impossible with conventional USART method . I2C bus consists of two lines called Serial Data Line (SDA) and Serial Clock Line (SCL) ,Communication between devices is master and slave based. Master generates clock signal, initiates and terminates data transfer. In many case microcontroller is master and it generate clock and address for multiple slave devices . From electrical point of view I2C devices use open drain (open collector) pins. In order to operate correctly SDA and SCL lines require pull up resistors. Typically 4.7kΩ resistors are used as shown in figure below .And The devices that we are using TWI must support TWI protocal .

The complete tutorial about I2C or TWI protocal can be found in this section . The short description of this project is below the code .

Connecting multiple I2C device on Arduino Uno R3 (Code)


#include
#include
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);
#define DEV_ID 0x90 >> 1
#define DEV_ID2 0x9E >> 1
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4);
lcd.print("ELECTRONIFY.ORG");
Wire.begin();
Wire.beginTransmission(DEV_ID); // connect to DS1621 (#0
Wire.write(0xAC); // Access Config
Wire.write(0x02); // set for continuous conversion
Wire.beginTransmission(DEV_ID); // restart
Wire.write(0xEE); // start conversions
Wire.endTransmission();
Wire.begin();
Wire.beginTransmission(DEV_ID2); // connect to DS1621 (#0)
Wire.write(0xAC); // Access Config
Wire.write(0x02); // set for continuous conversion
Wire.beginTransmission(DEV_ID2); // restart
Wire.write(0xEE); // start conversions
Wire.endTransmission();
}
void loop()
{
int8_t firstByte;
int8_t secondByte;
int8_t firstByte2;
int8_t secondByte2;
float temp = 0;
float temp2 = 0;
Wire.beginTransmission(DEV_ID);
Wire.write(0xAA); // read temperature command
Wire.endTransmission();
Wire.requestFrom(DEV_ID, 2); // request two bytes from DS1621 (0.5 deg. resolution)
firstByte = Wire.read(); // get first byte
secondByte = Wire.read(); // get second byte
temp = firstByte;
Wire.beginTransmission(DEV_ID2);
Wire.write(0xAA); // read temperature command
Wire.endTransmission();
Wire.requestFrom(DEV_ID2, 2); // request two bytes from DS1621 (0.5 deg. resolution)
firstByte2 = Wire.read(); // get first byte
secondByte2 = Wire.read(); // get second byte
temp2 = firstByte2;
if (secondByte) // if there is a 0.5 deg difference
temp += 0.5;
if (secondByte2) // if there is a 0.5 deg difference
temp2 += 0.5;
Serial.println(temp);
Serial.println(temp2);
lcd.setCursor(0, 1);
lcd.print("Temp 1 ");
lcd.setCursor(9, 1);
lcd.print(temp);
lcd.setCursor(0, 2);
lcd.print("Temp 2");
lcd.setCursor(9, 2);
lcd.print(temp2);
}

Connecting multiple I2C device on Arduino Uno R3 (Schematic Diagram)

Connecting multiple I2C device on Arduino Uno R3 schematic diagram




READ  Arduino Relay Control Tutorial

Short Description

The code is very simple to understand . Here are some conceptual points used in the code

  • First we included wire.h library for I2C transmission
  • Also included LCD library and defined LCD used pin using LiquidCrystal lcd(12, 13, 8, 9, 10, 11); function
  • After that we defined address for both  I2C temperature device
  • Enable I2C devices using  wire.begin function with their respective address using data sheet of DS1261
  • now get the data from temperature sensor write and request function
  • And those data are displayed on Lquid Crystal Display also displayed on Serial communication window of arduino compiler
  • That’s all

The complete project file with simulation , and code can be download from this link download complete project file .
Please share this project if this helped you !




Leave a Comment

*
= 5 + 3

(Spamcheck Enabled)

Read previous post:
9V to 1kV DC DC converter
9V to 1kV DC/DC converter

Currently I'm working on a device to electrocute home insects like cockroaches, progress is small because they are smarter than...

Close
Scroll to top