Setup keeps repeating

My setup loop keeps repeating. It worked with the DPS and MPU initialization code just fine. Then I added the code to initialize the SD card and its no longer working.


#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_DPS310.h>
#include <SD.h>
#include <SPI.h>

// Instatiate Class Objects
Adafruit_DPS310 dps;
Adafruit_MPU6050 mpu;
File myFile;

// Data Points [Alt, Ax, Ay, Az, Gx, Gy, Gz, Temp]
float curData[] = {0, 0, 0, 0, 0, 0, 0, 0};
float startAltitude = 0;
char fileName[] = "data.csv";
const int chipSelect = 10;

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);

  // Initialize SD Card
  Serial.println("Task: Initialize SD");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card missing or failure");
    while(1) yield();
  }
  Serial.println("SD OKAY!");
  delay(100);

  //Initialize DPS310 Barometer
  Serial.println("Task: Initialize DPS310");
  if (! dps.begin_I2C()) {
    Serial.println("Failed to find DPS");
    while (1) yield();
  }
  Serial.println("DPS OK!");
  delay(100);

  // Initialize MPU6050 Accelerometer
  Serial.println("Task: Initialize MPU6050");
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) yield();
  }
  Serial.println("MPU OK!");
  Serial.println();
  delay(100);

  //Initialize DPS310 Params
  dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
  dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
  //Initialize MPU6050 Params
  mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

  // Initialize Starting Altitude

  Serial.println("Task: Zeroing Altitude Data");
  zeroAltitude();
  Serial.print("Starting Altitude: ");
  Serial.print(startAltitude);
  Serial.println(" m");
  Serial.println();

  delay(3000);

  startingProcedure();

}

void loop() {
  collectData();
  printData();
}

void startingProcedure() {
  Serial.println("Enter d to start data collection");
  while (Serial.available() == 0) {}
  
    char charRead = tolower(Serial.read());

    switch (charRead) {

      case 'd':
        writeHeaders();
        collectData();
        SDWrite();
        printData();

        delay(10000);
    }
}

void zeroAltitude() {
  startAltitude = dps.readAltitude();
  return;
}

void collectData () {
  getAccelerometerData();
  getAltitude();
}

void getAltitude() {
  curData[0] = dps.readAltitude() - startAltitude;
  return;
}

void getAccelerometerData() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);
  curData[1] = a.acceleration.x;
  curData[2] = a.acceleration.y;
  curData[3] = a.acceleration.z;
  curData[4] = g.gyro.x;
  curData[5] = g.gyro.y;
  curData[6] = g.gyro.z;
  curData[7] = temp.temperature;
}

void printData () {
  Serial.print("Alt: ");
  Serial.print(curData[0]);
  Serial.print(" | ");
  Serial.print("Ax: ");
  Serial.print(curData[1]);
  Serial.print(" | ");
  Serial.print("Ay: ");
  Serial.print(curData[2]);
  Serial.print(" | ");
  Serial.print("Az: ");
  Serial.print(curData[3]);
  Serial.print(" | ");
  Serial.print("Gx: ");
  Serial.print(curData[4]);
  Serial.print(" | ");
  Serial.print("Gy: ");
  Serial.print(curData[5]);
  Serial.print(" | ");
  Serial.print("Gz: ");
  Serial.print(curData[6]);
  Serial.print(" | ");
  Serial.print("Temp: ");
  Serial.print(curData[7]);
  Serial.println(" *C");
}

void writeHeaders() {
  myFile = SD.open(fileName, FILE_WRITE);
  if(myFile) {
    myFile.print("Altitude [m]");
    myFile.print("Ax [G]");
    myFile.print("Ay [G]");
    myFile.print("Az [G]");
    myFile.print("Gx [deg/s]");
    myFile.print("Gy [deg/s]");
    myFile.print("Gz [deg/s]");
    myFile.println("Temp [degC]");
  }
}

void SDWrite() {
  myFile = SD.open(fileName, FILE_WRITE);
  if(myFile) {
    myFile.print(curData[0]);
    myFile.print(curData[1]);
    myFile.print(curData[2]);
    myFile.print(curData[3]);
    myFile.print(curData[4]);
    myFile.print(curData[5]);
    myFile.print(curData[6]);
    myFile.println(curData[7]);
  }
}
1 Like

Please show schematics and pictures. Maybe a power draw issue from the 5/3.3v pins?

Thank you for posting the code correctly.
I don't know much about the SD card reader/writer and MPU.
But here are my thoughts:
I don't know if it's just me or is that while loop missing some parenthesis.
@apf1979 What do you think about that?

My understanding is that if it's on a single line, the curly brackets aren't needed. The code makes it past that point so I don't think that's an issue.

1 Like

It also doesn't seem to get to this part.

Which might mean that the problem is before that.
Interesting that it does not print MPU Ok.


Here's a picture of the hardware. As soon as I comment out the initialization of the SD card, it gets through the setup function just fine. That includes leaving the SD card reader connected to the 3.3V rail.

I think The issue in your code seems to be with the writeHeaders() and SDWrite() functions. When you call these functions, you open the file using SD.open(fileName, FILE_WRITE), but you don't close the file after writing. As a result, the file remains open in subsequent calls, which can cause issues with writing data.

To fix the issue, you need to close the file after writing in both writeHeaders() and SDWrite() functions.

1 Like

They take a bit of power..
Curious, try initializing the SD last in setup and see if it fails there instead..
Looks like a reboot due to power consumption..

good luck.. ~q

@qubits-us I agree.

@edaanderson Do you have another power source you can power the peripherals from? Make sure to tie the grounds together if you do.

Screenshot 2023-05-18 101749
This is what I got running the SD initialization last.

interesting..
Now disconnect the power to SD card..
from Uno Specs..

DC Current for 3.3V Pin 50 mA

btw, nice clean breadboard!

~q

I connected another power supply to the 3.3V rail. Without the SD initialization, it works. With the SD, it doesn't. Not sure if this would affect anything but it consistently doesn't get to the "Serial.print("MPU Okay!") line when the SD is initialized.

I'll see if that has any effect but I'm skeptical because it doesn't seem to get anywhere near those lines before it fails.

Haha I hope you're not making fun of me. I've made prettier ones...

Lol, not at all, compared to what I normally see here, it's beautiful!

kind of sounds like some conflict going on??

~q

Sorry Krishna, this didn't seem to help my current issue but I'm sure that saved me another debugging crisis in the future so thank you for the help!

Single line does not matter.

What matters is the number of statements

The syntax is while (condition) statement
If you have only one statement attached to the while, then you don’t need the curly brackets but if you want to do many things within the while you need to group them together so that the compiler sees them as one statement. The curly braces are for that - they create a compound statement.

1 Like

Curious,
Both of those sensors are 3.3 and 5v tolerant..
Maybe try running the sensors off 5v instead of 3.3??
Is the SD card reader 3.3v only??

~q

I tried running just the sensors on 5V, and then everything on 5V (Someone in the comments of the SD reader says it doesn't work with 5V, hence me being safe and using 3.3 but it works just fine on 5). Still getting the same setup looping I was before. Sidenote: I put an external power supply in parallel with the arduino 5V pin. So that shouldn't be a problem.