How to configure 2 I2C in one arduino uno r3

My homework required one LCD and RTC module to work up. But now i have use up A4 & A5 for SCL and SDA for my lcd screen. So i need to give to up lcd screen because of the RTC module device. The other way is using LiquidCrystal library instead of LiquidCrystal_I2C library. But i need to buy a new lcd screen. Any better solution for this ?

If your LCD and RTC have different I2C addresses they can share the I2C bus.

The whole point to I2C is that multiple devices can share the same bus - as long as they have different addresses. You can find out what address each device uses by reading the datasheet.
Connect both devices to the I2C bus and then run Nick Gammon's very useful I2C bus scanner sketch available at:

When I run that sketch, the output is:

I2C scanner. Scanning ...
Found address: 81 (0x51)
Found address: 84 (0x54)
Found address: 85 (0x55)
Found address: 86 (0x56)
Found address: 87 (0x57)
Found address: 104 (0x68)
Done.
Found 6 device(s).

0x68 is the DS3231 Chronodot realtime clock. 0x54 thru 0x57 are two EEPROMS and 0x51 is a PCF8593 real-time clock wired as an event counter.
I would have wired up the EEPROMS to use addresses 0x50 thru 0x53 but the PCF8593 uses 0x51 and that can't be changed. Fortunately, the EEPROM addresses can be changed so I set them to avoid the conflict.

Pete

Thanks el_supremo. i did it. I use I2C bus scanner and get the following result. Now i can show rtc on my lcd screen both using I2C.

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (9600);
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

ICscanerResult.png

Dear fuiyo,

When connecting all of the SDA pins on multiple I2C devices together, and all of their SCL together, there needs pull-up resistors between them. Then, then how do you choose the pull-up resistors?

piggybacking on this, do we always need pullup resistors? Meaning will some devices have their own built in say an ardafruit clock generator and Wiimote camera? Also when wiring these do we simply connect the SDA and SCL to a "bus" and connect the devices to said bus?

Wisdom:
Dear fuiyo,

When connecting all of the SDA pins on multiple I2C devices together, and all of their SCL together, there needs pull-up resistors between them. Then, then how do you choose the pull-up resistors?

johnwasser:
If your LCD and RTC have different I2C addresses they can share the I2C bus.

John;
I'm building a simple time display with push button. So the concept is, a person gets a shot, they have to wait a minimum of 15-30 minutes before leaving the facility. I press a button, the the Arduino gets the currrent time from the RTC & adds 15 minutes, then displays the "Wait until" time on the 7 segment led for 30 seconds, then goes back to top of loop until button is pressed again.

I ran the I2C scanner. 2 addresses show up: 0x68, 0x70. (RTC, LED backpack)
Pullup resisters? Is that to prevent over-current from the Uno?

I'm using an Uno right now, plan to try to get my sketch to fit on an Adafruit Trinket. Push button is an Adafruit Capacitive touch button, RTC is Adafruit DS3231, Adafruit 0.56" 4-Digit 7-Segment Display w/I2C Backpack - Green

The sketch for my timer works but does not wait for a button to be pressed, the LED example sketch by Adafruit works.

Waiting on answer concerning Pullup resister. I'm pretty sure the Uno is protected from over-current. Those guys have thought of everything. Don't want to chance it and have to wait for another in the mail.

Helps if I put my sketch up here huh!

Timer:

// Timer for displaying Now + 17 1/2 minutes; the desired
// time a donor must wait after receiving an immunization
//
//
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Button.h>
#include "Adafruit_LEDBackpack.h"
int BUTTON = 2;


RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

  pinMode(2, INPUT);// Pin 2 - Adafruit capacitive touch momentary switch

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(300); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time:
    // rtc.adjust(DateTime(2017, 3, 10, 19, 25, 0));// March 10th, 2017 @ 7:25pm
  }
}

void loop () {

  if (digitalRead(2) == LOW) {  // If button pressed, RUN LOOP CYCLE

    DateTime now = rtc.now();
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.println();
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.println();
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    Serial.println();


    // Serial.print( F('Patient must remain until:'));// ***** What's wrong with this stratement? ****

    // calculate 17 minutess and 30 seconds from now
    DateTime future = (now + (0, 0, 0, 0, 0, 1050)); // broke down 17 1/2 minutes into seconds and added that

    // Serial.print(" now + 17minutes & 30seconds: ");
    // Serial.print(future.year(), DEC);
    // Serial.print('/');
    // Serial.print(future.month(), DEC);
    // Serial.print('/');
    // Serial.print(future.day(), DEC);
    // Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    Serial.println();


    delay(10000);
  }
}

RTC:

/***************************************************
  This is a library for our I2C LED Backpacks

  Designed specifically to work with the Adafruit LED 7-Segment backpacks
  ----> http://www.adafruit.com/products/881
  ----> http://www.adafruit.com/products/880
  ----> http://www.adafruit.com/products/879
  ----> http://www.adafruit.com/products/878

  These displays use I2C to communicate, 2 pins are required to
  interface. There are multiple selectable I2C addresses. For backpacks
  with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
  with 3 Address Select pins: 0x70 thru 0x77

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

Adafruit_7segment matrix = Adafruit_7segment();

void setup() {
#ifndef __AVR_ATtiny85__
  Serial.begin(9600);
  Serial.println("Testing 1 2 3 4");
  Serial.println("Starting counter to show that all segments are working properly");
#endif
  matrix.begin(0x70);
}

void loop() {
  // try to print a number thats too long
  matrix.print(10000, DEC);
  matrix.writeDisplay();
  delay(500);

  // print a hex number
  matrix.print(0xBEEF, HEX);
  matrix.writeDisplay();
  delay(500);

  // print a floating point
  matrix.print(12.34);
  matrix.writeDisplay();
  delay(500);

  // print with print/println
  for (uint16_t counter = 0; counter < 9999; counter++) {
    matrix.println(counter);
    matrix.writeDisplay();
    delay(10);
  }

  // method #2 - draw each digit
  uint16_t blinkcounter = 0;
  boolean drawDots = false;
  for (uint16_t counter = 0; counter < 9999; counter ++) {
    matrix.writeDigitNum(0, (counter / 1000), drawDots);
    matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
    matrix.drawColon(drawDots);
    matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
    matrix.writeDigitNum(4, counter % 10, drawDots);

    blinkcounter += 50;
    if (blinkcounter < 500) {
      drawDots = false;
    } else if (blinkcounter < 1000) {
      drawDots = true;
    } else {
      blinkcounter = 0;
    }
    matrix.writeDisplay();
    delay(10);
  }
}

Hi,

I just noticed you made the same mistake I did with my watering project.

 pinMode(2, INPUT);// Pin 2 - Adafruit capacitive touch momentary switch
...
  if (digitalRead(2) == LOW) {  // If button pressed, RUN LOOP CYCLE

is always low or floating , unless you put it in pullup

like :

int hand = 4;
...
void setup(){

pinMode(hand,INPUT_PULLUP);// vlotter aansluiting  , pullup, dus andere pootje naar gnd
...

If you have your pin configured as an INPUT, and are reading a switch, when the switch is in the open state the input pin will be "floating", resulting in unpredictable results. In order to assure a proper reading when the switch is open, a pull-up or pull-down resistor must be used. The purpose of this resistor is to pull the pin to a known state when the switch is open. A 10 K ohm resistor is usually chosen,