Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15454

Arduino sd card initialization failed

$
0
0
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_LSM9DS1.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>


File dataFile;

float temperatureOffset = -30;
const int chipSelect = 4;


void setup() {
  Serial.begin(9600);
  while (!Serial) 


  if (!HTS.begin()) {
    Serial.println("온도 센서 초기화 실패");
    while (1);
  }


  if (!BARO.begin()) {
    Serial.println("대기압 센서 초기화 실패");
    while (1);
  }


  if (!SD.begin(chipSelect)) {
    Serial.println("SD 카드 초기화 실패");
    while (1);
  }

  if (!IMU.begin()) {
    Serial.println("IMU(가속도) 초기화 실패");
    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in g's");
  Serial.println("X\tY\tZ");
}


void loop() {
  float temperature = HTS.readTemperature();
  float pressure = BARO.readPressure();
  float x, y, z;

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);

    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');
    Serial.println(z);
  }

  // print the sensor value
  Serial.print("대기압 = ");
  Serial.print(pressure * 10);
  Serial.println(" hPa");


  // print each of the sensor values
  Serial.print("온도 = ");
  Serial.print(temperature + temperatureOffset);
  Serial.println(" °C");


  // print an empty line
  Serial.println();


  saveDataToSD(pressure, temperature, x, y, z);


  // wait 1 second to print again
  delay(1000);
}


void saveDataToSD(float temperature, float pressure, float x, float y, float z) {
  File dataFile = SD.open("data.csv", FILE_WRITE);


  if (dataFile) {
    // 온도 데이터를 A 열에 추가
    dataFile.print(temperature + temperatureOffset);
    dataFile.print(' °C');
    dataFile.print(",");


    // 압력 데이터를 B 열에 추가
    dataFile.print(pressure * 10);
    dataFile.print(' hPa');
    dataFile.print(",");

    // 가속도 x, y, z C, D, E 열에 추가
    dataFile.print(x);
    dataFile.print(",");
    dataFile.print(y);
    dataFile.print(",");
    dataFile.print(z);


    // 파일 닫기
    dataFile.close();


    Serial.println("데이터 저장 완료");
  } else {
    Serial.println("파일 열기 실패");
  }
}

I want to store data measured by the built-in sensor of the Arduino Nano 33 BLE SENSE on a sd card. However, the initialization of the sd card keeps failing.

< Hardware >

MOSI: 11 pins, MISO: 12 pins, SCK: 13 pins, CS: 4 pins, 5V: 5V, GND: GND

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15454

Trending Articles