ESP32-cam SD card problem with saving data

I am using esp32-cam and would like to save sensor data to SD card. Unfortunately, when I send data command by file.println in void loop, the file is empty or the file only contains data after one pass of the loop.

1 Like

Probably a problem with your code that you forgot to post.

#include "SD_MMC.h"
#include "SPI.h"

void setup(){ 
  Serial.begin(115200);    
}

void loop(){
   
     if(!SD_MMC.begin()){
        Serial.println("Failed to mount card");
        return;
    }
 
    File file = SD_MMC.open("/test.txt", FILE_WRITE);
     
    if(!file){
        Serial.println("Opening file failed");
        return;
   }
     
    file.println("Test");
    delay(1000);
    } 

You are doing this every loop. You only need to do it once... put it in setup();

I did it. The same problem file is empty.

You need to close the file as well...

    file.close();

#include "SD_MMC.h"
#include "SPI.h"

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

  if (!SD_MMC.begin())
  {
    Serial.println("Failed to mount card");
    return;
  }

  File file = SD_MMC.open("/test.txt", FILE_WRITE);

  if (!file)
  {
    Serial.println("Opening file failed");
    return;
  }

  for (uint8_t x = 0; x < 10; x++)
  {
    file.println("Test");
  }

  file.close();
}


void loop()
{}

Your code working, but this code isn't working in void loop. I want measure data sensor.

Of course that code will work in loop...

Just remember...

You open the file ONCE.
You write your sensor data... as many times as you want.
You close the file ONCE.

Ok, understand now. I'II try. Tkanks for helping.

Hi, I too am trying to log data to the sd card on an esp32-cam .. could you share your code that works? It would be extremely helpful to me

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