How to use micro SD card module with arduino zero

Hi, I want to control the sd card module through arduino zero not MKR Zero. However, when I connect the wires and write the code in the same way as the Arduino Uno, it does not work. The lines are connected like this: MISO -> D12, MOSI -> D11, SCK -> D13, CS -> D10 and It stop at "while(!SD.begin(10));". How to solve it?

#if !( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
      || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \
      || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) \
      || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD51__) || defined(__SAMD51J20A__) \
      || defined(__SAMD51J19A__) || defined(__SAMD51G19A__) || defined(__SAMD51P19A__)  \
      || defined(__SAMD21E15A__) || defined(__SAMD21E16A__) || defined(__SAMD21E17A__) || defined(__SAMD21E18A__) \
      || defined(__SAMD21G15A__) || defined(__SAMD21G16A__) || defined(__SAMD21G17A__) || defined(__SAMD21G18A__) \
      || defined(__SAMD21J15A__) || defined(__SAMD21J16A__) || defined(__SAMD21J17A__) || defined(__SAMD21J18A__) )
  #error This code is designed to run on SAMD21/SAMD51 platform! Please check your Tools->Board setting.
#endif

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

// These define's must be placed at the beginning before #include "SAMDTimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG         0
#define _TIMERINTERRUPT_LOGLEVEL_     1

// Select only one to be true for SAMD21. Must must be placed at the beginning before #include "SAMDTimerInterrupt.h"
#define USING_TIMER_TC3         true      // Only TC3 can be used for SAMD51
#define USING_TIMER_TC4         false     // Not to use with Servo library
#define USING_TIMER_TC5         false
#define USING_TIMER_TCC         false
#define USING_TIMER_TCC1        false
#define USING_TIMER_TCC2        false     // Don't use this, can crash on some boards

// Uncomment To test if conflict with Servo library
//#include "Servo.h"

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

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "SAMDTimerInterrupt.h"
#include <SPI.h>
#include <SD.h>
#define TEST_PIN                 9
#define TIMER_INTERVAL_US        500


#if USING_TIMER_TC3
  #define SELECTED_TIMER      TIMER_TC3
#elif USING_TIMER_TC4
  #define SELECTED_TIMER      TIMER_TC4
#elif USING_TIMER_TC5
  #define SELECTED_TIMER      TIMER_TC5
#elif USING_TIMER_TCC
  #define SELECTED_TIMER      TIMER_TCC
#elif USING_TIMER_TCC1
  #define SELECTED_TIMER      TIMER_TCC1
#elif USING_TIMER_TCC2
  #define SELECTED_TIMER      TIMER_TCC
#else
  #error You have to select 1 Timer  
#endif

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

SAMDTimer ITimer(SELECTED_TIMER);
File myFile;

volatile uint32_t idx=0;
volatile uint8_t flag=0;
/*   0: ADC
 *   1: save
*/

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

void TimerHandler(){
  static bool toggle = false;
  digitalWrite(TEST_PIN, toggle);
  toggle = !toggle;
  flag |= 1;
}

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

void setup(){
  pinMode(TEST_PIN, OUTPUT);
  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_US, TimerHandler)){
    ITimer.stopTimer();
    digitalWrite(TEST_PIN, HIGH);delay(1000);
    digitalWrite(TEST_PIN, LOW);delay(1000);
    digitalWrite(TEST_PIN, HIGH);delay(1000);
    digitalWrite(TEST_PIN, LOW);
  }else{
    digitalWrite(TEST_PIN, HIGH);
    while(1);
  }
  analogReadResolution(12);
  while(!SD.begin(10));  
  ITimer.restartTimer();
}

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

void loop(){
}

I use this module.
sd

Have you tried using the SPI pins on the ICSP header ?

image

1 Like

It also does not work.

Have you tried the SD card examples that come with IDE?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project.

It still doesn't work. SD.begin keeps failing to read SD card.

Are you applying 5V power to the module's Vcc pin?

Is your SD card either SD or SDHC, but not SDXC?

Just started playing with Arduino myself.
Thanks @UKHeliBob for hint to use 6 pin connection on that board :). 5 pins from SDBoard goes there directly:
VCC/GRN/MOSI/MISO/SCT but CS (chip select) connected to pin 4 (8 worked as well) on Ardiuno Zero board and Sample code from Arduino IDE started to work.

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