Go Down

Topic: I cant wride data in my SDcard use arduino and ESP32 (Read 969 times) previous topic - next topic


I am a beginner
I am reading the status of the eight pins of ESP32 and transmitting it with Bluetooth and storing it with SD card.
My program is compiled without errors, but I can't write my data [val] to the SD card.
How can I modify it?
so that,I can write the data【val】in the SD card

Code: [Select]

#include <SD.h>
#include <SPI.h>
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
int key;

#define SD_CS 34

void setup()

 if(!SD.begin(SD_CS)) {
   Serial.println("Card Mount Failed");
 uint8_t cardType = SD.cardType();
 if(cardType == CARD_NONE) {
   Serial.println("No SD card attached");
 Serial.println("Initializing SD card...");
 if (!SD.begin(SD_CS)) {
   Serial.println("ERROR - SD card initialization failed!");
   return;    // init failed

 // If the data.txt file doesn't exist
 // Create a file on the SD card and write the data labels
 File file = SD.open("data.txt");
 if(!file) {
   Serial.println("File doens't exist");
   Serial.println("Creating file...");
   writeFile(SD, "data.txt", "data \r\n");
 else {
   Serial.println("File already exists");  


void loop()

 int value =0;
 int D0 = digitalRead(2);
 int D1 = digitalRead(15);
 int D2 = digitalRead(4);
 int D3 = digitalRead(16);
 int D4 = digitalRead(17);
 int D5 = digitalRead(5);
 int D6 = digitalRead(18);
 int D7 = digitalRead(19);
 char val[256];
 snprintf(val , sizeof(val) , "%d %d %d %d %d %d %d %d",

 byte Date[8];
 Date[0] = D0;
 Date[1] = D1;
 Date[2] = D2;
 Date[3] = D3;
 Date[4] = D4;
 Date[5] = D5;
 Date[6] = D6;
 Date[7] = D7;

 if(key == 65)
   for(int i=0;i<8;i++)
   key = 0;
 appendFile(SD, "data.txt", val);

void readFromAPP()
 while (SerialBT.available() > 0)
  key = SerialBT.read();


void appendFile(fs::FS &fs, const char * path, const char * message) {
 Serial.printf("Appending to file: %s\n", path);

 File file = fs.open(path, FILE_APPEND);
 if(!file) {
   Serial.println("Failed to open file for appending");
 if(file.print(message)) {
   Serial.println("Message appended");
 } else {
   Serial.println("Append failed");

void writeFile(fs::FS &fs, const char * path, const char * message) {
 Serial.printf("Writing file: %s\n", path);

 File file = fs.open(path, FILE_WRITE);
 if(!file) {
   Serial.println("Failed to open file for writing");
 if(file.print(message)) {
   Serial.println("File written");
 } else {
   Serial.println("Write failed");


Hopefully, you can see now why we ask you to use code tags when posting code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up