Can anyone see a problem with my code, SD card wont initialize

#include <LiquidCrystal_I2C.h>
#include <Wire.h>;
#include <SPI.h>
#include <SD.h>
int analogpin = 0; // analog input
int potstorage = 1; //stores last value
int count = 0; //counts changes in value
long elapsedtime = 0; // time since start used for math
long time1 = 0; // used for timing loops
long time2 = 0;
long time3 = 0;
long time4 = 0;

File myFile; // helps set up SD

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);


void setup() {
  SD.begin();
  pinMode(A0, INPUT); //sets up analog pin as input
  lcd.init(); //used to initialise LCD
  lcd.backlight();
  
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE); // if the file opened okay, write to it-will create a new folder if one isnt there:
  
  if (myFile) //sends info to "test.txt"
  {
    myFile.println("NEW TEST");
    lcd.setCursor(0,0);
    lcd.print("SD initialised");
    delay(2000);
    lcd.clear();
    // close the file:
    myFile.close();
  } 
  else
  {
    lcd.setCursor(0,0);
    lcd.print("SD init err");
    delay(2000);
    lcd.clear();
  }

}

void loop() 
{
lcd.setCursor(0,0);
lcd.print("Total Cycles = ");
lcd.setCursor(0,1);
lcd.print(count);
time3 = millis();
time4 = millis();
while (time4 - time3 < 8000)
{
  time4 = millis();
  potcounter(); //checks for change in values
  sd(); //saves info to sd
}  
lcd.clear(); 
elapsedtime = millis() / 60000;
lcd.setCursor(0,0);      
lcd.print("Time Since Start");
lcd.setCursor(0,1);
lcd.print(elapsedtime);
lcd.setCursor(13,1);
lcd.print("min");
time3 = millis();
time4 = millis();
while (time4 - time3 < 8000)
{
  time4 = millis();
  potcounter(); //checks for change in values
  sd(); //saves info to sd
}  
lcd.clear();

}


void sd() //used to send info to SD every 30 seconds
{
  elapsedtime = millis()/1000;
  time2 = millis();
  if (time1 + 30000 < time2) // creates a loop to save time every 30 seconds
  {
    myFile = SD.open("test.txt", FILE_WRITE); //opens/creates new folder
    if (myFile) 
    {
    myFile.print("Test Elapsed Time in Seconds;"); // saves info
    myFile.println("Count");
    myFile.print(elapsedtime);
    myFile.print(";");
    myFile.println(count);
    // close the file:
    myFile.close(); // closes folder
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("sent to SD");
    } 
    else
    {
      lcd.clear();
      lcd.print("SD error");
    }
  time1 = millis();
  }
}

void potcounter() //used to count change in values
  
 {
analogpin = analogRead(A0);
if (potstorage == 0 & analogpin > 600)
    {
        count = count + 1;
    }
analogpin = analogRead(A0);
if (analogpin > 600)
  {
      potstorage = 1;
  }
else
  {
    potstorage = 0;  
  }
delay(10);
}

When I use this code that I used for testing then it initializes fine and I can save the data with the same wiring set up and LCD still attached


#include <SPI.h>
#include <SD.h>
long elapsedtime = 0; // time since start
long time1 = 0;
long time2 = 0;

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  delay(1000);

  Serial.print("Initializing SD card...");
  SD.begin();
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("NEW TEST");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }



} 

void loop() 
{
save(); // calls save function
}


void save() // save function

{
  elapsedtime = millis()/1000;
  time2 = millis();
  if (time1 + 5000 < time2) // creates a loop to save time every 5 seconds
  {
    myFile = SD.open("test.txt", FILE_WRITE); //opens/creates new folder
    if (myFile) 
    {
    myFile.print("time elapsed in Seconds = "); // saves info
    myFile.println(elapsedtime);
    Serial.print("time elapsed = "); // sends info to serial
    Serial.println(elapsedtime);
    // close the file:
    myFile.close(); // closes folder
    } 
    else
    {
      Serial.println("error");
    }
  time1 = millis();
  }

}

Have you tried it with one of the example sketches that comes with the library? With those you know the code is good.....

did that when I was setting up and it worked. The second code that I posted is based on the example set and that works as well. Are there any other considerations to running an LCD and an SD at the same time that I might be missing?

What do you see in the serial monitor / lcd with the first code?
What’s your arduino?
How are things wired and powered?

Our messages crossed in the ether...

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }

SD.begin(4) seems to be the only difference... did you try that bit of code in your sketch?

Chip select pin maybe?

  bool SDClass::begin(uint8_t csPin) {

no serial monitor with the first code, when I initially had that in there my LCD would light up and nothing else would happen

the LCD will display "SD init err" on start up and then "SD error" when it tries to save later on in the code, other then that the counting and time functions work fine.

powering from my computers USB port. 5V pin & ground to breadboard rails that power the devices

Arduino UNO R3

SD device
3.3V-3.3V pin
5V-5V rail
CS-4
MOSI-11
SCK-13
MISO-12
GND-GND rail

LCD device uses I2C
GND-GND rail
VCC-5V rail
SDA-A4
SCL-A5

See post #7

I had

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }

There initially before I removed all the serial monitor related code when that started playing up.

I'm not sure what you meant by

bool SDClass::begin(uint8_t csPin) {

do you reckon adding

SD.begin(4);

might help?

new set up seems to work

void setup() {
  SD.begin();
  SD.begin(4);
  pinMode(A0, INPUT); //sets up analog pin as input
  lcd.init(); //used to initialise LCD
  lcd.backlight();
  
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE); // if the file opened okay, write to it-will create a new folder if one isnt there:
  
  if (myFile) //sends info to "test.txt"
  {
    myFile.println("NEW TEST");
    lcd.setCursor(0,0);
    lcd.print("SD initialised");
    delay(2000);
    lcd.clear();
    // close the file:
    myFile.close();
  } 
  else
  {
    lcd.setCursor(0,0);
    lcd.print("SD init err");
    delay(2000);
    lcd.clear();
  }

}

added

SD.begin(4);

can you explain why doing this worked?

Because when you initialise the card with "begin" you need to tell it which Arduino pin is the Chip Select pin... this is used by the SPI communication.

The code you copied is poor... it is calling begin twice.. the first one won't initialise the card.

You can remove the SD.begin() statememt.

This code is from the SD library file... sometimes helps to have a look at the library to understand what it is expecting.

it is very very unlikely that printing to serial interferes with other code making the other code malfunction.

The only exception is a loop that does create the step-signal for driving stepper-motors. Executing Serial.print needs additional time that might not be available for a loop that has to bitbang LOW-HIGH an IO-pin 10000 times per second.

for anything else
communicating via SPI-bus (like SD-card), communicating via I²C-bus, onewire-bus etc.
printing to serial works always great.

You should always use printing to the serial monitor for debugging. You will find the bugs much faster using serial.print than with anything else and it is always there and independent of having an LCD in a project.

best regards Stefan

Can you post a schematic as you have it wired showing all connections including hardware, power and ground. Frizzy pictures are next to useless. Also post links to technical data on the hardware, azon links are just sales info and not much help.

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