Issue with SD card not logging data

Hey guys, I am working on a frequency counter that measures about 4 fans at once. I have gotten it to print fine using Serial.print but I am having issues when I want to log the same data to an SD card. I suspect that the interrupts are what is causing the SD card error and tried using interrupts() and nointerrupts() but I still get the error. If anyone has suggestions I would appreciate it.

[code]
#include "FS.h"
#include "SD.h"
#include "SPI.h"
const byte        interruptPin = 33;              // Assign the interrupt pin
volatile uint64_t StartValue;                     // First interrupt value
volatile uint64_t PeriodCount;                    // period in counts of 0.000001 of a second
float             Freg;                           // frequency
char              str[21];                        // for printing uint64_t values
bool              flag = true;
volatile uint64_t count;
hw_timer_t * timer = NULL;                        // pointer to a variable of type hw_timer_t
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;  // synchs between maon cose and interrupt?
String Data;


// Digital Event Interrupt
// Enters on falling edge in this example
//=======================================
void IRAM_ATTR handleInterrupt()
{
  portENTER_CRITICAL_ISR(&mux);
  if (flag == true) {
    uint64_t TempVal = timerRead(timer);        // value of timer at interrupt
    PeriodCount = TempVal - StartValue;         // period count between rising edges in 0.000001 of a second
    StartValue = TempVal;                       // puts latest reading as start for next calculation
    count++;
  }
  portEXIT_CRITICAL_ISR(&mux);
  //detachInterrupt(digitalPinToInterrupt(interruptPin));
}

void IRAM_ATTR onTime()
{
  portENTER_CRITICAL_ISR(&mux);
  flag = true;
  count = 0;
  portEXIT_CRITICAL_ISR(&mux);
}




// Converts unit64_t to char for printing
// Serial.println(uintToStr( num, str ));
//================================================
char * uintToStr( const uint64_t num, char *str )
{
  uint8_t i = 0;
  uint64_t n = num;
  do
    i++;
  while ( n /= 10 );

  str[i] = '\0';
  n = num;

  do
    str[--i] = ( n % 10 ) + '0';
  while ( n /= 10 );

  return str;
}

// SetUp
//======================================
void setup()
{
  Serial.begin(115200);
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  writeFile(SD, "/test.txt", "Hello ");
  timer = timerBegin(2, 80, true);                                                // this returns a pointer to the hw_timer_t global variable
  // 0 = first timer
  // 80 is prescaler so 80MHZ divided by 80 = 1MHZ signal ie 0.000001 of a second
  // true - counts up
  timerStart(timer);  // starts the timer
  timerAttachInterrupt(timer, &onTime, true);
  timerAlarmWrite(timer, 5000000, true);
  timerAlarmEnable(timer);
  pinMode(interruptPin, INPUT_PULLUP);                                            // sets pin high
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING); // attaches pin to interrupt on Falling Edge
}

void loop()
{
  portENTER_CRITICAL(&mux);
  if (flag == true && PeriodCount != 0 && count == 2) {
    Freg = 1000000.00 / PeriodCount;                    // PeriodCount in 0.000001 of a second
    Serial.print("Frequency   "); Serial.println(Freg);
    Data = "Frequency: " + String(Freg); + "\n";
    appendFile(SD, "/test.txt", Data.c_str());
    delay(200);
  }
  portEXIT_CRITICAL(&mux);
}
[/code]

what's your arduino?

My bad, Its actually an esp32 but I am sure the same concept applies.

appendFile and writeFile are custom functions you wrote or took from somewhere ?

what error do you get?

append and write are functions from the SD example sketch for the esp32. I believe they interact with the FS.h library. The esp32 keeps rebooting before it can store anything to the SD card.

And writing to SD works fine with those functions if you don’t have the interrupts going on?

Yes they work fine without the interrupts.

So if you detach the interrupt during the SD access, would that work?

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