Error in Initializing Micro SD card


If you are asking a question, please follow this template:

  1. My goal is: Card reader for micro Sd
  2. My actions are: As I am using the library by nilivu it initializing only SD card, not micro SD card.

I am using esp32 and normal 16gb sandisk microsd card . with testsd ( app.


The library and the demo application work ok as long as you set the correct settings in your mos.yml, initialize the library for SPI or SDMMC interface and have the pullup resistors as stated in the
In the case where you use the SPI interface, be aware that you can’t connect another SPI device an the same bus as the SD card.

FYI, I’ve always used micro sd cards.


I am using the following code for Logging IMU data IN SD card by calling JS using ffi my main in c but I am calling it in JS as follows.
This Is main .c

// Required Header files
#include "mgos.h"
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "driver/sdspi_host.h"
#include "sdmmc_cmd.h"
#include "mgos_i2c.h"
#include "mgos_imu.h"

//****SPI2 Pin Defined Here****
#define PIN_NUM_MISO 2
#define PIN_NUM_MOSI 15
#define PIN_NUM_CLK  14
#define PIN_NUM_CS   13

int a=1; // Temp Variable(Required)

 void mpu();
static void imu_cb(void *user_data) 
	if (a == 1) //Condition for Initalize
 	{  // ****************SDMMC Initilize*******************
	LOG(LL_INFO, ("%s", "SDMMC initialize !!!!!"));
  sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  slot_config.gpio_miso = PIN_NUM_MISO;
  slot_config.gpio_mosi = PIN_NUM_MOSI;
  slot_config.gpio_sck  = PIN_NUM_CLK;
  slot_config.gpio_cs   = PIN_NUM_CS;
  esp_vfs_fat_sdmmc_mount_config_t mount_config = 
    .format_if_mount_failed = true,
    .max_files = 20,
    .allocation_unit_size = 16 * 1024

  sdmmc_card_t* card;
  esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

  if (ret != ESP_OK)
    if (ret == ESP_FAIL)
      LOG(LL_INFO, ("%s", "Failed to mount filesystem.  If you want the card to be formatted, set format_if_mount_failed = true."));
      LOG(LL_INFO, ("Failed to initialize the card (%s). ", esp_err_to_name(ret)));
  sdmmc_card_print_info(stdout, card);
  a = 0; //If card Initilie no Need To start Again..
// Structure For IMU
  struct mgos_imu *imu = (struct mgos_imu *)user_data;
  float ax, ay, az;
  float gx, gy, gz;
  float mx, my, mz;
    char filename_format[40] = "/sdcard/%d.csv";
    char filename[sizeof(filename_format) + 5];
  for (int i = 1; i <400; i++) {
  snprintf(filename, sizeof(filename), filename_format, i);
  //FILE* f = fopen(filename, "w+");
  if (!imu) return;
  // Logging Accelerometer 
   FILE* f = fopen(filename, "a"); 
   fprintf(f,"Ax,Ay,Az,Gx,Gy,Gz,Mx,My,Mz\n ");
  for (int x = 0; x <10; x++) {
   // fprintf(f,"*********************************** \n");
   mgos_imu_accelerometer_get(imu, &ax, &ay, &az);
   mgos_imu_gyroscope_get(imu, &gx, &gy, &gz);
   mgos_imu_magnetometer_get(imu, &mx, &my, &mz);
   //f= fopen("/")
    //f = fopen(filename, "a");
      //LOG(LL_INFO, ("%s", "***************************** "));
    //  LOG(LL_INFO, ("%d \n",x));
      //LOG(LL_INFO, ("type=%-10s Accel X=%.2f Y=%.2f Z=%.2f",
                 // mgos_imu_accelerometer_get_name(imu), ax, ay, az));
     // LOG(LL_INFO, ("type=%-10s Gyro  X=%.2f Y=%.2f Z=%.2f",
                //  mgos_imu_gyroscope_get_name(imu), gx, gy, gz));
     // LOG(LL_INFO, ("type=%-10s Mag   X=%.2f Y=%.2f Z=%.2f",
                 // mgos_imu_magnetometer_get_name(imu), mx, my, mz));            
     // FILE* f = fopen("/sdcard/data.txt", "a");

     fprintf(f,"X=%.2f, Y=%.2f, Z=%.2f, X=%.2f, Y=%.2f, Z=%.2f, X=%.2f, Y=%.2f, Z=%.2f, \n",
                                                  ax, ay, az, gx, gy, gz, mx, my, mz);
        //LOG(LL_INFO, ("%s", "closing file 1"));          
    } fclose(f);
    if (f == NULL)
    LOG(LL_INFO, ("%s","Writing  Failed"));
    LOG(LL_INFO, ("Writing %d",i));
// Main APP initiated              
enum mgos_app_init_result mgos_app_init(void)
void mpu(){
    struct mgos_i2c *i2c = mgos_i2c_get_global();
  struct mgos_imu *imu = mgos_imu_create();
  struct mgos_imu_acc_opts acc_opts;
  struct mgos_imu_gyro_opts gyro_opts;
  struct mgos_imu_mag_opts mag_opts;

  if (!i2c) {
    LOG(LL_ERROR, ("I2C bus missing, set i2c.enable=true in mos.yml"));
    return false;

  if (!imu) {
    LOG(LL_ERROR, ("Cannot create IMU"));
    return false;

  acc_opts.type = ACC_MPU9250;
  acc_opts.scale = 16.0; // G
  acc_opts.odr = 100;    // Hz
  if (!mgos_imu_accelerometer_create_i2c(imu, i2c, 0x68, &acc_opts))
    LOG(LL_ERROR, ("Cannot create accelerometer on IMU"));

  gyro_opts.type = GYRO_MPU9250;
  gyro_opts.scale = 2000; // deg/sec
  gyro_opts.odr = 100;    // Hz
  if (!mgos_imu_gyroscope_create_i2c(imu, i2c, 0x68, &gyro_opts))
    LOG(LL_ERROR, ("Cannot create gyroscope on IMU"));

  mag_opts.type = MAG_AK8963;
  mag_opts.scale = 12.0; // Gauss
  mag_opts.odr = 10;     // Hz
  if (!mgos_imu_magnetometer_create_i2c(imu, i2c, 0x0C, &mag_opts))
    LOG(LL_ERROR, ("Cannot create magnetometer on IMU"));
  LOG(LL_INFO, ("%s", "Writing Started !!!!!"));
   mgos_set_timer(10000, true, imu_cb, imu);
   LOG(LL_INFO, ("%s", "Writing Started !!!!!"));


let f = ffi('void mpu()');
 print('Calling C my_func:', f());


author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

# Optional. List of tags for online search.
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
  - fs
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
  - fs

  #- ["debug.level", 3]
  - ["i2c.enable", "b", true, {title: "Enable I2C"}]
  - ["i2c.unit_no", "i", 0, {title: "Which hardware unit ot use, 0 or 1"}]
  - ["i2c.sda_gpio", "i", 21, {title: "GPIO to use for SDA"}]
  - ["i2c.scl_gpio", "i", 22, {title: "GPIO to use for SCL"}]
  - ["sys.tz_spec", "EET-2EEST-3,M3.5.0/03:00:00,M10.5.0/04:00:00"]
  - ["debug.level", 2]

build_vars:                                           # Add these
        ESP_IDF_EXTRA_PARTITION: fs_ext,data,spiffs,,256K   # two lines

  - origin:
  - origin:
  - origin:
  - origin:
  - origin:
  - origin: 
  - origin:
  - origin:
  - origin:

# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

I want to use a micro sd card module
following :

but it only works with the following module:

What necessary change should I do to in.YML or IN c to work with a smaller module.


Maybe the micro sd module is not 3.3V compatible?


Sir, I am providing 5V to both modules.


If it works with one sd/micro-sd card module and it doesn’t with another one, there is certainly a hardware issue.

BTW, you are not using my sdlib as long as you included the code from the sd_card example in your main.c. Adding the sdlib in your mos.yml only pulls the necessary components to make the application build.


I found the solution for it is Resistor error the micro sd card module has pull down resistor bank . so i ordered with only pull ups.