Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Adafruit HUZZAH32 - ESP32 Feather with SD card module
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit HUZZAH32 - ESP32 Feather with SD card module

by Maximumotion on Thu Nov 25, 2021 4:56 am

Hello,

I am trying to connect SD card module to Adafruit HUZZAH32 - ESP32 Feather and am getting "Initialization failed!" message.
I connected the wires to the specified pins.
As for the code, I am just testing it with a standard Arduino example library code for SD card.
Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

void setup() {
  // 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...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

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

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}


As for CS connection I used pin 4 or in this particular board it needs to be used on a different pin?

thank you,

Maksym
Attachments
20211125_005102.jpg
20211125_005102.jpg (902 KiB) Viewed 175 times

Maximumotion
 
Posts: 4
Joined: Sun May 24, 2020 10:13 pm

Re: Adafruit HUZZAH32 - ESP32 Feather with SD card module

by dastels on Thu Nov 25, 2021 11:57 am

Um.. do you mean a "Card failed, or not present" message?

The connections look right. I'd check the soldering. This could easily be a bad connection.

But also, do you have a schematic for the board. It doesn't look like an Adafruit product, but based on what I see I think it has a 3.3v regulator and level shifters on board, which would mean that Vcc should be 5v rather than 3.3v. I would verify that using the schematic or instructions.

Dave

dastels
 
Posts: 7962
Joined: Tue Oct 20, 2015 3:22 pm

Re: Adafruit HUZZAH32 - ESP32 Feather with SD card module

by Maximumotion on Thu Nov 25, 2021 10:33 pm

Thanks for reply.
If I use ESP32 board, the error message says - "flash reader err, 1000" and f I use other Arduino SD based libraries, it also says "open failed".
The board that I am using is an actual Adafruit HUZZAH32 - ESP32 Feather https://learn.adafruit.com/adafruit-huzzah32-esp32-feather
I tried Arduino SD library with Arduino boards and it's all working well but it doesn't work for ESP32 based board.

Changes that I made:
I used another SD card module for 3.3V.
As for SD card library it seems to be working well with the one that is for ESP32https://randomnerdtutorials.com/esp32-microsd-card-arduino/
[/code]
I simplified it a bit, sharing below with those who will need it:

Code: Select all | TOGGLE FULL SIZE
#include "FS.h"
#include "SD.h"
#include <SPI.h>

#define SD_CS 21

String dataMessage;

long randNum;
long randNum1;
long randNum2;

void setup() {
 
  Serial.begin(115200);

  SD.begin(SD_CS);
  if (!SD.begin(SD_CS)) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }
  Serial.println("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;    // init failed
  }

  File file = SD.open("/data.txt");
  if (!file) {
    Serial.println("File doens't exist");
    Serial.println("Creating file...");
    writeFile(SD, "/data.txt", "Reading ID, Date, Hour, Temperature \r\n");
  }
  else {
    Serial.println("File already exists");
  }
  file.close();



}

void loop() {
 
  getReadings();
  logSDCard();
}

// Function to get temperature
void getReadings() {
  randNum = random(10, 25);
  randNum1 = random(30, 35);
  randNum2 = random(40, 45);

  Serial.print("Number1: ");
  Serial.print(randNum);
  Serial.print("Number2: ");
  Serial.print(randNum1);
  Serial.print("Number3: ");
  Serial.println(randNum2);
}


// Write the sensor readings on the SD card
void logSDCard() {
  dataMessage = String(randNum) + ", " + String(randNum) + ", " +
                String(randNum) + "\r\n";
  Serial.print("Save data: ");
  Serial.println(dataMessage);
  appendFile(SD, "/data.txt", dataMessage.c_str());
}

// Write to the SD card (DON'T MODIFY THIS FUNCTION)
void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}


So my guess is that in the code, all the SPI pins need to be specified specifically for the pins of the ESP32 board as just specifying CS pin doesn't work.
I tried to change it but couldn't do it yet. So I used the code above which works well.
But I am still interested to find out how to adapt theESP32 board with the standard Arduino SD library.

Maksym
Attachments
20211125_175313.jpg
20211125_175313.jpg (725.06 KiB) Viewed 162 times

Maximumotion
 
Posts: 4
Joined: Sun May 24, 2020 10:13 pm

Please be positive and constructive with your questions and comments.