SD Card Module to Arduino UNO

Hello,

I have the MicroSD Breakout with Level Shifter (MicroSD Breakout With Level Shifter Hookup Guide - SparkFun Learn) and Arduino UNO Wifi Rev 2 with me.

I was trying to code the SD module with the following code but doesn't seem to recognize my SD card module (SD card is installed).

Code is:

// Include the following libraries (C++ language code)

#include <SD.h>                                     // Needed for the SD module
#include <Wire.h>                                   // Needed for I2C to GNSS (Activcate any wires connected to the Arduino
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>   // Library can be found here: http://librarymanager/All#SparkFun_u-blox_GNSS
SFE_UBLOX_GNSS myGNSS;                              // Assigning SFE_UBLOX_GNSS variable from SD library to myGNSS (avoid any overrides)

#define PIN_SPI_CS 4                                // Read and write pin is assigned on pin 4

long lastTime = 0;                                  // Simple local timer. Limits amount if I2C traffic to u-blox module.
File myFile;                                        // Assigning File variable from SD library to myFile (avoid any overrides)


// Set up

void setup()
{
  Serial.begin(57600);
  while (!Serial)
    ; //Wait for user to open terminal
 
  if (!SD.begin(PIN_SPI_CS)) 
  {
    Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
    while (1); // don't do anything more:
  }

  Serial.println(F("SD CARD INITIALIZED."));
  
  Wire.begin();

  if (myGNSS.begin(Wire) == false)          //Connect to the u-blox module using Wire port
  {
    Serial.println(F("Please check wiring. Freezing."));
    while (1);
  }
  myGNSS.setI2COutput(COM_TYPE_UBX);        //Set the I2C port to output UBX only (turn off NMEA noise)
   myGNSS.setNavigationFrequency(20);       //Set output to 20 times a second
}

// Gathering data

void loop()
{
  // Query module only every minute. Doing it more often will just cause I2C traffic.
  
  if (millis() - lastTime > 60000)
  {
    lastTime = millis(); //Update the timer
  
 
   File dataFile = SD.open("zedf9p.txt", FILE_WRITE);
   
   // if the file is available, write to it:
   
  if (dataFile) {

 ///////////////////
    
    // Print date and time in the Serial Monitor
    Serial.print(myGNSS.getYear());
    Serial.print(F("-"));
    Serial.print(myGNSS.getMonth());
    Serial.print(F("-"));
    Serial.print(myGNSS.getDay());
    Serial.print(F(","));
    Serial.print(myGNSS.getHour());
    Serial.print(F(":"));
    Serial.print(myGNSS.getMinute());
    Serial.print(F(":"));
    Serial.print(myGNSS.getSecond());
    Serial.print(F(","));

    // Print date and time in the SD card
    dataFile.print(myGNSS.getYear());
    dataFile.print(F("-"));
    dataFile.print(myGNSS.getMonth());
    dataFile.print(F("-"));
    dataFile.print(myGNSS.getDay());
    dataFile.print(F(","));
    dataFile.print(myGNSS.getHour());
    dataFile.print(F(":"));
    dataFile.print(myGNSS.getMinute());
    dataFile.print(F(":"));
    dataFile.print(myGNSS.getSecond());
    dataFile.print(F(","));

 ///////////////////
 
    // Collect the position data
    
    int32_t latitude = myGNSS.getHighResLatitude();
    int8_t latitudeHp = myGNSS.getHighResLatitudeHp();
    int32_t longitude = myGNSS.getHighResLongitude();
    int8_t longitudeHp = myGNSS.getHighResLongitudeHp();
    uint32_t accuracy = myGNSS.getHorizontalAccuracy();

    // Defines storage for the lat and lon units integer and fractional parts
    
    int32_t lat_int;                              // Integer part of the latitude in degrees
    int32_t lat_frac;                             // Fractional part of the latitude

    int32_t lon_int;                              // Integer part of the longitude in degrees
    int32_t lon_frac;                             // Fractional part of the longitude
   
     
    // Calculate the latitude and longitude integer and fractional parts
    
    lat_int = latitude / 10000000;                // Convert latitude from degrees * 10^-7 to Degrees
    lat_frac = latitude - (lat_int * 10000000);   // Calculate the fractional part of the latitude
    lat_frac = (lat_frac * 100) + latitudeHp;     // Now add the high resolution component
    if (lat_frac < 0)                             // If the fractional part is negative, remove the minus sign
    {
      lat_frac = 0 - lat_frac;
    }
    
    lon_int = longitude / 10000000;               // Convert latitude from degrees * 10^-7 to Degrees
    lon_frac = longitude - (lon_int * 10000000);  // Calculate the fractional part of the longitude
    lon_frac = (lon_frac * 100) + longitudeHp;    // Now add the high resolution component
    if (lon_frac < 0)                             // If the fractional part is negative, remove the minus sign
    {
      lon_frac = 0 - lon_frac;
    }


    // Print Lat and Long data in the Serial Monitor

    Serial.print(lat_int);                         // Print the integer part of the latitude
    Serial.print(".");
    printFractional(lat_frac, 9);                  // Print the fractional part of the latitude with leading zeros
    Serial.print(",");
    Serial.print(lon_int);                         // Print the integer part of the latitude
    Serial.print(".");
    printFractional(lon_frac, 9);                  // Print the fractional part of the latitude with leading zeros

 ///////////////////
 
    // Collect accuracy data
    
    float f_accuracy;

    // Convert the horizontal accuracy (mm * 10^-1) to a float
    f_accuracy = accuracy;
    // Now convert to m
    f_accuracy = f_accuracy / 10000.0;              // Convert from mm * 10^-1 to m

    // Print accuracy data in the Serial Monitor
    
    Serial.print(",");
    Serial.println(f_accuracy, 4);                  // Print the accuracy with 4 decimal places

 ///////////////////

    // Collect altitude, altitude (msl) and fix type. Then, print data in the Serial Monitor.
    
    long altitude = myGNSS.getAltitude();
    Serial.print(F(" Alt: "));
    Serial.print(altitude);

    long altitudeMSL = myGNSS.getAltitudeMSL();
    Serial.print(F(" AltMSL: "));
    Serial.print(altitudeMSL);
    Serial.print(F(" (mm)"));

    byte fixType = myGNSS.getFixType();
    Serial.print(F(" Fix: "));
    if(fixType == 0) Serial.print(F("No fix"));
    else if(fixType == 1) Serial.print(F("Dead reckoning"));
    else if(fixType == 2) Serial.print(F("2D"));
    else if(fixType == 3) Serial.print(F("3D"));
    else if(fixType == 4) Serial.print(F("GNSS + Dead reckoning"));
    else if(fixType == 5) Serial.print(F("Time only"));
    Serial.println();

 ///////////////////
 
    // Print position, accuracy, altitude, altitude (msl) and fix type in the SD card
  
    dataFile.print(lat_int);                // Print the integer part of the latitude
    dataFile.print(".");
    dataFile.print(lat_frac);               // Print the fractional part of the latitude with leading zeros
    dataFile.print(",");
    dataFile.print(lon_int);                // Print the integer part of the latitude
    dataFile.print(".");
    dataFile.print(lon_frac);               // Print the fractional part of the latitude with leading zeros
    dataFile.print(",");
    dataFile.print(f_accuracy, 4);          // Print the accuracy with 4 decimal places
    dataFile.print(",");
    dataFile.print(altitude);               // Print the altitude in mm
    dataFile.print(",");
    dataFile.print(altitudeMSL);            // Print the altitude (msl) in mm
    dataFile.print(",");
    dataFile.print(fixType);                // Print the Fix type (0=No fix, 1=Dead reckoning, 2=2D, 3=3D, 4=GNSS+Dead reckoning, 5=Time only)
    dataFile.println("");
    dataFile.close();
 
  }
  }
}

