SD card doesn't save data when connected to a 9V battery but works when plugged into the computer

Hello,
We are currently doing a project recording accelerometer data onto an SD card, this works fine when the Arduino is plugged the computer via the USB. However, when the Arduino is powered by a 9V battery no data is put onto the SD card.
It is seen that other people have ran into a similar problem before but none of those threads were resolved.
Below you can see the code we are using.
Any help would be appreciated
Here is a picture of our circuit with the battery plugged in.

#include "Arduino_BMI270_BMM150.h" 

#include <SD.h> 

#include <SPI.h> 

 

const int buttonPin = 7;  // Pin for button input 

const int ledPin = 9;    // Pin for LED (to indicate data collection) 

bool lastButtonState = HIGH;  // Previous button state (for debounce) 

unsigned long lastDebounceTime = 0;  // Last debounce time 

unsigned long debounceDelay = 50;  // Debounce delay 

 

// Number of readings and rate (can be adjusted) 

const int numberOfReadings = 100;  // 100 readings 

const float rateOfReadings = 10.0;  // 10 readings per second 

 

// SD card settings 

const int chipSelect = 10;  // Pin for CS (Chip Select) 

 

// File object for SD card 

File dataFile; 

 

void setup() { 

  Serial.begin(115200);  // Start serial communication 

  pinMode(buttonPin, INPUT_PULLUP);  // Set the button pin as input with internal pull-up resistor 

  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output 

 

  while (!Serial);  // Wait for serial port to open 

 

  if (!IMU.begin()) {  // Initialize IMU (accelerometer and magnetometer) 

    Serial.println("Failed to initialize IMU!"); 

    while (1);  // Halt if IMU initialization fails 

  } 

 

  // Initialize the SD card 

  Serial.println("Initializing SD card..."); 

if (!SD.begin(chipSelect)) { 

  Serial.println("SD card initialization failed!"); 

  while (1);  // Halt the program if SD initialization fails 

} else { 

  Serial.println("SD card initialized successfully!"); 

} 

  Serial.println("SD card initialized!"); 

 

  Serial.println("Ready to collect data! Press the button to start."); 

} 

 

void loop() { 

  int buttonState = digitalRead(buttonPin);  // Read button state 

 

  // Debounce logic to prevent multiple triggers on button press 

  if (buttonState != lastButtonState) { 

    lastDebounceTime = millis(); 

  } 

 

  if ((millis() - lastDebounceTime) > debounceDelay) { 

    if (buttonState == LOW) {  // LOW when the button is pressed 

      Serial.println("Button Pressed! Starting data collection..."); 

 

            // Turn on the LED to indicate data collection is starting 

      digitalWrite(ledPin, HIGH); 

       

      // Start data collection 

      collectData(); 

       

      // Turn off the LED after data collection completes 

      digitalWrite(ledPin, LOW); 

    } 

  } 

 

  lastButtonState = buttonState;  // Update last button state 

} 

 

// Function to collect sensor data 

void collectData() { 

  unsigned long dataT[numberOfReadings];  // Array to store time of readings 

  float dataX[numberOfReadings], dataY[numberOfReadings], dataZ[numberOfReadings];  // Arrays to store accelerometer data 

 

  // Calculate delay time between readings based on the specified rate 

  double delayTime = 1000.0 / rateOfReadings; 

 

  // Start time of data collection 

  unsigned long startTime = millis(); 

 

  // Open the file on the SD card 

  dataFile = SD.open("data.csv", FILE_WRITE); 

  if (dataFile) { 

    // Write the header to the CSV file 

    dataFile.println("Time(ms), X, Y, Z"); 

   

    // Collect data 

    for (int n = 0; n < numberOfReadings; n++) { 

      unsigned long elapsedTime = millis() - startTime;  // Calculate elapsed time 

 

      dataT[n] = elapsedTime; 

 

      // Read accelerometer data 

      float x, y, z; 

      IMU.readAcceleration(x, y, z);  // Read data from accelerometer 

      dataX[n] = x * 100;  // Scale the x-axis data 

      dataY[n] = y * 100;  // Scale the y-axis data 

      dataZ[n] = z * 100;  // Scale the z-axis data 

 

      // Write the data to the SD card 

      dataFile.print(dataT[n]);  // Time of reading 

      dataFile.print(","); 

      dataFile.print(dataX[n]);  // x-axis data 

      dataFile.print(","); 

      dataFile.print(dataY[n]);  // y-axis data 

      dataFile.print(","); 

      dataFile.println(dataZ[n]);  // z-axis data 

 

      // Wait for the next reading based on the rate 

      while (millis() - startTime < (n + 1) * delayTime) { 

        // Wait until it's time for the next reading 

      } 

    } 

 

    // Close the file after writing 

    dataFile.close(); 

    Serial.println("Data collection complete! Data saved to SD card."); 

  } else { 

    Serial.println("Error opening file on SD card!"); 

  } 

} 

I believe the SD card module you have need 5V not 3.3V.

We have just tested, the SD card initializes when connected to the 3.3V port and it doesn't work when it is connected the the 5V port.

Is that a Nano ESP32 board?

your code is waiting for serial connection.

1 Like

What a good spot! Don't know how that slipped through.
Thank you very much

You are welcome!

1 Like

Its a Nano sense BLE 33 REV 2

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