guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

sdcard.py, no sd card error

Mon Apr 05, 2021 10:30 pm

Hi I have been trying to use sdcard.py to run my sdcard with pico.
However I keep getting the following error: no SD card
I have checked the conections and it is conencted perfectly.
This is the code that I am using to call sdcard.py

Code: Select all

import machine
import os
import sdcard


sd = sdcard.SDCard(machine.SPI(1), machine.Pin(13))  
os.mount(sd, '/sd')
# check the content
os.listdir('/sd')

# try some standard file operations
file = open('/sd/test.txt', 'w')
file.write('Testing SD card write operations')
file.close()
file = open('/sd/test.txt', 'r')
file.read()
file.close()

hippy
Posts: 10892
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 2:35 pm

guillez4 wrote:
Mon Apr 05, 2021 10:30 pm
I have checked the conections and it is conencted perfectly.
It would be worth detailing how you have connected it, show a photo of that, and point to the reference which shows that is how it should be connected.

It is best to get that verified and confirmed as correct before potentially wasting time on it being something else when it isn't.

A link which shows that your code is what should be used to verify it is working wouldn't go amiss either.

It is always hard to debug a 'black box' which either works or doesn't, especially remotely, so the more you can help others to help you the easier that will be.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 5:56 pm

Thanks for the reply hippy

Sure I saw this topic and I thought I could use sdcard.py module to save data with my pico in the sd card.
viewtopic.php?t=307275

I also have also tested the sd card with my arduino nano and it works without any problem.
The pin connections are:
CS=GP13
MISO=GP12
MOSI=GP11
CLK=GP10
VDD=3.3V
PINOUT.jpeg
PINOUT.jpeg (164.65 KiB) Viewed 1530 times

danjperron
Posts: 3884
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 6:45 pm

Default SPI1_MISO is not GP12 but GP8!
move pin16 to pin 11!

Raspberry PI Pico python SDK manual page 16&17.

hippy
Posts: 10892
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 6:46 pm

guillez4 wrote:
Tue Apr 06, 2021 5:56 pm
Sure I saw this topic and I thought I could use sdcard.py module to save data with my pico in the sd card.
viewtopic.php?t=307275
That does seem to be using different GPIO pins than you are using.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 7:23 pm

danjperron wrote: Default SPI1_MISO is not GP12 but GP8!
move pin16 to pin 11!

Raspberry PI Pico python SDK manual page 16&17.
If you look the raspberry PICO pinout you can use pins 16 and 15 for miso and mosi respectively, just by declaring SPI(1).
Also in this video they use the same connections for the SDcard with the pico:
https://www.youtube.com/watch?v=yN1M8ZCN3x8
I have already tested the example of the video and it works fine, but I can´t make work sdcard.py.

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 8:17 pm

guillez4 wrote:If you look the raspberry PICO pinout you can use pins 16 and 15 for miso and mosi respectively, just by declaring SPI(1).
Not really. @danjperron is right, default pin for SPI1 MISO is pin 11 (GP8). You either have to move your wire or tell Pico explicitly to use the GP you want:

Code: Select all

SPI(1, miso = Pin(12))
In the code used in the video you've linked to there's this line (source here):

Code: Select all

spi = io.SPI(board.GP10, board.GP11, board.GP12)
which sets SCK to use GP10 (pin 14), MOSI to use GP11 (pin 15), and MISO to use GP12 (pin 16). SCK and MOSI are the same as defaults, but MISO is changed from GP8.

I think that you're confusing physical pin numbers ("legs" of the Pico) with GP numbers that are actually used to define which GPIO are used.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 9:33 pm

Aaaaa okey sorry yeah I have tried with those pins and it works the sdcard. Thanks!
However now I get the following error: OSError: 19
In

Code: Select all

os.mount(sd, '/sd')

danjperron
Posts: 3884
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 10:16 pm

You need to create the folder into the internal drive!

Use thonny!

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 10:25 pm

Yes I have already created the folder and I get this error.

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 10:39 pm

If I remember correctly this error means Pico cannot read the filesystem on the SD. What card are you using and how are you formatting it?

danjperron
Posts: 3884
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 10:41 pm

if you are using the code from your first post it won't work!

You forgot to specify the file system

Did you read fully the post that you specified? At least the post I replied about it?
viewtopic.php?f=145&t=308819#p1847373

Code: Select all

from machine import SPI,Pin
import uos
import SDCard
sd = SDCard.SDCard(SPI(1),Pin(13))
fs = uos.VfsFat(sd)
uos.mount(fs,"/sd")
uos.listdir("/sd")

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 10:54 pm

danjperron wrote:You forgot to specify the file system
It shouldn't be necessary in the newest (or at least newer than that installed by Thonny) MicroPython. I'm not doing it in any of my programs using SD card and everything works just fine.

This makes me wonder which version is @guillez4 using?

danjperron
Posts: 3884
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: sdcard.py, no sd card error

Tue Apr 06, 2021 11:57 pm

It shouldn't be necessary in the newest (or at least newer than that installed by Thonny) MicroPython. I'm not doing it in any of my programs using SD card and everything works just fine.
Thanks horuable,
you are correct! His code works fine on my pico.

You don't have to create the folder also.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Wed Apr 07, 2021 6:13 am

