# Can we detect the SD card if not present?

**URL:** <https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497>\
**Category:** SDK\
**Tags:** evb-amb82\
**Created:** [November 7, 2025, 5:35am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497 "2025-11-07T05:35:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 7, 2025, 5:35am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/1 "2025-11-07T05:35:52Z")

</div>

1. **Problem** : The SDK cannot detect whether an SD card is inserted or removed. Error returns are not exposed.
2. **Root Cause** : The SD card driver lacks proper initialization feedback or runtime status checks.

---

<div class="post-metadata">

**Author:** ![Pammy](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/pammy/32/936_2.png) [@Pammy](https://forum.amebaiot.com/u/Pammy)\
**Post date:** [November 11, 2025, 6:10am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/2 "2025-11-11T06:10:38Z")

</div>

Hi @DevomechSolutions

In the [FreeRTOS SDK](https://github.com/Ameba-AIoT/ameba-rtos-pro2), there is a function that is available to detect whether an SD card has been inserted or removed.

You can refer to the function `SD_disk_status` in [sdcard.c](https://github.com/Ameba-AIoT/ameba-rtos-pro2/blob/main/component/file_system/fatfs/disk_if/src/sdcard.c). However, do note that the SD card has to be properly initialized before calling this function to check its status.

If you’re using the Arduino SDK, please note that this feature is still under development.

---

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 11, 2025, 7:32am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/3 "2025-11-11T07:32:29Z")

</div>

I am using the Arduino SDK, Is there any hardware pin to detect the SD card in slot?

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 11, 2025, 10:23am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/4 "2025-11-11T10:23:54Z")

</div>

> [@DevomechSolutions](#):
>
> The SD card driver lacks proper initialization feedback or runtime status checks.

Could you try the following code for the run time checking?

```cpp
while (fs.status() != 1) {
    fs.begin();
}

```

`fs.status()` is the function for checking runtime status.

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 11, 2025, 10:25am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/5 "2025-11-11T10:25:25Z")

</div>

If we use the `fs.status()` to check, any GPIO can be the indicate pin.

---

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 11, 2025, 1:05pm UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/6 "2025-11-11T13:05:15Z")

</div>

The fs.status is always 0 in both cases if SD card inserted or removed

The fsStatus checks if the sdcard is init or not.

`int AmebaFatFS::status(void)`  
`{`  
`return fatfs_sd_is_inited();`  
`}`

I am using arduino sdk 4.0.9  
ArduinoIDE 2.3.6

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 12, 2025, 2:31am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/7 "2025-11-12T02:31:41Z")

</div>

That is odd, after `fs.begin();` if you check `fs.status()`

- if SD card inserted, get “1“
- if SD card removed, get “0“

Could you provide the log of trying [basic demo](https://github.com/Ameba-AIoT/ameba-arduino-pro2/blob/dev/Arduino_package/hardware/libraries/FileSystem/examples/GetFreeAndUseSpace/GetFreeAndUseSpace.ino) by updating following code

`while (fs.status() != 1) {`  
`fs.begin();`  
`}`

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 12, 2025, 2:52am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/8 "2025-11-12T02:52:13Z")

</div>

