Particle electron SD card problem [SDFAT library]

Im trying to use the Adafruit Sd card with particle electron and I can’t make it work

Im using SDFat library and I can able to flash the code for TryMeFirst example, when I check through the Terminal, it said can’t access SD card Do not reformat. No card, wrong chip select pin, or SPI problem? SD errorCode: 0X1,0X0 ( it is from SDFat.cpp library )

But still not working with Particle electron
( connection CS A2 , SCK A3 , D0 A4 , D1 A5, 3V and GND )

I already checked with my Arduino Uno, it is working !

My goal is that getting the data from the sensor and storing the data to the SD card.

This SD card holder seems to have a level shifter, which you don’t need/want with the Particle devices.

so it is not compatible with particle electron?

I think Rick recommended this SD card adapter ... that is why I bought it

Just for tries, remove your 3V connection and power the board off Vin while having the Electron powered off of USB.

The SparkFun has a level shifter and it works. I haven’t tested the Adafruit one, but I have one on order.

What does your setup code look like?

2 Likes

Also, I believe you should connect the 3V3 line of the Electron the 5V input, not the 3V line.

There's an onboard ultra-low dropout regulator that will convert voltages from 3.3V-6v down to ~3.3V (IC2). There's also a level shifter that will convert the interface logic from 3.3V-5V to 3.3V

I believe the 3V is intended to be an output, not an input.

2 Likes

This is what I’m using

I connected 5V to 3V3 in particle electron but same problem…

Im not using my own code, Im just using SDFAT library and one of example, TRYMEFIRST.

Terminal still shows,
Can’t access SD card. Do not reformat.
No card, wrong chip select pin, or SPI problem?
SD errorCode: 0X1,0X0

Right, but there are a whole bunch of configuration lines at the top of the file. Which one is uncommented?

This is my setup code

#include “SdFat.h”

// Pick an SPI configuration.
// See SPI configuration section below (comments are for photon).
#define SPI_CONFIGURATION 0
//------------------------------------------------------------------------------
// Setup SPI configuration.
#if SPI_CONFIGURATION == 0
// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFat sd;
const uint8_t chipSelect = SS;
#elif SPI_CONFIGURATION == 1
// // Secondary SPI with DMA
// // SCK => D4, MISO => D3, MOSI => D2, SS => D1
// SdFat sd(1);
// const uint8_t chipSelect = D1;
// #elif SPI_CONFIGURATION == 2
// // Primary SPI with Arduino SPI library style byte I/O.
// // SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
// SdFatLibSpi sd;
// const uint8_t chipSelect = SS;
// #elif SPI_CONFIGURATION == 3
// // Software SPI. Use any digital pins.
// // MISO => D5, MOSI => D6, SCK => D7, SS => D0
// SdFatSoftSpi<D5, D6, D7> sd;
// const uint8_t chipSelect = D0;
#endif // SPI_CONFIGURATION
//------------------------------------------------------------------------------

This is just a plain Electron, correct? Not an AssetTracker?

On the AssetTracker the LIS3DH accelerometer uses A2 as the chip select so you need to use a different pin for the SD card CS.

This is a plain SD card

It’s a mystery to me. That SD card breakout should work, and it looks like it’s wired correctly.

I spent many time for this and can’t find issues …

5V -> 3V3
GND -> GND
CLK->A3
DO -> A4
DI -> A5
CS -> A2

maybe it is not compatible with Particle electron ?

I tested with the Adafruit SD card breakout and an Electron and SD Fat 0.0.7 and it worked properly for me.

Connections:

Breakout Electron Color
CD No connection
CS A2 Yellow
DI A5 (MOSI) Green
DO A4 (MISO) Blue
CLK A3 (SCK) Orange
GND GND Black
3V No connection
5V 3V3 Red

And this is the firmware I used:

#include "Particle.h"

// dependencies.SdFat=0.0.7
#include "SdFat.h"


// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFat sd;
const uint8_t chipSelect = A2;

const unsigned long TEST_INTERVAL_MS = 10000;
unsigned long lastTest = 0;


void tryMeFirst();


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

void loop() {
	if (millis() - lastTest >= TEST_INTERVAL_MS) {
		lastTest = millis();
		tryMeFirst();
	}
}

void tryMeFirst() {
	File myFile;

	// Initialize the library
	if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
		Serial.println("failed to open card");
		return;
	}

	// open the file for write at end like the "Native SD library"
	if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
		Serial.println("opening test.txt for write failed");
		return;
	}
	// if the file opened okay, write to it:
	Serial.print("Writing to test.txt...");
	myFile.println("testing 1, 2, 3.");
	myFile.printf("fileSize: %d\n", myFile.fileSize());

	// close the file:
	myFile.close();
	Serial.println("done.");

	// re-open the file for reading:
	if (!myFile.open("test.txt", O_READ)) {
		Serial.println("opening test.txt for read failed");
		return;
	}
	Serial.println("test.txt content:");

	// read from the file until there's nothing else in it:
	int data;
	while ((data = myFile.read()) >= 0) {
		Serial.write(data);
	}
	// close the file:
	myFile.close();
}

And the output:

Writing to test.txt...done.
test.txt content:
testing 1, 2, 3.
fileSize: 18
Writing to test.txt...done.
test.txt content:
testing 1, 2, 3.
fileSize: 18
testing 1, 2, 3.
fileSize: 49
Writing to test.txt...done.
test.txt content:
testing 1, 2, 3.
fileSize: 18
testing 1, 2, 3.
fileSize: 49
testing 1, 2, 3.
fileSize: 80
2 Likes

Im trying to use your code right now and it still said failed to open card from the terminal
It means sd card has problem? Do I need to do something for sd card?
when I tried with Arduino Uno, It doesn’t have issue for sd card…

57%20AM

Unfortunately it’s impossible to say why it’s not working for you. I used a 16 GB FAT32 formatted card in my test.

  • Problem with card
  • Problem with the card formatting
  • Problem with reader
  • Connection problem (though it looks correct from the picture)
  • ???

I really have no idea why it’s not working for you, it looks like it should work.

Did you use SDFAT library in particle library, right?

I tried to use another SD card FAT32 formatted card and still not working, it is so weird…

I have both the sparkfun and adafruit version sdcards. I have followed the same pins and used the exact same code as you. The output continues to say “failed to open card” and no txt file is created. In the past, these cards have worked very sporadically. One day it will work, but the next it won’t, nothing changed. I think there might be a problem with the Particle itself…?

Can you try the updated SdFat library v1.0.15?