(solved, but continued) Ethernet-Shield MicroSD lesen/schreiben


wie kann ich auf eine MicroSD, die ich ins Ethernet-Shield (http://www.ebay.de/itm/400397352919)
stecke, zugreifen? (8GB groß und FAT32 formatiert; Arduino Mega2560 R3)

PS: Die Beispiele haben nicht funktioniert.

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

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

This example code is in the public domain.


#include <SD.h>

File myFile;

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

Serial.print(“Initializing SD card…”);
// On the Ethernet Shield, CS is pin 4. It’s set as an output by default.
// Note that even if it’s not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);

if (!SD.begin(4)) {
Serial.println(“initialization failed!”);
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:
} 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) {

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

void loop()
// nothing happens after setup


Ich glaube bei diesem Shield gibts auch ein Problem damit, wenn eine MicroSD drin ist, funktioniert das Shield nicht. Ich habe ein Webserver eingerichtet, ohne der Karte läufts, sobald die Karte drin ist, ist die Verbindung unterbrochen. Aber berichte ruhig weiter, du kommst mir genau recht :D

Edit: Eben dein Code aufgespielt und dies Zeigt in der Console an:

Initializing SD card...initialization done. Writing to test.txt...done. test.txt: testing 1, 2, 3.

Verwendet wurde dabei eine 128 MB mit FAT16 Formatierung

Ich werds mal mit einer anderen MicroSD versuchen.

Danke! Habs hinbekommen. Ich denke es lag an irgendwelchen Konflikten zwischen den beiden Libraries, wie du auch schon vermutet hast.
Die 8GB Karte wird nun erkannt. Mich würde es interessieren, ob es eine Möglichkeit gibt, einen Webserver zu hosten, der html-Datein von der Karte liest.

Genau dies habe ich auch vor! Ich habe zwar ein Webserver online aber ausprobieren mit Arduino will ich es trotzdem.

Folgender Sketch sollte es ermöglichen:

* USERS OF ARDUINO 0023 AND EARLIER: use the 'SDWebBrowse.pde' sketch...
* 'SDWebBrowse.ino' can be ignored.
* USERS OF ARDUINO 1.0 AND LATER: **DELETE** the 'SDWebBrowse.pde' sketch
* and use ONLY the 'SDWebBrowse.ino' file. By default, BOTH files will
* load when using the Sketchbook menu, and the .pde version will cause
* compiler errors in 1.0. Delete the .pde, then load the sketch.
* I can't explain WHY this is necessary, but something among the various
* libraries here appears to be wreaking inexplicable havoc with the
* 'ARDUINO' definition, making the usual version test unusable (BOTH
* cases evaluate as true). FML.

* This sketch uses the microSD card slot on the Arduino Ethernet shield
* to serve up files over a very minimal browsing interface
* Some code is from Bill Greiman's SdFatLib examples, some is from the
* Arduino Ethernet WebServer example and the rest is from Limor Fried
* (Adafruit) so its probably under GPL
* Tutorial is at http://www.ladyada.net/learn/arduino/ethfiles.html
* Pull requests should go to http://github.com/adafruit/SDWebBrowse

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

/************ ETHERNET STUFF ************/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 105};
EthernetServer server(80);

/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void error_P(const char* str) {
  PgmPrint("error: ");
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.println(card.errorData(), HEX);

void setup() {
  PgmPrint("Free RAM: ");
  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards. use SPI_FULL_SPEED for better performance.
  pinMode(53, OUTPUT); // set the SS pin as an output (necessary!)
  digitalWrite(53, HIGH); // but turn off the W5100 chip!

  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  if (!root.openRoot(&volume)) error("openRoot failed");

  // list file in root with date and size
  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  // Recursive list of all directories
  PgmPrintln("Files found in all dirs:");
  // Debugging complete, we start the server!
  Ethernet.begin(mac, ip);

void ListFiles(EthernetClient client, uint8_t flags) {
  // This code is just copied from SdFile.cpp in the SDFat library
  // and tweaked to print to the client output in html!
  dir_t p;
  while (root.readDir(p) > 0) {
    // done if past last used entry
    if (p.name[0] == DIR_NAME_FREE) break;

    // skip deleted entry and entries for . and ..
    if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;

    // only list subdirectories and files
    if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;

    // print any indent spaces
    client.print("<li><a href=\"");
    for (uint8_t i = 0; i < 11; i++) {
      if (p.name[i] == ' ') continue;
      if (i == 8) {
    // print file name with possible blank fill
    for (uint8_t i = 0; i < 11; i++) {
      if (p.name[i] == ' ') continue;
      if (i == 8) {
    if (DIR_IS_SUBDIR(&p)) {

    // print modify date/time if requested
    if (flags & LS_DATE) {
       client.print(' ');
    // print size if requested
    if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
      client.print(' ');

// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100

void loop()
  char clientline[BUFSIZ];
  int index = 0;
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    // reset the input buffer
    index = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // If it isn't a new line, add the character to the buffer
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZ)
            index = BUFSIZ -1;
          // continue to read more data!
        // got a \n or \r new line, which means the string is done
        clientline[index] = 0;
        // Print it out for debugging
        // Look for substring such as a request to get the root file
        if (strstr(clientline, "GET / ") != 0) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          // print all the files, use a helper to keep it clean
          ListFiles(client, LS_SIZE);
        } else if (strstr(clientline, "GET /") != 0) {
          // this time no space after the /, so a sub-file!
          char *filename;
          filename = clientline + 5; // look after the "GET /" (5 chars)
          // a little trick, look for the " HTTP/1.1" string and
          // turn the first character of the substring into a 0 to clear it out.
          (strstr(clientline, " HTTP"))[0] = 0;
          // print the file we want

          if (! file.open(&root, filename, O_READ)) {
            client.println("HTTP/1.1 404 Not Found");
            client.println("Content-Type: text/html");
            client.println("<h2>File Not Found!</h2>");
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          int16_t c;
          while ((c = file.read()) > 0) {
              // uncomment the serial to debug (slow!)
        } else {
          // everything else is a 404
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println("<h2>File Not Found!</h2>");
    // give the web browser time to receive the data

Problem ist halt, dass mein Ethernet-Shield nicht die gleichzeitige Benutzung von SD und LAN erlaubt.
Ich werde mal schaun, ob es mit meinem selbst gebauten SD-Kartenleser funktioniert. Ich melde mich dann.

Binäre Sketchgröße: 22.184 Bytes (von einem Maximum von 32.256 Bytes)

Ist ne Menge Holz! Da kommt noch das Menü für das Display dazu mit 10k Bytes und dabei habe ich noch keine Funktionen erstellt!