Detecting an inserted SD card



  • Hi,

    As sd = SD() seems to blocking infinitely if no SD card is inserted I would love to know how I can detect if a SD card is inserted.



  • @daniel

    I just test this and the behaviour is still as before. Trying to initialize the SD card without a SD inserted raises a OSError exception, which is OK.

    my English is bad and when i wrote

    Something was changed.

    than this was only assumption - i do not test this with current 1.6.3 firmware
    i tested this on 1.5.0 and it worked - and if for @kanu it does not work
    then i supposed that he tested this with current firmware

    Card detection is simply done via a GPIO pin

    good point - i must connect it :)



  • @kanu @livius I just test this and the behaviour is still as before. Trying to initialize the SD card without a SD inserted raises a OSError exception, which is OK.

    @kanu which firmware version are you running and on which board? Do you have anything else connected to the SD card pins?

    Cheers,
    Daniel



  • But will be good to see some callback that someone plug/unplug SD
    Is this somehow possible?

    Yes, it is. Card detection is simply done via a GPIO pin, so registering a callback on the card detection pin it's that is needed. Just note that on our Expansion Board there are no connections for the card detect signal, so it would have to be done with custom SD card hardware. In any case, in an embedded system, plugin the SD card in an out is not something that happens really often.



  • @daniel
    interesting - i have tested this before and this does not hang the board
    Something was changed.

    But will be good to see some callback that someone plug/unplug SD
    Is this somehow possible? I suppose that yes.



  • @kanu

    You would do it like this:

    from machine import SD
    
    try:
        sd = SD()
    except:
        sd = None
    
    if sd:
        ....
        ....
    

    But if course, calling SD() with no card inserted should not hang the board. I'll have a look at it and fix it. Thanks!

    Cheers,
    Daniel



  • This post is deleted!


Pycom on Twitter