LoginSignup
2
0

今回はESP32-S3からSDカードを操作したので記事にしました。
image.png

ハードウェア

1、ESP32-S3-DevKitC
https://akizukidenshi.com/catalog/g/g117073/
2、マイクロSDカードスロットDIP化キット
https://akizukidenshi.com/catalog/g/g105488/

配線図

image.png

ソフトウェア

今回はmgo-tec電子工作様の記事を参考にさせていただきました。
https://www.mgo-tec.com/blog-entry-esp32-wroom-micro-sdhc-01.html

#include <SD.h>
 
const uint8_t cs_SD = 10; 
const char* fname1 ="/test1.txt";

File f1;
 
void setup() {
  Serial.begin(115200);
  SD.begin(cs_SD, SPI, 24000000, "/sd");
 
  Serial.println("\r\n File Write");
  f1 = SD.open(fname1, FILE_WRITE);
  serial_print_Fwrite(f1, 1, "このファイルはテスト1\r\n");
  f1.close();
  delay(1);
   
  Serial.println("File Read");
  f1 = SD.open(fname1, FILE_READ);

  serial_print_Fread(f1, 1);
 
  f1.close();
   
/*  
  Serial.println("\r\n------------ファイル削除");
  Delete_File(fname1);
*/
}
 
void serial_print_Fwrite(File ff, byte num, const char *message){
  if(ff.print(message)){
    Serial.printf("f%d Message wrote. Good!\r\n", num);
  }else{
    Serial.printf("f%d write failed\r\n", num);
  }
}
 
void serial_print_Fread(File ff, byte num){
  size_t len = ff.size();
  uint8_t buf[256];
   
  if (ff.read(buf, len)) {
    Serial.printf("f%d File found OK!!\r\n", num);
    for(int i=0; i<len; i++){
      Serial.write(buf[i]);
    }
  }else{
    Serial.printf("f%d File not found\r\n", num);
  }
}
 
void Delete_File(const char *fname){
  if(SD.remove(fname)){
      Serial.printf("%s File deleted\r\n", fname);
  } else {
      Serial.printf("%s Delete failed\r\n", fname);
  }
}
 
void loop() {
}

プログラムの内容としては、
serial_print_Fwrite(f1, 1, "このファイルはテスト1\r\n");
の部分でファイルに書き込みを行い、
serial_print_Fread(f1, 1);
の部分でファイルの内容を読み込んでいます。

最後に

ESP32-S3では少しピンアサインがわかりにくい部分があったので
多少配線に時間がかかりました。
みなさんもこの記事を参考にSDカードを操作してみてください。

ESP32やLPWA(プライベートLoRa)などの開発を行っております。
IoT製品の開発も行っていますのでなにかありましたら
お気軽にご相談ください。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0