Guide by Brenton; Makerverse Micro SD Adapter - MicroPython Guide and Data Logging

I have just shared content in Guides > Raspberry Pi Pico: “Makerverse Micro SD Adapter - MicroPython Guide and Data Logging”





Read more

2 Likes

I have been trying to follow this guide using all the same components on the guide and keep receiving an OSError that there was a timeout waiting for a response. This only seems to occur when I add in a “while True:” loop. Remove the loop and the error disappears. It doesn’t even matter if the SD card is written to or not. Just mounting it will cause the error.

Do you have any ideas for troubleshooting?

2 Likes

This issue is certainly perplexing…

What happens when:

  • Lines are run one by one in the REPL?
  • A “long” (say, 1 second) time.sleep() is added on line 15?

Could you also post the whole script inside a formatted code block (started and ended with triple ` symbols)?

-Brenton

4 Likes

Hi Brenton,

I’ve been tackling your guide. However, when I try to download sdcard.py I get the error message, “Not found”.

https://raw.githubusercontent.com/micropython/micropython/master/drivers/sdcard/sdcard.py

Have you got a new link?

1 Like

Topic - Makerverse Micro SD Adapter - MicroPython Guide

Download sdcard.py Not found!

https://raw.githubusercontent.com/micropython/micropython/master/drivers/sdcard/sdcard.py

Have you a new link?

2 Likes

Hi Ken,

Thanks for letting us know about the broken link, it seems the MicoPython repo was moved.
That link should be fixed again now, you’ll just need to get a non-cached version of the project page, like the link below.

2 Likes

Hi,

Can this module be used with an ESP32 with Arduino/PlatformIO?

If so, are there any tips to which libraries to use?

Thanks

1 Like

Is it exclusively dedicated to micropython only? Or it can be used with Arduino too? Will it support the Arduino SD.h library?

1 Like

Hi Rick and @Rooppoor212784

This module just uses SPI as an interface to the SD card so it ought to work fine with other existing Arduino libraries if you didn’t want to use the micropython library we supply.

Just a note that as at 12 Feb, using v1.19unstable of the micropython installation (by Thonny) I could not get the sdcard.py file to “upload” to the pico. I could copy the file to the pico in UF2 mode, but when the script run it selected the non-uf2 mode and restarted the pico and the sdcard.py file would not appear.

I worked around this issue by have the pico in USB mode (not UF2) and creating a new file in Thonny and cut and pasting into the new file and using Thonny to save it as “sdcard.py”.

I am new at using Thonny and micropython so it might be useful info to some, or just a user error I was overcoming with this approach.

I have uploaded an Arduino C example of how to use this with the Pico (based on the same wiring as the micropython example here and using existing Arduino libraries.

2 Likes

Hi Nigel,

Thanks for sharing your code in case others want a non-micropython implementation with their Pico :slight_smile:

2 Likes

Any idea about how to solve this error? I have been stuck on this for way too long and am just running in circles. I have rewired and used different SPI buses.
image

1 Like

Hi Catie, welcome to the forum!

Could you share some more info on your setup/troubleshooting?

  1. Are you using this on a Pi Pico?
  2. Send a picture of how you have things wired, we might spot something :slight_smile:
  3. Have you tried different SD cards? How have you been formatting the cards?
  4. Did it ever work, or have you been having this issue since you started using it?
  5. Could you point us to the code you’re running, or if you wrote/remixed it yourself, attach it in between 3 backticks (```) for nice viewing on this forum.

The SD adapter doesn’t do any processing, so it’s almost a straight connection between the card and the host microcontroller, so there’s not too much that can go wrong, but we won’t rule out a hardware fault.

Keen to get to the bottom of this one with you!

1 Like

Hey!
I found this thread because I have also run into this error.

  File "/lib/sdcard.py", line 269, in readblocks
  File "/lib/sdcard.py", line 196, in readinto

It’s being caused by this function.

