Skip to content

Speeding up SD card writes #1117

@schlaegerz

Description

@schlaegerz

Hardware:

Board: ESP32 Dev module
Core Installation/update date: 2/13/2018
IDE name: Arduino ID
Flash Frequency: 40Mhz
Upload Speed: ?115200?

Description:

I am trying to figure out how to speed up my SD card writes. I don't really care about the file structure of the SD card, and only want to write a lot of data to an SD card. I was hoping to get at least 1 MB/s of writing but I seem to be getting significantly lower than that no matter what I try I cant get much better than .25 MB/s.

I am not sure if this is a hardware issue, or something that I can fix with software, or if this is just the max speed I can expect.

I am using a micro sd card with an adapter like this:

https://www.amazon.com/SenMod-Adapter-Reader-Module-Arduino/dp/B01JYNEX56/ref=sr_1_2?s=pc&ie=UTF8&qid=1518737045&sr=1-2&keywords=micro+sd+card+adapter+board

I have tried the SD_MMC but haven't been able to get it to work.

Sketch:

#include <SD.h>

#define PIN_NUM_MISO 2
#define PIN_NUM_MOSI 15
#define PIN_NUM_CLK  14
#define PIN_NUM_CS   13
void setup() {
	Serial.begin(9600);
	delay(3000);
	auto SPI = SPIClass();
	SPI.begin(PIN_NUM_CLK, PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CS);
	auto speed = 4000000;
	if(!SD.begin(PIN_NUM_CS, SPI, speed))
       {
           Serial.println("NOT INIT");
           return;
        }


	testFileIO( "/test.txt", 512, 1);
	testFileIO( "/test.txt", 1024, 1);
	testFileIO( "/test.txt", 2046, 1);

	testFileIO("/test.txt", 512, 10);
	testFileIO("/test.txt", 1024, 10);
	testFileIO("/test.txt", 2046, 10);

}

void loop() {

}


void testFileIO(const char * path, uint32_t buffSize, uint32_t numMB) {
	//File file = fs.open(path, FILE_WRITE);
	File file = SD.open(path, "w");
	size_t i;
	auto start = millis();
	uint8_t * buff = new uint8_t[buffSize];
	auto numToWrite = (numMB * 1024 * 1024) / buffSize;
	for (i = 0; i<numToWrite; i++) {
		//file.write(buf, buffSize);
		file.write(buff, buffSize);
		yield();
	}
	auto end = millis() - start;
	double seconds = end / 1000;
	if (seconds < 0)
	{
		seconds = 1;
	}
	double MBS = numMB / seconds;
	Serial.printf("%d: MB per Second: %6g :", end, MBS);
	Serial.printf("Seconds per MB: %6g: numMB %d buffSize %d\n", 1 / MBS, numMB, buffSize);
	file.close();
	delete[] buff;
}

I get:
5004: MB per Second: 0.2 :Seconds per MB: 5: numMB 1 buffSize 512
4330: MB per Second: 0.25 :Seconds per MB: 4: numMB 1 buffSize 1024
4812: MB per Second: 0.25 :Seconds per MB: 4: numMB 1 buffSize 2046
46069: MB per Second: 0.217391 :Seconds per MB: 4.6: numMB 10 buffSize 512
45727: MB per Second: 0.222222 :Seconds per MB: 4.5: numMB 10 buffSize 1024
45967: MB per Second: 0.222222 :Seconds per MB: 4.5: numMB 10 buffSize 2046

Debug Messages:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions