arduino lcd problem

Hello, everybody!

So I am making project about arduino weighting scale with attendance system in it. In the process i stumbled upon a little problem:

When i start the start the system my lcd is working fine (it's showing everything correctly) until the part where it has to show me the weight of the items I am measuring and the pieces.

I am posting the code in a hope that somebody will point me in the right direction.

P.S: It's probably possible that the problem may be in my schematic, but I am not sure.

Thank you for your help!

#include "HX711.h"
#include <LiquidCrystal.h> 
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC


#define SS_PIN 10
#define RST_PIN 9
#define CS_SD 3

// Create a file to store the data
File myFile;

// Instance of the class for RTC
RTC_DS1307 rtc;

// Define check in time
const int checkInHour = 9;
const int checkInMinute = 5;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Variable to hold the tag's UID
String uidString;
 
MFRC522 mfrc522(SS_PIN, RST_PIN);

HX711 scale;
float elements = 2.5;

LiquidCrystal lcd(A2,A1,5,4,A0,A5);
const int BackLight = 8;
String LCDLine1,LCDLine2;

int n;
const int buzzer = 2;
float rounded;

void updateLCD () {
   lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(LCDLine1);
  
  
  lcd.setCursor(0,1);
  lcd.print(LCDLine2);

}

void setup() {
  Serial.begin(9600);
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  SD.begin(CS_SD);
  lcd.begin(16,2);
  pinMode(BackLight, OUTPUT);
  digitalWrite(BackLight, HIGH);
  LCDLine1="Welcome";
  LCDLine2="Scan your ID";
  updateLCD();
  while(true){
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
  
    continue;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
 tone(buzzer, 2000); 
 noTone(buzzer);       
    continue;
  }

   
  
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "9E 7E 85 89") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(3000);
    break;
  }
 
 else   {
    Serial.println(" Access denied");
    delay(3000);
  }
 }
 
  Serial.println("HX711 Demo");
  LCDLine1="Initializing ...";
  LCDLine2="";
  updateLCD();

   // Setup for the SD card
  Serial.print("Initializing SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Setup for the RTC  
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if(!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
  // parameter "gain" is ommited; the default value 128 is used by the library
  // HX711.DOUT  - pin #A1
  // HX711.PD_SCK - pin #A0
  scale.begin(A4, A3);

  

  scale.set_scale(1900.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(10), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
            // by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
 Scale();
 updateLCD();
}
void logCard() {
  // Enables SD card chip select pin
  digitalWrite(CS_SD,LOW);
  
  // Open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // If the file opened ok, write to it
  if (myFile) {
    Serial.println("File opened ok");
    myFile.print(uidString);
    myFile.print(", ");   
    
    // Save time on SD card
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    myFile.close();
    
 // Print time on Serial monitor
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close();

    
    userCheckInHour = now.hour();
    userCheckInMinute = now.minute();
    digitalWrite(CS_SD,HIGH);
  }
}

void Scale() 
  { 
     float average = scale.get_units(10);
if (average < 0.3)
{
average = 0;
}
 

  float number =  scale.get_units()/elements;//divide the average of 20 readings with the elements value to get the number of elements
   n = round(number);
   //n = (int)number; // convert float into int
   
  Serial.print("\t| elements:\t");
  Serial.print(n); //serial monitor
  // lcd.clear();
       //set cursor first row
  LCDLine1=String(average,1)+" g"; //print on lcd average value from 10 raw readings 1 time
  LCDLine2=String(n)+" pcs";
  
  Serial.print("\t| average:\t");
  Serial.println(average); // serial monitor -||-
  
 
   //print PCS
  
  scale.power_down();             // put the ADC in sleep mode
  delay(1000);
  scale.power_up();
  updateLCD();
  }

Update: I figured out its the rtc code that bugs me. After i removed it everything went fine, but i still cant figure out where to put the rtc code so i can save the information (UID, time, date) from the RFID card.

LiquidCrystal lcd(A2,A1,5,4,A0,A5);

A5 is SCL which is an i2c bus pin used by the rtc. Use a different pin for the display.

cattledog:

LiquidCrystal lcd(A2,A1,5,4,A0,A5);

A5 is SCL which is an i2c bus pin used by the rtc. Use a different pin for the display.

Hey cattledog! Thanks for the help. It worked.

But i still seem to strugle with the rtc and sd card. I can't make it to save the information on the sdcard. The only thing it does is to open data file on the sd card without saving any information.

My main goal is to be able to save the data from the load cell sensor in a file to a RFID UID.

Example: Today 27.11.19 UID: 9E 45 32 12 Elements: 1000

I'm not sure if this is the correct forum section, so sorry for it.

Thanks for the help!

I can't make it to save the information on the sdcard.

Where do you ever call logCard()?

Hello cattledog,

I've updated the code but it's still seems to not working as it should.

I've tried using an example just for the RTC module and the output on the serial monitor is just egyptian symbols. This tells me my rtc connection with the arduino is wrong or there is problem in the code which i can't figure out. Probably this is the reason why i cant save any data to the file on the SD card.

#include "HX711.h"
#include <LiquidCrystal.h> 
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h> // for the SD card
#include "RTClib.h" // for the RTC


#define SS_PIN 10
#define RST_PIN 9
#define CS_SD 3

// Create a file to store the data
File myFile;

// Instance of the class for RTC
RTC_DS1307 rtc;

// Define check in time
const int checkInHour = 9;
const int checkInMinute = 5;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Variable to hold the tag's UID
String uidString;
 
MFRC522 mfrc522(SS_PIN, RST_PIN);

HX711 scale;
float elements = 2.5;

LiquidCrystal lcd(A2,A1,5,4,A0,7);
const int BackLight = 8;
String LCDLine1,LCDLine2;



int n;
const int buzzer = 2;
float rounded;

void updateLCD () {
   lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(LCDLine1);
  
  
  lcd.setCursor(0,1);
  lcd.print(LCDLine2);

}

void setup() {
  Serial.begin(9600);
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  lcd.begin(16,2);
  pinMode(BackLight, OUTPUT);
  digitalWrite(BackLight, HIGH);
  LCDLine1="Welcome";
  LCDLine2="Scan your ID";
  updateLCD();

  if (SD.begin(CS_SD))
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
 

  while(true){
  // Look for new cards02`11
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
  
    continue;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...  
  digitalWrite(CS_SD,LOW); // enables the cs pin
 
  // If the file opened ok, write to it

  
  if (myFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (myFile.available()) {
      Serial.write(myFile.read());
   }
    myFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  } 
 
    digitalWrite(CS_SD,HIGH);
  }
    break;
  }
  
   
  while(true){
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
   for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "9E 7E 85 89") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(3000);
    break;
  }
 
 else   {
    Serial.println(" Access denied");
    delay(3000);
  }
  }
 
  Serial.println("HX711 Demo");
  LCDLine1="Initializing ...";
  LCDLine2="";
  updateLCD();

  
  // parameter "gain" is ommited; the default value 128 is used by the library
  // HX711.DOUT  - pin #A1
  // HX711.PD_SCK - pin #A0
  scale.begin(A4, A3);

  

  scale.set_scale(1900.f);                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("After setting up the scale:");

  Serial.print("read: \t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("read average: \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("get value: \t\t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("get units: \t\t");
  Serial.println(scale.get_units(10), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
            // by the SCALE parameter set with set_scale

  Serial.println("Readings:");
}

void loop() {
 Scale();
 updateLCD();
 myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {    
     DateTime now = rtc.now();
     myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close(); // close the file
}
}
void Scale() 
  { 
     float average = scale.get_units(10);
if (average < 0.3)
{
average = 0;
}
 

  float number =  scale.get_units()/elements;//divide the average of 20 readings with the elements value to get the number of elements
   n = round(number);
   //n = (int)number; // convert float into int
   
  Serial.print("\t| elements:\t");
  Serial.print(n); //serial monitor
  // lcd.clear();
       //set cursor first row
  LCDLine1=String(average,1)+" g"; //print on lcd average value from 10 raw readings 1 time
  LCDLine2=String(n)+" pcs";
  
  Serial.print("\t| average:\t");
  Serial.println(average); // serial monitor -||-
  
 
   //print PCS
  
  scale.power_down();             // put the ADC in sleep mode
  delay(1000);
  scale.power_up();
  updateLCD();
  }

Thanks for the help again.

Best regards,
NexTySa

So, I've tested the RTC module with the following code:

// DS1302:  RST pin    -> Arduino Digital 2
//          DATA pin   -> Arduino Digital 3
//          CLK pin  -> Arduino Digital 4

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

DS1302 rtc(5, 6, 7);

LiquidCrystal lcd(A2,A1,5,4,A0,A5);
const int BackLight = 8;

void setup()
{
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup LCD to 16x2 characters
  lcd.begin(16, 2);
  pinMode(BackLight, OUTPUT);
  digitalWrite(BackLight, HIGH);
  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(THURSDAY);        // Set Day-of-Week to FRIDAY
rtc.setTime(18, 38, 0);     // Set the time to 12:00:00 (24hr format)
rtc.setDate(28, 11, 2019);   // Set the date to August 6th, 2010
}

void loop()
{
  // Display time centered on the upper line
  lcd.setCursor(4, 0);
  lcd.print(rtc.getTimeStr());
  
  // Display abbreviated Day-of-Week in the lower left corner
  lcd.setCursor(0, 1);
  lcd.print(rtc.getDOWStr(FORMAT_SHORT));
  
  // Display date in the lower right corner
  lcd.setCursor(6, 1);
  lcd.print(rtc.getDateStr());

  // Wait one second before repeating :)
  delay (1000);
}

And it seems that it stuck on the year 2000 and it shows only 00:00:00.

Whats wrong? Is my battery dead?

NexTySa:
So, I've tested the RTC module with the following code:

// DS1302:  RST pin    -> Arduino Digital 2

//          DATA pin  -> Arduino Digital 3
//          CLK pin  -> Arduino Digital 4

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

DS1302 rtc(5, 6, 7);

LiquidCrystal lcd(A2,A1,5,4,A0,A5);
const int BackLight = 8;

void setup()
{
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
 
  // Setup LCD to 16x2 characters
  lcd.begin(16, 2);
  pinMode(BackLight, OUTPUT);
  digitalWrite(BackLight, HIGH);
  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(THURSDAY);        // Set Day-of-Week to FRIDAY
rtc.setTime(18, 38, 0);    // Set the time to 12:00:00 (24hr format)
rtc.setDate(28, 11, 2019);  // Set the date to August 6th, 2010
}

void loop()
{
  // Display time centered on the upper line
  lcd.setCursor(4, 0);
  lcd.print(rtc.getTimeStr());
 
  // Display abbreviated Day-of-Week in the lower left corner
  lcd.setCursor(0, 1);
  lcd.print(rtc.getDOWStr(FORMAT_SHORT));
 
  // Display date in the lower right corner
  lcd.setCursor(6, 1);
  lcd.print(rtc.getDateStr());

// Wait one second before repeating :slight_smile:
  delay (1000);
}





And it seems that it stuck on the year 2000 and it shows only 00:00:00.

Whats wrong? Is my battery dead?

*EDIT: I tested the battery with my multimeter and it shows 2.45 V, so it should be working!

DS1302 rtc(5, 6, 7);
LiquidCrystal lcd(A2,A1,5,4,A0,A5);

You need to pay attention to which pins that you are using for what. Pin 5 is used for both the RTC and display.

// DS1302:  RST pin    -> Arduino Digital 2
//          DATA pin   -> Arduino Digital 3
//          CLK pin  -> Arduino Digital 4

Comments that are wrong are less than useless.

groundFungus:

DS1302 rtc(5, 6, 7);

LiquidCrystal lcd(A2,A1,5,4,A0,A5);




You need to pay attention to which pins that you are using for what. Pin 5 is used for both the RTC and display.



// DS1302:  RST pin    -> Arduino Digital 2
//          DATA pin  -> Arduino Digital 3
//          CLK pin  -> Arduino Digital 4




Comments that are wrong are less than useless.

Hello groundFungus,

I'am testing which library will work at the moment, this is why i didnt bother even changing the comments, because my CLK DATA RST pins are on 5,6,7. Also the reason why the RTC is not working is not that i'm using both pins.

I'm using another example at the moment:

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>

namespace {

// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

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

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

  // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);

  // Set the time and date on the chip.
  rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  delay(1000);
}

which outputs me exactly the same thing on the serial monitor.

As i read on the web DS1302 is pain in the a**. It seems it doesnt work by default.

Is there a way i can take the date and time from my PC. Even tho thats not the idea of the project I'm doing.

Thanks.

I would use the DS1307 that you have or else get a new DS3231. I would not struggle with the DS1302.

With the DS1307, can you see it on the i2c bus with this scanner sketch?

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

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; 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() {}

cattledog:
I would use the DS1307 that you have or else get a new DS3231. I would not struggle with the DS1302.

With the DS1307, can you see it on the i2c bus with this scanner sketch?

// I2C Scanner

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

#include <Wire.h>

void setup() {
  Serial.begin (115200);

// Leonardo: wait for serial port to connect
  while (!Serial)
    {
    }

Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
 
  Wire.begin();
  for (byte i = 8; 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() {}

Hey cattledog, i dont have DS1307. Also i dont wanna wait another one or two moths for my sensor to arrive. Have to deal with 1302 or maybe just remove it from the project..

Hey cattledog, i dont have DS1307

So what was this all about in the first posted sketch

#include <RTClib.h> // for the RTC
RTC_DS1307 rtc;

cattledog:
So what was this all about in the first posted sketch

Well look at me, it was my bad. There are so much RTC libraries and sensors that one can miss that and most people using them have the same problems.

Well sorry about that. Will remove anything now and go on without time records..

If you can't get the DS1302 to work, you can always use the Time Library (Time, by Michael Margolis available through the library manager) to set a time from Serial, and then use the internal clock of the Arduino to keep time. It may not be very accurate, and will loose time in a power cycle, but it may be the best you can do until you get an RTC. The DS3231 is the best choice.