LCD shows unwanted text like DDDDDDDDDDDDDDDD

I have a draft version of a voltage logger with and SD card writer, which shows it's sensor values also on an LCD display.
When I write the first text "Initializing SD" to the LCD, it appears as it should. But when I then write "SD Card ok", it shows
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD

All other LCD sample programs work fine so I think the connections are ok. And the rest of the program also works as it should.

Any idea what I'm doing wrong?

// Voltage Logger

// include the library code:
#include <LiquidCrystal.h>
#include <RTClib.h>
#include <SD.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int chipSelect = 10, count = 0;
String dataString = "", fileName = "", dtString = "";

float voltage = 0.0;

RTC_DS1307 rtc;

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

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

  //rtc.adjust(DateTime(2023,3,31,18,43,0));
 
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Initializing SD ");

  Serial.println("\nInitializing SD Card");
  delay(1000);

  while (!SD.begin(chipSelect)) {
    Serial.println("SD Card failed");
    lcd.setCursor(0, 1);
    lcd.print("SD Card Failed  ");
  }
  Serial.println("SD Card initialized");
  lcd.setCursor(0, 1);
  lcd.print("SD Card ok      ");
  delay(1000);
}

void loop() {
  DateTime now = rtc.now();
  dtString = String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());

  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023);
  Serial.print(dtString); Serial.print(" - "); Serial.print(String(voltage, 2)); Serial.println(" V");

  lcd.setCursor(0, 0);
  lcd.print(dtString);
  lcd.setCursor(0, 1);
  lcd.print(String(voltage, 2));

  fileName = "datalog.csv";
  File dataFile = SD.open(fileName, FILE_WRITE);
  if (count == 0)
    dataFile.println("Time,Voltage");
  dataString = dtString + "," + String(voltage, 2);
  dataFile.println(dataString);
  dataFile.close();
  delay(1000);
  count++;
}

Are you sharing pins between the SD card and LCD?

SPI library is included by SD.h, so SPI is being used. Pin 11 is MOSI an pin 12 is MISO.

I'm using a Velleman shield on top of an Arduino UNO, so indeed maybe using the same pins. I'll disable the SD functions and try again.

Yes, now it's working fine. So SD card and LCD cannot be combined? Or should I use other pins?

The SD library has a way to specify the pins for CS, SCK, MOSI and MISO by adding parameters to the begin function.

boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, int8_t mosi = -1, int8_t miso = -1, int8_t sck = -1);

May be a way to go since changing the hardwired LCD shield pins would be difficult.

I changed the pins like LiquidCrystal lcd(8, 7, 5, 6, 3, 2); and rewired accordingly and it works fine.

Thanks a lot all!

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