16*2 LCD problem

Hello,
I'm using the following code:

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  lcd.begin(16, 2);
  analogWrite(6, 100);

  if (!rtc.isrunning()) {
    Serial.println("RTC-ul nu este pornit!");
  }
}

void loop() {
  DateTime now = rtc.now();

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

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(now.day());
  lcd.print(".");
  lcd.print(now.month());
  lcd.print(".");
  lcd.print(now.year());

  Serial.print("Time: ");
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.print(now.second());
  Serial.print("   Date: ");
  Serial.print(now.day());
  Serial.print(".");
  Serial.print(now.month());
  Serial.print(".");
  Serial.println(now.year());

  delay(1000);
}

It prints the time for about 5 seconds before showing some weird characters and eventually not showing anything.

This code right here works perfectly fine

#include <LiquidCrystal.h>

  LiquidCrystal lcd(12,11,5,4,3,2);
  

void setup() {
    analogWrite(6,100);
    lcd.begin(16,2);
    
}

void loop() {

    lcd.setCursor(1,0);
    lcd.print("Hello world");
    
}

What could be the problem?

we don't see your wiring. nor your schematic.
It might be bad contacts or the missing/not grounded RW line.

I've been thinking about this, but I tried the 2nd code and that is working completely fine.

I am not very competent in the matter but I wanted to tell you that sometimes when it is rewritten over the screen everything gets messed up, try putting an LCD CLEAR at the beginning of the void loop.

The second code works because HELLO WORLD is the same every time, in your code the clock changes.
I'll give a shot at

lcd.clear()

Try this code:

// With LCD Clear()
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  lcd.begin(16, 2);
  analogWrite(6, 100);

  if (!rtc.isrunning()) {
    Serial.println("RTC-ul nu este pornit!");
  }
}

void loop() {
  DateTime now = rtc.now();

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

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(now.day());
  lcd.print(".");
  lcd.print(now.month());
  lcd.print(".");
  lcd.print(now.year());

  Serial.print("Time: ");
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.print(now.second());
  Serial.print("   Date: ");
  Serial.print(now.day());
  Serial.print(".");
  Serial.print(now.month());
  Serial.print(".");
  Serial.println(now.year());

  delay(1000);
  lcd.clear();
}
1 Like

Or this code:

// With if <10
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  lcd.begin(16, 2);
  analogWrite(6, 100);

  if (!rtc.isrunning()) {
    Serial.println("RTC-ul nu este pornit!");
  }
}

void loop() {
  DateTime now = rtc.now();

  lcd.setCursor(1, 0);
  lcd.print("Time: ");
  if (now.hour() < 10)
    lcd.print(0);
  lcd.print(now.hour());
  lcd.print(":");
  if (now.minute() < 10)
    lcd.print(0);
  lcd.print(now.minute());
  lcd.print(":");
  if (now.second() < 10)
    lcd.print(0);
  lcd.print(now.second());

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  if (now.day() < 10)
    lcd.print(0);
  lcd.print(now.day());
  lcd.print(".");
  if (now.month() < 10)
    lcd.print(0);
  lcd.print(now.month());
  lcd.print(".");
  lcd.print(now.year());

  Serial.print("Time: ");
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.print(now.second());
  Serial.print("   Date: ");
  Serial.print(now.day());
  Serial.print(".");
  Serial.print(now.month());
  Serial.print(".");
  Serial.println(now.year());

  delay(1000);
}

I tried using an I2C converter and that worked

What is on pin 6?

Your original code works as it should. Check your connections, especially grounds (LCD, RTC, Arduino).

https://wokwi.com/projects/370435657847858177

// https://forum.arduino.cc/t/16-2-lcd-problem/1148622/6

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  lcd.begin(16, 2);
  // analogWrite(6, 100);

  if (!rtc.isrunning()) {
    Serial.println("RTC-ul nu este pornit!");
  }
}

void loop() {
  DateTime now = rtc.now();

  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  if (now.hour() < 10)
    lcd.print("0");
  lcd.print(now.hour());
  lcd.print(":");
  if (now.minute() < 10)
    lcd.print("0");
  lcd.print(now.minute());
  lcd.print(":");
  if (now.second() < 10)
    lcd.print("0");
  lcd.print(now.second());

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  if (now.day() < 10)
    lcd.print("0");
  lcd.print(now.day());
  lcd.print(".");
  if (now.month() < 10)
    lcd.print("0");
  lcd.print(now.month());
  lcd.print(".");
  lcd.print(now.year());

  Serial.print("Time: ");
  if (now.hour() < 10)
    Serial.print("0");
  Serial.print(now.hour());
  Serial.print(":");
  if (now.minute() < 10)
    Serial.print("0");
  Serial.print(now.minute());
  Serial.print(":");
  if (now.second() < 10)
    Serial.print("0");
  Serial.print(now.second());
  Serial.print("   Date: ");
  if (now.day() < 10)
    Serial.print("0");
  Serial.print(now.day());
  Serial.print(".");
  if (now.month() < 10)
    Serial.print("0");
  Serial.print(now.month());
  Serial.print(".");
  Serial.println(now.year());

  delay(1000);
}

@xfpd
See what happens when seconds pass 59.......Should show 0, 1, 2.....
but show 09, 19, 29, ......

And when seconds minutes add up to 59.......Should show 0, 1, 2.....
but it shows 0 minutes and 159 seconds............

image

@ruilviana
Yes, it proceeds strangely... in the Serial Monitor, seconds proceed 1, 3, 5, 7, but in the LCD, 18, 38, 58, 78... (I could not capture it... cheap computer) so it looks like this is needing a "zero pad" and place the cursor before every second, minute, month and day.

Note that the second example I suggested to the OP fixes this problem.

I see your zeropad now. : )

(zeropad on wokwi)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.