def readinto(self, buf):
        self.cs(0)

        # read until start byte (0xff)
        for i in range(_CMD_TIMEOUT):
            self.spi.readinto(self.tokenbuf, 0xFF)
            if self.tokenbuf[0] == _TOKEN_DATA:
                break
            time.sleep_ms(1)
        else:
            self.cs(1)
            raise OSError("timeout waiting for response")

You can see that it’s just looping a bunch of times until it gets it’s token. On my copy of sdcard.py _CMD_TIMEOUT = 100 and _TOKEN_DATA = const(0xFE)
tokenbuf is a bytearray as self.tokenbuf = bytearray(1)

Its really a wrapper around the SPI.readinto function which is defined here.

SPI.readinto(buf, write=0x00 )
Read into the buffer specified by buf while continuously writing the single byte given by write. Returns None.
Note: on WiPy this function returns the number of bytes read.

So I read this as …

  • Read a byte into tokenbuf.
  • check tokenbuf
  • do that 100 times until you read 0xFE

Note that the comment wants to find 0xFF… so what’s up with that?

I chucked a few print statements into the code.


def readinto(self, buf):
        self.cs(0)
        print("reading")
        # read until start byte (0xff)
        for i in range(_CMD_TIMEOUT):
            self.spi.readinto(self.tokenbuf, 0xFF)
            print(self.tokenbuf[0])
            if self.tokenbuf[0] == _TOKEN_DATA:
                break
            time.sleep_ms(1)
        else:
            self.cs(1)
            raise OSError("timeout waiting for response")

#OUTPUT
<io.FileIO 20014900>
reading
255
254
reading
62
0
0
0
0
# ...  95 more lines of zeros until...
Traceback (most recent call last):
  File "wavplayer.py", line 61, in i2s_callback
  File "/lib/sdcard.py", line 270, in readblocks
  File "/lib/sdcard.py", line 197, in readinto
OSError: timeout waiting for response

These zeros make sense.
I’m reading a wav file, so you would expect a lot of zeros at the beginning.

Infact, here is a hex dump of io.FileIO 20014900

00000000   52 49 46 46 3F 11 1C 02 57 41 56 45 4A 55 4E 4B  RIFF?...WAVEJUNK
00000010   1C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000030   66 6D 74 20 12 00 00 00 01 00 02 00 3F 3F 00 00  fmt ........??..
00000040   00 3F 02 00 04 00 10 00 00 00 64 61 74 61 68 11  .?........datah.
00000050   1C 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000060   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
*
00007000   3F 3F 00 00 3F 3F 00 00 00 00 01 00 00 00 01 00  ??..??..........
... blah blah wav data

What am I missing here?
Anyone ever solve this?
Thanks in advance.
Pix.:heavy_heart_exclamation:

2 Likes

Hey Pixmusix,

What changes if you use a larger value for _CMD_TIMEOUT?

From my understanding of the source code it seems like a fairly arbitrary number chosen to break out of the loop if it gets that far in and doesn’t find any readable data.

0xfe is the start byte used by most read formats so I think the comment is just an unfortunately misleading typo.

sdcard.py seems to be reading the card in 512 byte block increments. I would try changing _CMD_TIMEOUT to different values between 100-512 and seeing if that makes a difference.

As for WHY its stopping only after 100 loops specifically, your guess is as good as mine. :thinking:

Best of luck!

2 Likes

That was my first attempt. I set it to 1000 and it finds the first instance of FE that turns up organically in my .wav file which is 16552 bytes in :stuck_out_tongue:.
I’m going to try a bunch of different wav files and see if they all have the same error.

Man it’s starting to really feel like my entire approach is wrong. How am I supposed to play a wav file with micropython from an SD card. Is there a library I’m supposed to be using? I’m not the first person to try this and I’m having to do sooo much source code diving.

1 Like

It might be time for the sanity check - can you upload some photos of how things are connected, and also tell us what format + size the SD card is?

1 Like

Can do.
I might link you over to here since I’m no longer relevant to Brentons guide anymore.

1 Like