I want to use an Arduino Uno, MPU6050 and SD card to write data from the MPU6050 to the SD card, but the program does not properly works. Please tell me where I need to fix it. The program to write to the SD card itself and the program to output the data from the MPU6050 work fine, but I cannot successfully write the data output from the MPU6050 to the SD card. I bought https://amzn.asia/d/45Q4yul
https://amzn.asia/d/hBRhdDV
https://amzn.asia/d/97xE9zO
via Amazon.
I connected
(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
as shown in the figure below.
Program are
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include "I2Cdev.h"
#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <Wire.h>
#include "MPU6050.h"
MPU6050 accelgyro;
#include <time.h>
float pretime;
#define k 0.8
float omegaY;
float degY2;
float degY3;
int16_t ax,ay,az;
int16_t gx,gy,gz;
File myFile;
void setup() {
Wire.begin();
Serial.begin(9600);
// 初期化
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.txt");
}
}
void loop() {
//File yourFile;
//yourFile = SD.open("value.txt", FILE_WRITE);
File dataFile;
dataFile = SD.open("MPU6050.txt", FILE_WRITE);
//int t,i;
//for(i=0;i<10000;i++){
//int t = t + 1;
// dataFile.println(t);
//}
//dataFile.close();
//exit(0);
// 時間を計っとく
static float timer = 0;
// 各軸加速度と角速度を取得
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
//X、Y軸まわりの角度を計算
float degX = atan2(ay, az) * RAD_TO_DEG;
float degY = atan2(ax, az) * RAD_TO_DEG;
//float degZ = atan2(ay, az) * RAD_TO_DEG;z
//float omegaX;
float omegaY;
//float omegaZ;
float dt=(micros()-pretime)/1000000;
pretime = micros();
//omegaX = float(gx)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
omegaY = float(gy)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
//omegaZ = float(gz)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
//degX2 += omegaX*dt;//ジャイロセンサ
//degX3 = k*(degX3+omegaX*dt)+(1-k)*degX;//相補フィルタ
degY2 += omegaY*dt;//ジャイロセンサ
degY3 = k*(degY3+omegaY*dt)+(1-k)*degY;//相補フィルタ
//degZ2 += omegaZ*dt;//ジャイロセンサ
//degZ3 = k*(degZ3+omegaZ*dt)+(1-k)*deg;//相補フィルタ
//c.print(degX); Serial.print(", ");
//Serial.print(degX2);Serial.print(", ");
//Serial.println(degX3);
Serial.print(degY); Serial.print(", ");
Serial.print(degY2);Serial.print(", ");
Serial.println(degY3);
dataFile.print(degY); dataFile.print(", ");
dataFile.print(degY2);dataFile.print(", ");
dataFile.println(degY3);
delay(3000);
//Serial.print(degZ); Serial.print(", ");
//Serial.print(degZ2);Serial.print(", ");
//Serial.println(degZ3);
dataFile.close();
exit(0);
}
This does not work properly.
This program indicate angle via accelerometer, gyrometer and complementary filter.
The results are shown in the diagram below.
Continuous data export is not possible and output stops halfway through.
For reference, here is a program that works well. These are the programs for writing data on the MPU6050 and writing to the SD card.
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 accelgyro;
float pretime;
#define k 0.8
float omegaY;
float degY2;
float degY3;
int16_t ax,ay,az;
int16_t gx,gy,gz;
void setup() {
Wire.begin();
Serial.begin(9600);
// 初期化
accelgyro.initialize();
delay(10);
// 計測範囲を2000 deg/secに設定、16.4 LSB/deg/s
accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
delay(10);
accelgyro.CalibrateGyro();
accelgyro.CalibrateAccel();
}
void loop() {
// 時間を計っとく
static float timer = 0;
// 各軸加速度と角速度を取得
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
//X、Y軸まわりの角度を計算
float degX = atan2(ay, az) * RAD_TO_DEG;
float degY = atan2(ax, az) * RAD_TO_DEG;
//float degZ = atan2(ay, az) * RAD_TO_DEG;z
//float omegaX;
float omegaY;
//float omegaZ;
float dt=(micros()-pretime)/1000000;
pretime = micros();
//omegaX = float(gx)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
omegaY = float(gy)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
//omegaZ = float(gz)/16.4;//omega[deg/s]=gz[LSB]/16.4[LSB/deg/s]
//degX2 += omegaX*dt;//ジャイロセンサ
//degX3 = k*(degX3+omegaX*dt)+(1-k)*degX;//相補フィルタ
degY2 += omegaY*dt;//ジャイロセンサ
degY3 = k*(degY3+omegaY*dt)+(1-k)*degY;//相補フィルタ
//degZ2 += omegaZ*dt;//ジャイロセンサ
//degZ3 = k*(degZ3+omegaZ*dt)+(1-k)*deg;//相補フィルタ
//Serial.print(degX); Serial.print(", ");
//Serial.print(degX2);Serial.print(", ");
//Serial.println(degX3);
Serial.print(degY); Serial.print(", ");
Serial.print(degY2);Serial.print(", ");
Serial.println(degY3);
//delay(1000);
//Serial.print(degZ); Serial.print(", ");
//Serial.print(degZ2);Serial.print(", ");
//Serial.println(degZ3);
}
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include "I2Cdev.h"
#include <SPI.h>
#include <SD.h>
#include <stdio.h>
#include <Wire.h>
#include "MPU6050.h"
MPU6050 accelgyro;
#include <time.h>
File myFile;
void setup() {
Wire.begin();
Serial.begin(9600);
// 初期化
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.txt");
}
}
void loop() {
//File yourFile;
//yourFile = SD.open("value.txt", FILE_WRITE);
File dataFile;
dataFile = SD.open("AAA.txt", FILE_WRITE);
int t,i;
for(i=0;i<10000;i++){
int t = t + 1;
dataFile.println(t);
}
dataFile.close();
exit(0);
}
I am Japanese, so please tell me in simple English.