Hardware
I was receiving my ordered SD module but I was struggling a bit to connect it to my ESP32 via the SPI interface.
- I double checked the connections to make sure that I was using the right SPI pins, no change!
- I reformatted the SD drive again using FAT32. This did not help either!
Finally I got it to work when I connected VCC to 5V instead of 3.3V – I should have read the spec because it was saying ‘Power supply is 4.5V ~ 5.5V, 3.3V voltage regulator circuit board’!
Here are the relevant connections:
SD | ESP32 |
---|---|
CS | VSPI-CS0 (GPIO 05) |
SCK | VSPI-CLK (GPIO 18) |
MOSI | VSPI-MOSI (GPIO 23) |
MISO | VSPI-MISO (GPIO 19) |
VCC | VIN (5V) |
GND | GND |
Software
The next challenge was to master the Arduino SD library. There are quite a few different implementations out there but I concentrated on the one which comes with the ESP32. On all examples that you find in the Web you get a sketch that writes to the SD and another which reads the data.
I wanted to implement a stack that can deal with variable length entries, so I needed to push (write) and pop (read) repeatedly in the same sketch.
It seems that the only reliable way to do this is structure the logic in the following way:
- Write (push)
- File file = SD.open(fileName, FILE_WRITE);
- file.seek(new position);
- file.write(data, len);
- file.close();
I could only see the data after closing the file and changing to read mode!
- Read (pop)
- File file = open(fileName); // for read
- file.seek(last_position);
- file.read(data, len);
- file.close();
It seems that a read/write mode where you could do reads and writes at the same time does not exist (at least not in the ESP32 implementation).
I am looking forward now to do some new projects which rely on a lot of memory…
0 Comments