bclose

61 – Combining several I2C devices

Objectives

 

 

    • Use the I2C bus with several devices.
    • Connect several I2C devices to the same bus.
    • Visualize on a LCD display the date and time read via an RTC.

 

BILL OF MATERIALS

Arduino UNO

Arduino Uno or equivalent.

Breadboard

A solderless breadboard.

Jumper wires

Some jumper wires

RTC clock A I2C TinyRTC digital clock with a backup battery.
16x2 LCD Display

A  I2C 16×2 LCD display.

 

INTRODUCTION AND ASSEMBLY

 

We have already used different I2C devices in several chapters, but so far we have always done it in isolation. And the good thing when using the I2C interface is that it allows us to connect and control several devices of this kind very easily. If you are not familiar with the I2C bus we recommend you, before going ahead, taking a look at the chapter in which is explained in enough detail what it is and how it works, using precisely the LCD display that we also use in this chapter. And in the same way, if the RTC doesn’t ring a bell here is the corresponding chapter.

If you have already these two subjects under control, you will see how to integrate the two components into a single assembly and within a single sketch is very easy. The only thing you have to bear in mind is which address is associated to each device, and define it correctly in the sketch. And in the case of this RTC, you do not even have to do that, since the own library manages it by itself.

As for the assembly, the two devices share the same Vcc and GND pins for the power supply, and of course the pins corresponding to the I2C bus: SCA and SCL, which in the case of the Arduino UNO correspond to the pins A4 and A5. As you can see, thanks to this bus we have saved a lot of wires and it makes it much easier to include new devices of this kind, just in case we want to expand our project.

montaje componentes I2C

 

PROGRAMMING

 

If the assembly did not have too much mystery, the sketch is just as simple. To begin, we will include the libraries we are going to use, define a variable with the address of the LCD (in the case of the RTC is not necessary) and create an instance of type LiquidCrystal_I2C:

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27

LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7);

Inside the setup function we initialize the LCD, indicating that it is a 16 × 2 LCD display, and setting the RTC to the correct date and time:

void setup()
{
   Serial.begin(115200);
   lcd.begin (16, 2);                    	// Initialize the display with 16 characters and 2 lines
   lcd.setBacklightPin(3, POSITIVE);
   lcd.setBacklight(HIGH);
   // while (!Serial) ;                  	// Only for the Arduino Leonardo
   setSyncProvider(RTC.get);             	// We use the RTC
   setTime(10, 40, 00, 14, 11, 2016);     	// 14 November 2016, 10:40:00
   if (timeStatus() != timeSet)
      Serial.println("Unable to sync with the RTC");
   else
      Serial.println("RTC has set the system time");
}

And once it is done we will reuse the functions we use in the RTC chapter, but instead of showing it on the display we will use the LCD library to display the time and date on the LCD:

void digitalClockDisplay()
{
   lcd.home (); 		// Go home
   lcd.print(hour());
   printDigits(minute());
   printDigits(second());
   lcd.setCursor ( 0, 1 ); 	// Go to the 2nd line
   lcd.print(day());
   lcd.print("/");
   lcd.print(month());
   lcd.print("/");
   lcd.print(year());
}

void printDigits(int digits)
{
   lcd.print(":");
   if (digits < 10)
      lcd.print('0');
   lcd.print(digits);
}


void printDigits(int digits)
{
   lcd.print(":");
   if (digits < 10)
      lcd.print('0');
   lcd.print(digits);
}

And finally we have just to call this function every second to show the date and time:

void loop()
{
 digitalClockDisplay();
 delay(1000);
}

You can download here the full sketch:

Sketch 61.1

 

Showing the date and time on the LCD

WHAT TO DO IF SEVERAL DEVICES HAVE THE SAME ADDRESS

 

At this point someone could have thought about connecting several devices of the same kind that have the same address. As almost everything, this has already happened to someone previously, and that is why I2C devices usually have a pair of connections labelled C1 and C2, so you can change the direction by welding or cutting these tracks, which allows up to 4 different addresses.

Change I2C address

UOnce changed you just have to use the program I2C scanner and see which is its new address to assign it.

 

SUMMARY

 

In this chapter we have learnt several important things:

 
    • We have learnt to connect several I2C devices.
    • It is very easy to use several I2C devices in the same sketch.
    • We know now how to change the address of these kind of devices.
 

No Comments

Give a Reply