Got an Esp32 DevKit C V2 (ESP32 NodeMCU Module WLAN WiFi Dev Kit C Development Board mit CP2102 – AZ-Delivery) with a bunch of sensors (Luxmeter, DHT11, BMP180, NTC) an RTC and an LCD.
Now i want to create the ability to store some Values on an SD-Card. So i got this 5V SPI SD-Card Reader.
Since I have 5V and 3.3V in my Setup there shouldnt be a Problem.
This here is the Setup of the whole Thing
My Code is a bit split up to keep it modular, but here is the explanation.
This is my Setup File. I cleared all the excess Stuff like the LCD or the sensors to keep it simple.
const int SD_CardSelect = 5; // CS-Pin of the SD-Card
void setup()
{
// Setup Serial Connection
Serial.begin(115200);
Serial.println("\n----------------------------------------------------------\n"); // Make it easier to see where the current Script starts
Serial.println("Starting Setup, First LCD");
// Setting up all the other Things
// Setup SD-Card
SD_Setup(SD_CardSelect);
//Testing
SD_WriteFile("Data.txt", "TestData");
}
To keep it modular I have the SD-Stuff in another File. The baseidea is, that in the Setup it simply connects to the SD-Card. This part works, since it returns "Connected to SD" and prints me the Cardtype 2. To keep the File open for as short as possible, I open it, write to it and close it again. At least thats the Plan. In the SD_WriteFile I can see, that the File does not exist (Even if I create it with my PC ???). "OF1" and "OF2" gets printed, but the File won't open, which is why it returns here.
#include <SD.h>
File thisFile;
bool SD_Setup(int sd_Connection)
{
if(SD.begin(sd_Connection))
{
Serial.println("Connected to SD");
Serial.println("Card Type: " + (String)SD.cardType()); // Prints 2
return true;
}
else{
Serial.println("Connection to SD failed. \n- Card Inserted?");
return false;
}
}
bool SD_WriteFile(String file, String data)
{
Serial.println("File exists: " + (String)SD.exists(file));
// Open File for Write
Serial.println("OF 1");
thisFile = SD.open(file,FILE_WRITE, true);
Serial.println("OF 2");
if(!thisFile)
{
Serial.println("Can't open File: " + file);
return false; // Return when File not opened
}
// Write to File
Serial.println("WF 1");
thisFile.print(data);
Serial.println("WF 2");
thisFile.close();
Serial.println("CF");
return true;
}
Any Ideas why this wont work? Using the Example Sketches is kinda harsh since I have no idea, since running the SD_Test Example, it wrote and changed everything correctly.
EDIT: Simulating with Wokwi shows the same error. EPS32-SD_Card.ino - Wokwi Arduino and ESP32 Simulator