Problems with SD card and SD card module

Im trying to connect SD card module (pictures in the att.) to Arduino UNO and upload example code Write/Read from SD library.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

For some reason I almost always get failed initialization on serial monitor. I bought 6 of these modules, I have 10 SD cards and few Unos, tried many combinations and never could get a proper reading except one time when it worked but after half an hour again it doesent work. I have no idea what the problem is. Any ideas?

I think there may be two problems.

First, try changing the CS line to the D10 pin, and change the code accordingly, and see if that works:

if (!SD.begin(4)) {

if (!SD.begin(10)) {

Second, the SD modules you are using are not right for use with a 5V Arduino. They are for 3.3V processors. All SD cards are 3.3V devices, but I believe those modules allow 5V from the Arduino to reach the SD card, and that shouldn’t happen. I’ll attach the schematic of a similar module, and you might want to confirm that it also applies to yours.

I don’t know where to buy a full-size SD module that’s designed correctly, but I’ll attach a picture of how I “fixed” mine. It’s basically cutting the traces of three lines at the header, and connecting the header directly to the holder pins with diodes. The idea is that the Uno can bring the lines low, but cannot bring them high (that’s handled by the pullup resistors on the module, or in the case of CS, the pullup resistor inside the SD card).

I take it back. It looks like this SD module is correct. I haven't tried it, but it has a level-shifter IC, which is what you need when working with 5V Arduinos.

I should also say that the typical Ebay microSD module does have the level shifter IC. So if you ever switch to microSD, the voltage issue would go away.

If you take a glance at sd2 picture I attached in my original post you can see the elemnts on these module which IC chip is I believe voltage regulator. Also the board has both 5V and 3.3V pins maybeone goes to regulator and other one directly to SD pin? I ve tried both d3 and d10, same thing happens,sometimes they work perfectly other times cards fail to initialize. Ive found that another schematic where you hook up SD card directly to Arduino with just a few voltage dividers and one 10k resistor works fine but the problem is I have to solder pins to SD card and after that I cant use it on the SD card reader. another problem is that I really want to find out why this is happening to this modules would be a shame to throw them out :slight_smile:

Yes, the three-pin I/C is the 3.3V regulator. The 5V pin at the header feeds the input of the regulator, and the output of the regulator feeds the SD card holder’s Vcc pin. But you can also provide 3.3V at the 3.3V header, which will feed the Vcc pin directly, bypasssing the regulator. Either way, you provide 3.3V to the SD card’s Vcc pin which is what it must have.

But the resistors R1-R4 are just pullup resistors to Vcc for the signal lines coming from the Arduino. Those lines are at 5V, and go directly to the MOSI, CLK and CS pins of the SD card. The pullup resistors have very little effect, and the voltage remains at 5V. This is substantially in excess of the permitted voltage on those pins when the card is being powered by 3.3V.

The only thing I can suggest is that you modify one of your modules, and see if it works then. You would need three small signal diodes such as 1N4148, installed as shown in my “fix” picture. And you would need to cut the existing traces as close as possible to the headers so the pullup resistors will still work. I’ll attach a schematic showing the modifications. Then if that works, you can modify the others the same way.

I can’t guarantee this modification will solve your problem. The other possibility is that the 3.3V coming out of the regulator is sagging. You could use your meter to see if that’s happening.

Im Having a similar issue writing a sound file to sd card at 38.5 Khz
it misses data about every 6.5ms
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;

struct soundhdr {
char riff[4]; /* “RIFF” /
long flength; /
file length in bytes /
char wave[4]; /
“WAVE” /
char fmt[4]; /
"fmt " /
long chunk_size; /
size of FMT chunk in bytes (usually 16) /
short format_tag; /
1=PCM, 257=Mu-Law, 258=A-Law, 259=ADPCM /
short num_chans; /
1=mono, 2=stereo /
long srate; /
Sampling rate in samples per second /
long bytes_per_sec; /
bytes per second = srate*bytes_per_samp /
short bytes_per_samp; /
2=16-bit mono, 4=16-bit stereo /
short bits_per_samp; /
Number of bits per sample /
char data[4]; /
“data” /
long dlength; /
data length in bytes (filelength - 44) */
} wavh;
//variable to store incoming audio sample
int timer=0;
int flushing=0;
//clipping indicator variables
boolean clipping = 0;
boolean writing = 1;
unsigned int incomingAudio;
File wavFile;
void setup(){

//pinMode(13,OUTPUT);//led indicator pin
// Setup Wave header
strncpy(wavh.riff,“RIFF”,4);
strncpy(wavh.wave,“WAVE”,4);
strncpy(wavh.fmt,"fmt ",4);
strncpy(wavh.data,“data”,4);
wavh.flength=1155044;
// size of FMT chunk in bytes
wavh.chunk_size = 16;
wavh.format_tag = 1; // PCM
wavh.num_chans = 1; // mono
// This is easier than converting to hex and then to bytes :slight_smile:
wavh.srate = 38500;
wavh.bytes_per_sec=77000;
wavh.bytes_per_samp=2;
wavh.bits_per_samp = 16;
wavh.dlength=1155000;

SD.begin(chipSelect);
if (SD.exists(“sound.wav”)) {
SD.remove(“sound.wav”);
}
wavFile = SD.open(“sound.wav”, FILE_WRITE);
wavFile.write((byte *)&wavh,44);
wavFile.flush();
cli();//disable interrupts

//set up continuous sampling of analog pin 0

//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;

ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only

ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements

sei();//enable interrupts

//if you want to add other things to setup(), do it here
delay(15000);
writing=0;
}

ISR(ADC_vect) {//when new ADC value ready

uint8_t LowADC = ADCL;
uint8_t HiADC = ADCH;
incomingAudio = HiADC<<2|LowADC>>6;
incomingAudio = (incomingAudio-512)*64;
LowADC=incomingAudio&0xff;
HiADC=(incomingAudio&0xff00)>>8;
if (writing){
if (wavFile){
wavFile.write(LowADC);
wavFile.write(HiADC);
}
}else{
wavFile.flush();
wavFile.close();
}
}

/*
https://www.newbiehack.com/MicrocontrollersADC10Bits.aspx
*/

void loop(){
/*timer++;
if (timer >150){
//digitalWrite(13,HIGH);
writing=0;
}
delay(100);
*/
}

I have a 900hz sine wave on a0 and when writing it to an sd card it drops data about every 6.5 ms

It appears that every 512 bytes it drops about 5 to 10 bytes when writing a stream at speed