Thanks for the replies
I am using two micro SD cards of 16GB with FAT32: Intenso and Sam Disk.
I have installed the last micropython version that Thonny lets my update, which is v1.14-0205

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Wed Apr 07, 2021 6:59 am

Try installing the newest release directly from MicroPython downloads and see if that helps.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: sdcard.py, no sd card error

Wed Apr 07, 2021 7:31 am

Thanks it´s solved, I have done the build process and now it works

TheMecaProject
Posts: 11
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Fri Apr 16, 2021 1:53 am

Hello everyone! If there is still time for a micropython newbie to join the discussion, I'd like to participate aswell.

I've uploaded my firmware to the most recent UF2 file available on Micropython's website, as @horuable suggested. then I connected my SD card to the Pico using what I understood to be the default SPI setting explained here:

Pico's GP10 (SCK) --> SD card's SCK
Pico's GP11 (MOSI) --> SD card's MOSI
Pico's GP8 (MISO) --> SD card's MISO
Pico's GP13 --> SD card's CS

By doing this and running the code of the very first message I get the same "No SD card" reply.
If I only switch the MOSI and MISO wires of the SD card, it then becomes the "OSError: 19".
The SD card has 2GB storage and has been formatted as FAT32.

Perhaps I'm missing something silly, but as I said I'm new to micropython. And SPI wiring specially always confused me for some reason.
Can anyone give me a light of wisdom? Many thanks in advance!

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Fri Apr 16, 2021 11:01 am

Your wiring seems to be correct, but you forgot to mention what adapter are you using to connect the card to Pico. Some refuse to work, especially if they have a level shifter on board.

TheMecaProject
Posts: 11
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Fri Apr 16, 2021 3:51 pm

Fair enough! I'm using a just-bought module which happens to be the same as the one in this video (already mentioned in this thread by the way). I can post a picture of my wiring as soon as possible if it helps.

horuable
Posts: 243
Joined: Sat Mar 06, 2021 12:35 am

Re: sdcard.py, no sd card error

Fri Apr 16, 2021 4:21 pm

TheMecaProject wrote:the same as the one in this video
Yup, this is your problem. I have no idea how they've made it work in the video, probably circuitpyhon just uses some kind of magic. Try using a simple adapter like this shown by @danjperron here: viewtopic.php?f=146&t=307275#p1838662

TheMecaProject
Posts: 11
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Fri Apr 16, 2021 8:26 pm

horuable wrote: Yup, this is your problem. I have no idea how they've made it work in the video, probably circuitpyhon just uses some kind of magic. Try using a simple adapter like this shown by @danjperron here: viewtopic.php?f=146&t=307275#p1838662
Quite interesting. I decided to check on Arduino if things would go any better, and it actually wrote/read data quite nicely. Perhaps what compromises for the Pico side is the logic level? Besides the library and language, that was the only difference I spotted.

Ayway, I'll go ahead and try using the adapter you've mentioned. Let's see what comes out. Thank you for the help!

TheMecaProject
Posts: 11
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Sat Apr 17, 2021 1:35 am

So I soldered the pins on a common microSD adapter I had around, just like @danjperron's post, but saw no difference on the outcome, no matter which pins I chose for the Pico SPI or the code to be tested. Unable to think of any other possible cause, I decided to test different microSD cards and finally one of them worked. It mounted nicely and the Shell displayed the available root files of the card.

So in short it worked, but for one microSD card only.

I've tested five of them and all seemed to work fine as storage units (some where even storing Raspbian and MotionEye). All have then been formatted as FAT32, but nothing changed on results. So are there any specific settings I should apply to the microSD cards in order to make them work?

TheMecaProject
Posts: 11
Joined: Fri Apr 16, 2021 12:02 am

Re: sdcard.py, no sd card error

Sun Apr 18, 2021 10:14 pm

horuable wrote: Yup, this is your problem.
I've been testing a bit more and found out the adapter was no problem - I took that one microSD that worked, tested on both modules and they both successfuly read/wrote data on it. My problem is somehow related to the microSDs themselves.

Yesterday I tested seven SD cards and managed to make an extra one work, so now I have two going OK. Both are SDHC, both are 8GB and both have been Windows-10 formatted as FAT32. Both also have no brand and came on 3D printers I bought in the past.

Image

The other SD cards were formatted aswell and are from makers like Samsung and SanDisk, some are SD 2GB, some are SDHC 8GB and there's one with 16GB. They all work fine as storage units. I just can't understand why that's happening.

User avatar
jbeale
Posts: 3893
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: sdcard.py, no sd card error

Sun Apr 18, 2021 10:29 pm

At the beginning of the "sdcard.py" program I'm using is this code:

Code: Select all

from micropython import const
import time
_CMD_TIMEOUT = const(100)
I have a black 8GB SanDisk card that looks much like several in your "not working" pile. My card also did not work.

I changed that _CMD_TIMEOUT value in sdcard.py from 100 to 1000, and then my card worked!
(Not right away, after changing the code I also had to pull it out and re-insert, but then it did work.)
So that is probably worth a try.

There was a saying where I used to work about the reliability of systems that need various "magic numbers" for time delays.
A timeout limit is not quite the same as a fixed delay, but the idea still seems applicable here.

Return to “MicroPython”