Forums » Req. WJ2(ESP32) support »
Write data on SDcard via SPI
Added by Marco Cini 7 months ago
Hi,
I am using ESP32 DevKit and I am trying to use a very simple model to log data on SD card via SPI adapter (this one: https://www.az-delivery.de/en/products/copy-of-spi-reader-micro-speicherkartenmodul-fur-arduino).
Hardware connection seems to be OK, pins set up is shown in the model attached.
In the SD card I have created a folder called "sdcard" and an empty .csv file called "data".
After some seconds with the SD card plugged on the SPI adapter the csv file is still empty but the model seems to run correctly since LED is blinking. Could you please help me with this?
Thank you in advance,
Marco Cini
Replies (22)
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 7 months ago
Hi Marco,
Thank you for your patience. Currently taking a look. Will keep you updated.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 7 months ago
The model was not running correctly.
It seems like the reason was because the sample time in the constant blocks was 'inf', which means the inputs from the blocks were never collected by the string processing block and hence never logged to SD card.
Please change the sample time of the constant blocks to a number that represents how often you want to log the value(s) to the SD card.
I would also recommend using the Print block to help print the values onto the serial monitor. That is also how I was able to debug that the input from the constant blocks weren't being logged.
You may enable the Serial monitor from the Waijung 2 Target Setup block itself. That way you won't need to use an external serial monitor like PuTTY / Docklight and the Command Prompt will automatically open once the build is successful.
Hope this helps.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 7 months ago
Hi Parth,
thank you for your support.
Unfortunately, I tried again following your suggestion but I failed again due to another issue, After the building process this is what I get as output in the Command Promt:
E (452) vfs_fat_sdmmc: mount_to_vfs failed (0x102).
I (453) gpio: GPIO5| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
E (456) WJG2_SD_BLOCK: Failed to initialize the card (ESP_ERR_INVALID_ARG). Make sure SD card lines have pull-up resistors in place.
E (970) WJG2_SD_BLOCK: Failed to open file and config for writing
I tried to mount 10KOhm pull-up resistor on MOSI,MISO,SCK and CS pin but the result is the same: sometimes "ESP_ERR_INVALID_ARG" changes into "ESP_ERR_INVALID_CRC" or "ESP_ERR_INVALID_TIMEOUT".
I tried to reduce SD host frequency from 20MHz to 10MHz but it is still not working.
Could you please help me also with this step?
Thank you in advance,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 7 months ago
I was previously using an ESP32 with a built-in SD card slot to run your example and did not face any issues, but faced similar/same errors when I tried it with ESP32 DevKit + external SD adapter.
Please refer to this for more information. It states that external SD card adapters face incompatibility issues as is and also states the default pins to use on ESP32 and other families in order to connect to SD card via SPI.
Here are the changes I made that made it work with ESP32 DevKit + external SD adapter.
1) Use the default pins given in the link attached (or as shown in the image below).
2) Use SD host frequency of 10MHz.
3) Change default stack depth to 4096 in the Waijung 2 target setup block.
4) Use 5V input instead of 3.3V. The reason may be because some SD card modules have one Vcc (power input) pin, and it is wired to a 3.3V regulator. Powering a 3.3V regulator with 3.3V can result in a lower output voltage such that it is not enough to operate the SD card.
Hope this helps fix the problem.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 7 months ago
Hi Parth,
thank you for your quick response!
I followed your instruction but the issue is the same. I tried even with the ESP expansion board, suppling the expansion with 5V but I got the same result.
Moreover, I noticed that during some flash procede I got this error:
Instead, if I am able to succesfully complete the flashing procedure, the error I got is the same as my previuos post.
Best regards,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Marco Cini 6 months ago
Hi,
just another note to go deeper in this issue: I installed the SD card adapter on a PCB in order to reduce the wires length, I also performed different tests with 10kOhm pull-up resistors but the response I got from the Command Promt is always:
I (453) gpio: GPIO5| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
E (456) WJG2_SD_BLOCK: Failed to initialize the card (ESP_ERR_INVALID_ARG). Make sure SD card lines have pull-up resistors in place.
E (970) WJG2_SD_BLOCK: Failed to open file and config for writing
To me this could be a crucial functionality to open the development of systems via Waijung and ESP32.
Thank you in advance,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 5 months ago
Hi Marco,
Thank you very much for your patience. Please accept my sincere apologies for the late response.
It's quite strange how me running the same thing works on my end but doesn't on yours. The only difference being the hardware modules we are using but since you are able to write to the SD card using Arduino IDE it is clear that ESP32 or the SD card module is not the issue. I am wondering though, what is the size of the SD card you are using? Is it more than 32GB?
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 5 months ago
Hi Parth,
thank you for your feedback.
I am using a 32GB SD card. I will try with a smaller one. In which way did you format your SD card?
Best regards,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 5 months ago
Hi Marco,
I always use 16GB. Please also make sure it is formatted in FAT32 format.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 5 months ago
Hi Parth,
I format again my SD card in FAT32 but it still not working.
Once the building process is completed, I always got this result, with or without the SD card placed in the SD card adapter connected with my ESP32 DevKit V1:
Best regards,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 5 months ago
Hi Marco,
Two things that I would like to confirm:
1) Along with the latest formatting change, you also have the four changes applied that I mentioned earlier right? i.e. using GPIO 2,13,14,15, switching to 10MHz host frequency, default stack depth of 4096, and using 5V input.
2) Do you have a logic analyzer or oscilloscope? If yes, could you please monitor the SPI signals and let us know the results?
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 5 months ago
Hi Parth,
I will answer to your points below.
1) Yes, I checked again and I am testing with MISO:2, MOSI:15, SCK:14, CS:13. SD host frequency 10MHz. Default xTaskCreate_stack depth = 4096 and 5V SD card slot supply voltage.
2) I checked and I saw this:
MOSI= costant, slightly above 1V
MISO= 0V
SCK=0V
CS=3.3V
I tried to span in the range 1 ns/div to 100 ms/div but I have never observed somethinng similar to SPI signal.
Best regards,
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 5 months ago
Hi Marco,
Could you send us some photos of the signals you are seeing on the logic analyser/oscilloscope?
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 5 months ago
Hi Parth,
Using this adapter here https://www.amazon.it/Adattatore-Compatibile-Raspberry-Intelligenti-Stampanti/dp/B08FB3GC34 the result is:
MOSI
While MISO, SCK and CS are something like this:
Moreover, I noticed that usign the adapter shown here: https://www.az-delivery.de/it/products/copy-of-spi-reader-micro-speicherkartenmodul-fur-arduino?variant=38523274578&utm_source=google&utm_medium=cpc&utm_campaign=21123962822&utm_content=160034815093&utm_term=&gad_source=1&gclid=CjwKCAjw8diwBhAbEiwA7i_sJQD9567THrO52-zot9qs7tz1OJa-FZ0rUZg1zbbGIngzXnR0rJJr2hoCB_MQAvD_BwE
the result seems to be different since MOSI is 0V and MISO is the one shown here (5V supply voltage):
Are you using ESP32 DevKit V1 with one one of this two adapter? Could you please send me a pcture of your setup? I would like to replicate it identically. From my side is very weird to get these results.
Marco Cini
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 5 months ago
Hi Marco,
Apologies for the late response.
It seems like the results will be different from module to module, so it's difficult to derive insights.
Here is my setup:
1) I used ESP32 DevKit V4 initially but it worked with other models of ESP32 as well on my end.
2) The SD card adapter I am using is this one.
3) Other things I applied to my setup were what we've already spoken about i.e.
a) MISO:2, MOSI:15, SCK:14, CS:13
b) SD host frequency 10MHz
c) Default xTaskCreate_stack depth = 4096
d) 5V SD card slot supply voltage
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 4 months ago
Hi Parth,
Is your ESP32 DevKit V4 the one reported here? https://docs.espressif.com/projects/esp-idf/en/stable/esp32/hw-reference/esp32/get-started-devkitc.html
In which way do you create the HW connections between the SD card adapter and EPS32 board?
Best regards,
Marco
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 4 months ago
Is your ESP32 DevKit V4 the one reported here? https://docs.espressif.com/projects/esp-idf/en/stable/esp32/hw-reference/esp32/get-started-devkitc.html
Yes it is
In which way do you create the HW connections between the SD card adapter and EPS32 board?
I am using F-F jumper wires only to create the connections as attached below.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 4 months ago
Hi Parth,
I bought a ESP32 DevKit V4 and I tested it with the same model and same microSD adapter.
Finally, I can write on micro SD using ESP32 DevKit V4 but I still cannot do it with the one shown in the picture (used up to now).
Do you know whichh are the differences between these two?
Best regards,
Marco
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 4 months ago
Hi Marco,
Glad to know it is working for you now. The image that you have attached - is it of ESP32 DevKit V1? Which other ESP32 boards have you tested that work/don't work?
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 4 months ago
Hi Parth,
yes, I think that the proper name of that board is ESP32 DevKit V1.
The result is: ESP32 DevKit V4 works, ESP32 DevKit V1 doesn't.
Best regards,
Marco
RE: Write data on SDcard via SPI
-
Added by Parth Maheshwari (พาร์ท) 4 months ago
Hi Marco,
Noted, thank you.
Seems like a compatibility issue with ESP32 DevKit V1. Do you have any other ESP32 boards that you can try with as well? If yes, please do let me know the result with those too.
Sincere regards,
Parth
RE: Write data on SDcard via SPI
-
Added by Marco Cini 4 months ago
Hi Parth,
no, unfortunately I have only these two.
Sincere regards,
Marco