SD Card Reader example not working

Hi,

I’ ve been trying to get an SD card reader working with the SD library. After lots of reading of forum articles I can’t find anything that solves the problem I have. Having struggled with my own code I went back to run the examples and to see which of them work.

The CardInfo example works and the Files example seems to work however the ReadWrite example fails. The library will not write successfully to an SD card. I have tried two different readers and four different SD Cards - they all give the same problem. The SD card ReadWrite example code is (Note this is not my code but the code that comes with the 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(10)) {
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
}

After an attempt to write to the SD card there some weird looking file names. Running the CardInfo example after a readwrite gives the following:

Initializing SD card…Wiring is correct and a card is present.

Card type: SD1
Clusters: 490848
Blocks x Cluster: 8
Total Blocks: 3926784

Volume type is: FAT32
Volume size (Kb): 1963392
Volume size (Mb): 1917
Volume size (Gb): 1.87

Files found on the card (name, date and size in bytes):
B⸮@)0 .t⸮ 2016-00-23 12:02:48 1082158849
=⸮@`qp.⸮/ 2022-04-16 12:02:04
⸮AQ ⸮".⸮⸮ 1982-00-24 10:01:00 1099173953
PA.⸮A/ 1981-00-05 00:32:48
A⸮AP 1988-02-01 00:16:10 68702240
A⸮AP. 2012-12-04 02:02:02 33883160
A⸮AP.A/ 2020-01-00 08:12:08
⸮AP.A⸮ 1982-00-24 10:01:00 1099173953
PA.⸮A/ 1981-00-05 00:32:48
A⸮AP 1988-02-01 00:16:10 1511936032
⸮⸮P⸮Qd.J⸮R 1989-01-01 08:04:00 2147483906
@⸮$

I’m sure others have had this problem given some of the stuff I have read in the forum - however I cannot find a solution.

Any help will be gratefully received.

Thanks.

Could you clarify which Arduino you are using, and which SD card module?

I have solved my problem. I found an article which mentioned that the SD Card Readers didn't work on 5v drawn from the Arduino and that 3.3v should be used. Once I knew the right question to ask I found a number of articles on the topic.

I modified my ciruit such that 3.3v could be used to power the SD Card and used a voltage divider to ensure the SPI signals from the ardunio were at about 3.3v (I'll probably use a level converter in the final solution). The net result is that files can be successfully written to the SD Card.

Why it won't work with 5v is unclear ... but at least I can move forward.

(I am using an Arduino Uno (CH340) and a SD card reader with LC Technology on the back of the board).

If this is a full-size SD card module, it probably looks like the one in the attached picture. If so, it has a 3.3V regulator on board, so you should be able to power it with 5V applied to the “5V” header pin, and you should then see 3.3V on the “3.3V” header pin.

But that does nothing to match the 5V SPI lines coming from the processor to the 3.3V pins of the SD card.

What’s shown in the picture is a fix I’ve used successfully. You cut three traces on the back of the module, and solder diodes between the MOSI, SCK and CS pins of the SD card and their respective header pins. Since the module has pullup resistors on those lines, the processor only needs to ground a line to bring it low. The diode keeps it from bringing the pin up to 5V, but the pullup resistor takes care of that - pulling it up to 3.3V.

Alternatively, you could use this module, which appears to have a level translator on the back side:

Thanks for you comments and schematic (very helpful) - I am going to give it try and see what happens.

Hi,
It is not correct to use a diode separation scheme.
The SD card does not have a z-output, so it cannot work on the SPI bus simultaneously with another device.
For correct operation, you need to share MISO via buffer / line driver / 3-state / for example 74HCT125 (74LV125).
There are errors everywhere in China, so you cannot use more than one device, since the SD card at the MISO output is low.
When you use the correct MISO output through element 74LVC125, you will have three more elements free. They can be used for MOSI, CLK, CS.
Such a scheme will be correct and will allow working with other devices on the SPI bus (I use the ATMega2560 + W5100 (W5500) + MCP2515 (CAN) + FM25CL64 + SD card scheme and everything works). If there is such a microcircuit on the board, you need to fix the connection (as in the diagram, attached) or connect the SD card via 74LV125 (Quad buffer / line driver / 3-state / 3.3v).

As an add-on, I have the same module for a large SD card. I did not use diodes for decoupling and everything works fine for me when there is only one module on the SPI bus (+ 5v ATMega2560 or UNO boards). When used in a project together with other SPI devices, the work was random for all modules. Then I took an oscilloscope and studied the signals. SD card signal (MISO) does not have z-out. I have used a 74HC125 and have had no problems for a long time (SD card Mirex SDHC 8Gb class 10). Best regards.

Thanks for your comments - they are very helpful.

As an addition, remember

void setup(){
digitalWrite (10, HIGH);  // disable w5100
digitalWrite (4, HIGH);   // disable SD card
pinMode (9, OUTPUT);      // activate the SPI OUT mode
pinMode (10, OUTPUT);     // CS pin for W5100
pinMode (4, OUTPUT);      // CS pin for SD card
}

or

void setup(){
digitalWrite (10, HIGH); // disable w5100
digitalWrite (9, HIGH);  // disable SD card
pinMode (9, OUTPUT);     // CS pin for SD card (+activate the SPI OUT mode)
pinMode (10, OUTPUT);    // CS pin for W5100
}

I also added smd ceramic capacitors
for 5v 47uF (near the reset button) - 1 piece.
for 3.3v 47uF - 2 pcs. (soldered on top of another).
If it works for you, you are great, otherwise look for an error.
Best regards.

another external SD card:
pin13 for 74VLC125 disconnect from GND & connect pin CS

When many and I had a problem with connecting an SD card on the W5100 board, I have been looking for the right libraries for a long time to get both devices working together. It always worked badly, unstable. And now I really needed to make the SD card work on the W5100 board, so I started to study exchange signals with an oscilloscope. I made sure that the SD card does not have a z-out, and therefore very much depends on who wins the W5100 or SD. Now that I know this, a 74HCT125 (74LVC125) is required. Where to install it? On the W5100 board there is a 74LVC14 where one element is used, and the rest are free. I removed the 74LVC14 and installed the 74HCT125 using the Z output as an inverter. So I wrote a post above to help others fix the problem.

But why do I need an inverter (1 element from a whole microcircuit), I thought? And I began to look for documentation to study the W5100 chip how it works. Latest WIZnet documentation version 1.1.8 for W5100. There is little written and there is no necessary. Then I decided to experiment. I just removed the 74LVC14 from the board and the LAN board worked! What a miracle! Then I started examining the MISO signal and realized that the chip manufacturer had made a mistake. W5100 no Z output on MISO when CS signal is turned off!

When we use an inverter on the SEN pin (pin 31 W5100), then the MISO output has a Z state. But then SPI is disabled and Direct Bus Interface mode is enabled. (In order to read or write the data on the sequential address, set AI bit of MR (Mode Register). With this, user performs above 1 only one time. Whenever read or write IDM_DR, IDM_AR, the value is automatically increased by 1). Anyone interested can study page 60 leadership.

Therefore, the probability of failures with such a scheme for switching the operation of the SPI / Direct Bus interfaces is very high. And those who have the W5100 for Arduino are just accidentally lucky. You can check for yourself that if you just remove the 74LVC14AD chip from the board, then the W5100 shield without SD card will continue to work!

Now that we know why there is a poor performance of the W5100, it is the fault of the 74LVC14AD which needs to be replaced with the 74LVC125 (I used what I found 74HCT125, it also works at 3.3v).

We use one element 74LVC125 to disable the MISO W5100 and the second element for the MISO SD card.

As I did, see the photo.

  1. Blown away 74LVC14AD
  2. Remove the jumpers pin 1; 3; 5; nine; 11 from GND and cut the pin 12 track (I used UNO not to bend the pins)
  3. You need to ring, all contacts must be disconnected (be in the air), except pin: 7; 13 & 14.
  4. Install the 74LVC125 (74HCT125) on the board and solder all the pins.
  5. We connect the necessary signals with wires, “MISO SPI OUT board” must be connected through the z-output elements.
  6. We connect and test. If everything works, then you can wash and use hot melt glue.
  7. I used additional ceramic SMD capacitors of 47 uF for + 5V and + 3.3V to improve the power supply.

Ok, everything works great!
Now you can use SPI.setClockDivider (SPI_CLOCK_DIV2); and card.init (SPI_FULL_SPEED, pin_SD);

Anyone who has such boards and problems with them should know about this.
Best regards.

You can use my code for testing:

Test_W5100_SD_upgrade.zip (2.62 KB)

result

74HCT125D_for_W5100.jpg