0

MicroSD Card with CircuitPython on Arduino MKR ZERO
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MicroSD Card with CircuitPython on Arduino MKR ZERO

by LeKian on Mon Jan 04, 2021 9:55 pm

I am trying to get an SD card to work on my Arduino MKR ZERO board using CircuitPython. The SD card is a generic 2 GB MicroSD card. I tried using the Adafruit tutorial from https://learn.adafruit.com/adafruit-mic ... cuitpython. However, when I try to initialize the SD card object, I get this exception:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "code.py", line 16, in <module>
  File "adafruit_sdcard.py", line 116, in __init__
  File "adafruit_sdcard.py", line 177, in _init_card
  File "adafruit_sdcard.py", line 145, in _init_card
OSError: no SD card


The relevant code here is:
Code: Select all | TOGGLE FULL SIZE
11 SD_CS = board.SD_CS
12
13 # Connect to the card and mount the filesystem.
14 spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
15 cs = digitalio.DigitalInOut(SD_CS)
16 sdcard = adafruit_sdcard.SDCard(spi, cs)
17 vfs = storage.VfsFat(sdcard)
18 storage.mount(vfs, "/sd")


It may not be relevant, but when inserting the SD card while connected to the computer via USB cable, the Arduino resets and sends this message to the serial console:
Code: Select all | TOGGLE FULL SIZE
Running in safe mode! Not running saved code.

You are in safe mode: something unanticipated happened.
The microcontroller's power dipped. Make sure your power supply provides
enough power for the whole circuit and press reset (after ejecting CIRCUITPY).


I have tried hooking the Arduino up to a 5V power supply, but it still didn't work and had the exact same error when plugging in the SD card. I have no idea what the issue here is and I would appreciate any advice. Thank you very much.

LeKian
 
Posts: 4
Joined: Thu Nov 19, 2020 8:54 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Tue Jan 05, 2021 9:45 pm

I'm guessing that you might need to use a different SD_CS pin with the MKR ZERO board. We give an example of using a GPIO pin for the ESP32 in comments. I think you would want to use GPIO28 on the MKR board for chip select

Code: Select all | TOGGLE FULL SIZE
cs = digitalio.DigitalInOut(board.SD_CS)
# Or use a GPIO pin like 15 for ESP8266 wiring:
#cs = digitalio.DigitalInOut(board.GPIO15)


Are you running the latest version of CircuitPython 6.0.1 and of the SD libraries from our bundle?

https://circuitpython.org/board/arduino_mkrzero/

Please do not insert the SD card or remove it when the board is powered.

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by LeKian on Wed Jan 06, 2021 12:01 am

For the libraries, I am using the 6.x mpy library bundle, which I assume is up to date since I downloaded it a week or two ago. The version of CircuitPython on the MKR ZERO is 6.0.0 according to the REPL.

The board does not appear to have any GPIO pins. Using the dir(board) method lists these options:
['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'BATTERY', 'D0', 'D1', 'D10', 'D11', 'D12', 'D13', 'D14', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'I2C', 'L', 'MISO', 'MOSI', 'RX', 'SCK', 'SCL', 'SDA', 'SD_CD', 'SD_CS', 'SD_MISO', 'SD_MOSI', 'SD_SCK', 'SPI', 'TX', 'UART', 'VOLTAGE_MONITOR']

For the record, I tried all of the different SD_ pins, none of which worked.

LeKian
 
Posts: 4
Joined: Thu Nov 19, 2020 8:54 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Wed Jan 06, 2021 4:29 pm

@LeKian,

I see the issue. You are currently use generic SPI pins meant for other peripherals, but the MKR ZERO has dedicated SPI pins that showed up in the dir() output. Try this line for spi initialization.

Code: Select all | TOGGLE FULL SIZE
 spi = busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO)

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by LeKian on Thu Jan 07, 2021 8:43 pm

Using the alternate SD pins fixes the OSError, but unfortunately there's now a new one: When the code reaches line 17,
vfs = storage.VfsFat(sdcard)
The serial console stops responding until saving the code again to cause a soft reboot, which outputs this error:
[Errno 5] Input/output error

Does this indicate some kind of hardware damage involving the SD?

LeKian
 
Posts: 4
Joined: Thu Nov 19, 2020 8:54 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Fri Jan 08, 2021 12:51 pm

LeKain,

The code you are using sounds correct now that it builds.

I think the issue is with accessing the SD Card filesystem. What have you done to prepare the card?

We have an official SD formatter we recommend. The SD card must be formatted with either FAT16 or FAT32 filesystem to work.

https://learn.adafruit.com/adafruit-mic ... -3068803-2

If you bought an SD card, chances are it's already pre-formatted with a FAT filesystem. However you may have problems with how the factory formats the card, or if it's an old card it needs to be reformatted. CircuitPython supports both FAT16 and FAT32 filesystems.

It's always good idea to format the card before using, even if it's new! Note that formatting will erase the card, so save anything you want first.