///////////////////
 
// Pretty-print the fractional part with leading zeros - without using printf
// (Only works with positive numbers)

void printFractional(int32_t fractional, uint8_t places)
{
  if (places > 1)
  {
    for (uint8_t place = places - 1; place > 0; place--)
    {
      if (fractional < pow(10, place))
      {
        Serial.print("0");
      }
    }
  }
  Serial.print(fractional);
}

I have the following connections on my unit:

Picture1
plus the CS pin on Arduino pin 4

Is there any way I can get help with this?

Thank you.

run the CardInfo diagnostic example

Here are the following results:

13:12:26.366 -> Initializing SD card...initialization failed. Things to check:
13:12:26.412 -> * is a card inserted?
13:12:26.460 -> * is your wiring correct?
13:12:26.460 -> * did you change the chipSelect pin to match your shield or module?

Card is inserted, chose the correct chipSelect pin and have followed the wiring I have provided above.

you didn't provide the wiring in your post.
do you have MISO to DO and MOSI to DI?

Have you formatted the SD with the proper formatter?

@Nick_Pyner the library can't communicate with the card. at this point it doesn't matter if the card is formatted



Here are the connections I have.

I see the cyan wire go from MISO to DI and brown from MOSI to DO

I have switched them but still no changes. Do you have an idea where I should plug in the CD pin to the arduino? Thanks.

CD is card detect. just a contact in the holder. you can wire it as button to any pin if you want to test in sketch if the card is inserted.
how is Vcc wired?

VCC and GND are wired to the following:
Picture1
. Breadboard is connected to the Arduino's "5V" and "GND" since I am using another sensor (for GNSS)

Are you sure it gets power from the breadboard rail?
there are two 5 V pins on Uno. one on the power header and one on the SPI header.
try it with 5 V pin of Uno directly powering the SD card.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.