Program Stuck at SD.begin(4)

Hello guys,

Just starting to use SD.h library to access my SD card. I tried many examples and all work just fine.
Finally i tried to use sd library to my program, but program stuck to void setup and nn particular to “SD.begin(4)” command.

As you can see to my program i use serial.println for debugging, and at my screen while program starts i see only “Initializing SD card…”

I use an arduino uno and an ethernet shield.

Any ideas? Thanks in advance!

#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
IPAddress dns1(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
EthernetServer server(80);  // create a server at port 80

//NTP Info
IPAddress timeServer(132, 163, 4, 101); //
const int timeZone = 3;     // Central European Time
unsigned int localPort = 8888;  // local port to listen for UDP packets
EthernetUDP Udp;

String HTTP_req;          // stores the HTTP request
boolean LED_status = 0;   // state of LED, off by default
boolean RELAY_status = 0; // state of Relay, off by default
boolean RELAY2_status = 0; // state of Relay, off by default
float temp;
int tempPin = 0;

void setup()
    Ethernet.begin(mac, ip, dns1, gateway, subnet);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    digitalWrite(10, HIGH);
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    Serial.println("SUCCESS - Found index.htm file.");

    pinMode(2, OUTPUT);       // LED on pin 2
    pinMode(3, OUTPUT);       // LED on pin 2
    pinMode(5, OUTPUT);       // LED on pin 2

myprogram.ino (11 KB)

Try disabling the SD card before starting the w5100. Add these two lines at the start of your setup function.

void setup() {
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

I have no problem using Ethernet and SD card at the same. They seem to take care of disabling the respective hardware after they function calls just fine.

Just to get a few simple questions answered: 1) Is there anything connected to pin 4 other than the SD card? 2) Is the CS pin of the SD card connected to pin 4? 3) What SD shield or module are you using? Give a link.

FYI the arduino Ethernet shield/library assumes pin 10 is used to select the Ethernet chip, regardless what arduino board. This better not conflict with SD card's chip select.


SurferTim, i tried it but nothing change

Liudr: I have the official ethernet shield ( Also nothing connected to pin 4 and pin 10.

As i said before, simpler examples work just fine with no problem. ?nother observation is that at serial screen i dont get the full message “Initializing SD card…”, but i only see “Initializing SD ca”. Some characters are missing! (check attach file)

It appears your Arduino is restarting for some reason. Hard to tell from here. Does the w5100 and SD card work ok separately?

Do you have anything in loop() ? loop() will execute even if the SD fails.

Try replacing these return statements in startup() with a forever loop. And add a Serial.flush. This won’t solve the problem but may allow the entire message to print.

 if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
       for (;;);    // init failed  <-----------------------------------forever loop
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        for (;;);  // can't find index file <---------------------------forever loop
    Serial.println("SUCCESS - Found index.htm file.");
    Serial.flush();  // <-------------------------------------------flush serial print data

I actually sat down and read your attached program. You are using a huge amount of print with bits of webpages. That drains down your SRAM and causes crash when more memory is requested to start SD card. Try this: print(F("here is a bit of webpage")). The FLASH helper F() saves your SRAM. The ultimate solution is to print out a static webpage off your sd card and leaving dynamic contents to javascript.

(Curse forum, I had to type this response twice as my first response was lost in their silly problems.)