I want to get data using Arduino Uno, MPU6050 and SD card

I want to insert a 9V battery into an arduino with a DC plug and run a compiled program.
I am making a program called MPU6050 with an accelerometer and SD module, and I want to write the accelerometer values that the MPU6050 expels to the SD card. However, I don't know how to compile and run the program when it is powered from the battery.

-Situation.
When powered from a PC, the data output and writing are working well. When the battery is connected and powered, I don't think it is connected to the PC, but how do I do the compilation etc.? The circuit diagram is as shown in the reference diagram.

(mpu6050) to (Arduino)
VCC to5V
GND to GND
SCL to A5
SDA to A4
(SD card adapter) to (Arduino)
CS to 4
SCK to 13
MOSI to 11
MISO to 12
VCC to 5V
GND to GND

The program is shown in the below. This program runs properly via a PC.

#include "I2Cdev.h"
#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <Wire.h>
#include "MPU6050.h"
MPU6050 accelgyro;
//#include <time.h>
unsigned long time;


int16_t ax,ay,az,gx,gy,gz,temperature;


File myFile;

void setup() {
  Serial.begin(9600);
  Wire.setClock(400000);
  Wire.begin();

  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1C);
  Wire.write(0x10);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1B);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x1A);
  Wire.write(0x05);
  Wire.endTransmission();

  // 初期化
  accelgyro.initialize();
  delay(10);

  // 計測範囲を2000 deg/secに設定、16.4 LSB/deg/s
  accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
  delay(10);
  
  accelgyro.CalibrateGyro();
  accelgyro.CalibrateAccel();
  // Open serial communications and wait for port to open:
  //.    Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.csv");
  }
}

void loop() {
  Wire.beginTransmission(0x68);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 14);
  while (Wire.available() < 14);
  ax = Wire.read() << 8 | Wire.read();
  ay = Wire.read() << 8 | Wire.read();
  az = Wire.read() << 8 | Wire.read();
  gx = Wire.read() << 8 | Wire.read();
  gy = Wire.read() << 8 | Wire.read();
  gz = Wire.read() << 8 | Wire.read();
  temperature = Wire.read() << 8 | Wire.read();

  time = millis();
  //Serial.println(time);
 File dataFile;
 dataFile = SD.open("AcGy.csv", FILE_WRITE);

  Serial.print(time);Serial.print(",");
  Serial.print(ax/16384.0); Serial.print(",");
  Serial.print(ay/16384.0); Serial.print(",");
  Serial.print(az/16384.0); Serial.print(",");
  Serial.print(gx/131.0); Serial.print(",");
  Serial.print(gy/131.0); Serial.print(",");
  Serial.println(gz/131.0);

  dataFile.print(time); dataFile.print(",");
  dataFile.print(ax/16384.0); dataFile.print(",");
  dataFile.print(ay/16384.0); dataFile.print(",");
  dataFile.print(az/16384.0); dataFile.print(",");
  dataFile.print(gx/131.0); dataFile.print(",");
  dataFile.print(gy/131.0); dataFile.print(",");
  dataFile.println(gz/131.0);

  delay(10);
  dataFile.close();
  //exit(0);
  }



I moved your topic to an appropriate forum category @haya81778.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

You don't have to. You compile and upload the code while the Arduino is connected to the PC; the upload will store the data in non-volatile memory in the Arduino and it will be "remembered".

After disconnecting the board from the PC and connecting it again, the program that was uploaded will still work.

Which battery? PP3 type as in the image below?
image

If yes, those things can't power an Arduino.

I use this I show in the below URL. I probably use PP3 type.

Please tell me what battery works well.

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