We strongly recommend you use the official SD card formatter utility - written by the SD association. It solves many problems that come with bad formatting!


https://www.sdcard.org/downloads/formatter/

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by seidler on Thu Jan 14, 2021 3:21 am

Hi, I'm working with LeKian and have the same hardware and, alas, the same issue as in his last post. I've used the recommended formatting tool on the SD card, and it is now definitely FAT16.
The code is:

Code: Select all | TOGGLE FULL SIZE
import board
import busio
import digitalio
import adafruit_sdcard
import storage

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)

vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
print('worked')


Same as before, the code is hanging at

vfs = storage.VfsFat(sdcard)

with the return message:

File "code.py", line 12 in <module>
OSError: [Errno 5] Input/output error

Any ideas? We're a few weeks out from wanting to use the SD card for students in a python + arduino class....

seidler
 
Posts: 11
Joined: Sat Nov 07, 2020 1:45 am

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Thu Jan 14, 2021 6:56 pm

I think the issue is now related to the CS pin. The MKR ZERO doc doesn't specify a specific pin to use and it is not necessary with a seperate SPI bus dedicated to the SD_CARD. Try taking out references to the CS pin.

https://store.arduino.cc/usa/arduino-mkrzero

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by seidler on Fri Jan 15, 2021 12:18 am

Thanks, Mike.

You're exactly right that the MKR zero doesn't have an SD_CS pin, we should have spotted that earlier.
I agree with the need to get rid of reference to the SD_CS pin, but the documentation for the adafruit_sdcard module is explicit:

"Requires an SPI bus and a CS pin. Provides readblocks and writeblocks methods so the device can be mounted as a filesystem."

see https://circuitpython.readthedocs.io/pr ... t/api.html

Hence, can you suggest a module to use to access the SD card in this case, or some other workaround with the MKR zero in circuit python?

seidler
 
Posts: 11
Joined: Sat Nov 07, 2020 1:45 am

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Fri Jan 15, 2021 1:36 pm

@seidler,

Since a CS pin is not necessary of any importance how about trying to use a GPIO on the MKR Zero such as board.D14 as the CS pin. Nothing needs to be connected and it might just work as is.

I think it would also be a good idea for you to open an issue with the Adafruit_CircuitPython_SD (aka adfruit_sdcard) library on github showing the code and where it stop working around the CS pin problem.

https://github.com/adafruit/Adafruit_Ci ... _SD/issues

This our classic MicroSD breakout which you can connect to over the exposed MKR Zero SPI.

https://www.adafruit.com/product/254
https://learn.adafruit.com/adafruit-mic ... d-tutorial

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by seidler on Fri Jan 15, 2021 11:25 pm

Thanks again. I tried using D14, so that I have:

spi = busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO)
cs = digitalio.DigitalInOut(board.D14)
sdcard = adafruit_sdcard.SDCard(spi, cs)

It crashes on the last of those lines (sdcard = ...) , with the traceback showing OS:Error: no SD card.

I just got a Feather M0 adalogger and will try that to see if there is an issue with the microSD (which works fine in my computer and has been FAT16 formatted), but otherwise I'm at a loss. I'll make a post at the github also, thanks for the suggestion.

seidler
 
Posts: 11
Joined: Sat Nov 07, 2020 1:45 am

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by seidler on Sat Jan 16, 2021 11:17 pm

An update: SUCCESS!

I tried the sample code (same as we've been using here) for the Feather M0 adalogger and the same 2GB FAT16 microSD card as I had been using with the MKRzero. The Feather M0 failed in the same way as the MKRzero had been (with that microSD card).

I bought a new 8GB micro SD card from a different vendor. Then the Feather M0 worked, which validated that I now had a working SD card (where 'working' means 'working with the adafruit_sdcard module').

Playing around with the MKR zero code finds that although the MKR zero doesn't have an SD_CS pin that one should simply act as if it does -- apparently the adafruit_sdcard module accommodates this eccentricity of the MKRzero. Hence, exactly the same code as for the Feather M0 adalogger does indeed work for the MKR zero. I've had no trouble using the MRKzero to write text files to the new SD card. For completeness here's the code:

Code: Select all | TOGGLE FULL SIZE
import busio
import digitalio
import adafruit_sdcard
import storage

# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SD_SCK, board.SD_MOSI, board.SD_MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

with open("/sd/testzero.txt", "w") as f:
    f.write("Hello world\n")
print('success, saved file!')


Of possible use to the future, I'll note that the SD card that did not function with circuit python (but was properly formatted
and worked on my PC) was 2 GB from Transcend, and that the 8GB microSD from 'KEXIN' is the one that works fine.

seidler
 
Posts: 11
Joined: Sat Nov 07, 2020 1:45 am

Re: MicroSD Card with CircuitPython on Arduino MKR ZERO

by mikeysklar on Mon Jan 18, 2021 3:23 pm

It has been a never ending nightmare dealing with finicky behavior of different brands of SD cards. Thank you for trying another device and getting back to us with your working code and feedback.

mikeysklar
 
Posts: 2880
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.