2

Recently I've been working on an automatic dog feeder dispenser (that dispenses by turning a screw attached to a stepper motor). Currently I have a Nema 17 connected to an L298N stepper driver which is connected to Arduino pins 9-12 and a DS1307 RTC connected to analog pins A4 and A5 (I would have a picture but the wiring is all over the place). I would like to connect a 16x2 LCD screen (with a potentiometer already on the back and 4 connecting pins) but from everything I've seen online, it also uses analog pins A4 and A5 which are occupied by the RTC. How would I go about connecting the LCD?

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RTClib.h>
#include <SoftwareSerial.h>
#include <Stepper.h>

RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x3F , 16 , 2);
Stepper myStepper(200 , 9 , 10 , 11 , 12);

void setup() {  
  lcd.init();  
  lcd.print("yes"); 
  Wire.begin();                                           
  Serial.begin(9600);                                       
  rtc.adjust(DateTime(2021 , 7 , 15 , 3 , 23 , 0));
  myStepper.setSpeed(100);
}

void loop() {
  DateTime now = rtc.now();
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.print(now.second());
  Serial.println(" ");

  lcd.print(now.hour());
  lcd.print(":");
  lcd.print(now.minute());
  lcd.print(":");
  lcd.print(now.second());
  lcd.println(" ");

  if ((now.hour() == 7 and now.minute() == 0 and now.second() == 0)) {    
    myStepper.step(1000);

}
  delay(1000);

}
4
  • 1
    I2C communication lines support more than one device on the same pins, so just connect the LCD to the same pins... or you could just look under your Arduino to locate the 2 more I2C pins(Left of the Aref pin)
    – Coder9390
    Aug 12 '21 at 1:19
  • arduino.cc/en/Reference/Wire
    – Juraj
    Aug 12 '21 at 4:15
  • I connected both the LCD and RTC to A4 and A5 via a breadboard and it works great. Thanks!
    – Carter
    Aug 13 '21 at 2:09
  • Here is one example where LCD and RTC both are conencted to same I2C pins. wokwi.com/arduino/projects/306828496357818944
    – ArduinoFan
    Aug 13 '21 at 17:05
1

As mentioned in the comments: Your LCD and the RTC both use the I2C bus for communication. That bus is designed to have many devices simultaneously connected. For this to work every slave device on the bus (the Arduino is the master device, which has no address) has to have a unique address. In your code you write, that the LCD is on address 0x3F. The RTC DS1307 is 0b1101000 or 0x68 (same value in binary or hexadecimal representation) (see the DS1307 datasheet, page 12).

So you can connect both devices to the same pins, as the communication interface there already supports that. The pins A4 and A5 are the I2C pins of the Arduino, so you need to connect them there for using the Arduinos I2C hardware.

1

Here are my 2 cents. As the other answer suggests, you can always use the same I2C lines for devices. The devices will be differentiated via an I2C address. I have compiled one example based on your code. Here is the link to the simulation

The code is below

#include <Wire.h>
#include "LiquidCrystal_I2C.h"
#include <RTClib.h>

RTC_DS1307 rtc;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Wire.begin();

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

  lcd.init();
  lcd.backlight();
  lcd.print("yes");
}

void loop() {
  DateTime now = rtc.now();
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.print(now.second());
  Serial.println(" ");

  lcd.setCursor( 0,0);
  lcd.print(now.hour());
  lcd.print(":");
  lcd.print(now.minute());
  lcd.print(":");
  lcd.print(now.second());

  if ((now.hour() == 7 and now.minute() == 0 and now.second() == 0)) {
    // myStepper.step(1000);
  }

  delay(1000);
}

As you can see, I2C lines are shared between the RTC module and the LC display module.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.