> **[Releases · Ameba-AIoT/ameba-arduino-pro2](https://github.com/Ameba-AIoT/ameba-arduino-pro2/releases/)**
>
> AmebaPro2 Arduino third-party package SDK. Contribute to Ameba-AIoT/ameba-arduino-pro2 development by creating an account on GitHub.

And could you try the latest early release version?

[https://github.com/Ameba-AIoT/ameba-arduino-pro2/raw/dev/Arduino\_package/package\_realtek\_amebapro2\_early\_index.json](https://github.com/Ameba-AIoT/ameba-arduino-pro2/raw/dev/Arduino_package/package_realtek_amebapro2_early_index.json)

thanks.

---

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 12, 2025, 6:16am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/9 "2025-11-12T06:16:54Z")

</div>

Thanks for replying **[M-ichae-l](https://forum.amebaiot.com/u/m-ichae-l)**  
Is it a stable release?

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 12, 2025, 6:45am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/10 "2025-11-12T06:45:31Z")

</div>

For the [early release](https://github.com/Ameba-AIoT/ameba-arduino-pro2/releases/tag/V4.1.0-QC-V05) version, it may not stable as official release version.

But we can use it for current debugging and will QC and release it ASAP.

---

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 12, 2025, 1:52pm UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/12 "2025-11-12T13:52:20Z")

</div>

The issue persists in latest rellease

---

<div class="post-metadata">

**Author:** ![DevomechSolutions](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@DevomechSolutions](https://forum.amebaiot.com/u/DevomechSolutions)\
**Post date:** [November 13, 2025, 11:17am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/13 "2025-11-13T11:17:46Z")

</div>

Hi @M-ichae-l  
I have checked the sd card status after fs.begin(); and its working in this scenario. but we would like to check it before fs.begin();

is there a way to check it before?  
also we have updated the sdk to the pre release version 4.1.0.

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 14, 2025, 10:09am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/14 "2025-11-14T10:09:24Z")

</div>

> [@M-ichae-l](#):
>
> fs.status()

you can use the “status()” before the “begin“

SD card must “begin“ in order to be used, and “status()” is to check if proceed to “begin“.

But if there is no “begin“, no matter does/does not insert the SD card, it is always “0“.

In this case, please allow me more time to investigate both SW and HW solution.

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 17, 2025, 8:57am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/15 "2025-11-17T08:57:20Z")

</div>

You may want to refer to [HDK](https://www.amebaiot.com/en/ameba-arduino-summary/), if want to get it from HW point of view.

The EVB AMB82-mini has the SD card socket circuit refer to the following pic.

 ![2025-11-17_16-53-30](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/3/3f26b17371d7a47bdf38596b9b2ac446b4c503aa.png)

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 17, 2025, 11:36am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/16 "2025-11-17T11:36:11Z")

</div>

There is a pin “SD\_CD“ it is low when there is SD card connected and high when is not. It can be used for the indicator before the sdio init.

But SD\_CD pin is critical, so that there is no HW pin out AMB82-mini EVB or and software Arduino API to direct control it.

Therefore, I will suggest to use lower level API to achieve the function we wanted. Please refer to my following test code.

```auto
#include "gpio_api.h"
#include "gpio_ex_api.h"
#include "AmebaFatFS.h"

AmebaFatFS fs;
gpio_t gpio_cd;
int gpio_test = 1;

void setup()
{
}

void test()
{
    gpio_init(&gpio_cd, PS_4);
    gpio_dir(&gpio_cd, PIN_INPUT);
    gpio_mode(&gpio_cd, PullNone);  
    gpio_test = gpio_read(&gpio_cd);

    if (gpio_test == 0) {
        gpio_deinit(&gpio_cd);
        printf("-------Have sd card!!!! \r\n");
        fs.begin();

        uint64_t used_bytes = fs.get_used_space();
        uint64_t free_bytes = fs.get_free_space();

        printReadableSize(used_bytes, "Used space on SD card");
        printReadableSize(free_bytes, "Free space on SD card");

        fs.end();
    } else {
        gpio_deinit(&gpio_cd);
        printf("-------No sd card!!!! \r\n");
    }
}

void loop()
{
    test();
    delay(1000);
}

void printReadableSize(uint64_t bytes, const char* label)
{
    double value = (double)bytes;
    const char* unit = "B";

    if (value >= 1024.0) {
        value /= 1024.0;
        unit = "KB";
    }
    if (value >= 1024.0) {
        value /= 1024.0;
        unit = "MB";
    }
    if (value >= 1024.0) {
        value /= 1024.0;
        unit = "GB";
    }

    printf("%s: %llu bytes (%.2f %s)\r\n", label, bytes, value, unit);
}

```

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [November 20, 2025, 7:36am UTC](https://forum.amebaiot.com/t/can-we-detect-the-sd-card-if-not-present/4497/17 "2025-11-20T07:36:30Z")

</div>

The test code provided is only a temporary solution, so please make sure to test it thoroughly before using it - especially when switching between GPIO and SDIO multiple times within a short period.

If you encounter any further issues, please let me know and we can discuss them here.
