Go Down

Topic: Problems using ethernet and SD card (Read 2099 times) previous topic - next topic


Hi I'm getting in twist switching between talking to the SD Card and the W5100 chip. This simple sketch is supposed to get some data from the Analog inputs and write it to a file on the card AND output it to a web client. I thought that the libary functions were supposed to handle the CS/SS lines but I cant get the thing to work. I've even thrown in some 'digitalWrite(ChipSelectPin,HIGH); or LOW lines to force the issue. But I just get Garbage scrolling across the serial screen and nothing over the intranet. Individually the two halves work. If I take out the Client.print bit then I can write to the card and visa versa send info to the client if datafile.print part is zapped.

This is really annoying as I want to be able to hold most of the web page info on the SD card as well as a datafile using unfo from the analoginputs. The final pages will be a mix of static HTML selective reads of the data and some NTP info gleaned by UDP. I've looked at the CAT on the MAT bit of Tom Igoe's 'Making Things Talk' several times but I'm dammed if I can see why my sketch doesn't behave.


You should use the chipSelect variable (pin 4) since you went to all that trouble. Pin 10 is the ethernet SS pin.
Code: [Select]
  digitalWrite(ethrSelect, HIGH); // Master mode
  Serial.print("Initializing SD card...");

if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");

This should be under another part of the forum, like networking or storage.


Switching the SD line 'if (!SD.begin(...'  to pin 10 was a deliberate act not a typo and is an indication of how frustrated Ive got with the thing!

The sketch started off with the right line 'if (!SD.begin(chipSelect)) {... but that did not work!!!
even forcing the chip selects on and off, which ought to be handled by '<Ethernet.h>  and ,SD.h>.


Jun 24, 2012, 11:27 am Last Edit: Jun 24, 2012, 02:53 pm by SurferTim Reason: 1
The sketch started off with the right line 'if (!SD.begin(chipSelect)) {... but that did not work!!!
even forcing the chip selects on and off, which ought to be handled by <Ethernet.h>  and <SD.h>.

Once the two interfaces are set up, the libraries do handle the SS pins. You do not need to manipulate the SS pins after both begin() functions are complete. It is up to you to avoid data collisions on the SPI bus until then.

edit: Does this work for you?
Code: [Select]
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {

  // disable w5100 while setting up SD

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) Serial.println("failed");
  else Serial.println("OK");
  // the SD.begin(4) function returns with its SPI disabled, so nothing needs to be done

  // set up w5100 with dhcp
  Serial.print("Initializing ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println("ok");

  // the Ethernet.begin(mac) function returns with its SPI enabled, so disable it

void loop() {

Go Up