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++;
}