How to write on SD Card for Ethernet Expansion Module

I tried to write on SD Card formatted as FAT32 2GB as indicated in the guide. We use INDIO.

We tested with another Arduino on SD expansion module and similar code with same SD works.

Here is the code:

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

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  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("VIBO.xls", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println(sensor);
    myFile.println(time);
    myFile.println(latitude, 6);
    myFile.println(longitude, 6);
    // 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("VIBO.xls");
  if (myFile) {
    Serial.println("VIBO.xls:");

    // 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");
  }

The code stops at: the code stops at: "error opening test.txt" when running it.

Thanks for your help.

 

Lorenzo Boncompagni
Lorenzo Boncompagni
7
| 0 0 0
Asked on 1/17/17, 1:12 AM
0
vote
242 Views

Hello, as you have the same error message in two parts of your sketch it is difficult to know where is the error. If you differentiate messages you will at least know if the error is in opening the file or in reading it. Anyhow I suggest you install the modified ethernet library for industruino and copy the headers from one of the examples, as it is written in the documentation. Headers look like this: char foo; //without a simple variable declaration, use of #ifdef at the top of your code raises an error! typedef uint8_t SOCKET; #include #include #define W5500_ETHERNET_SHIELD

Stefano Ariel Bonvini
on 1/17/17, 10:55 AM

Hello, unfortunately I don't have an Ethernet module available at the moment, I can only take a look inside the libraries.
So my question is: where do you actually define the SD library and object?

What library did you use for the other jardware?

Because as I see the SD is not defined in EthernetIndustruino.h

Maybe Ethernet.h includes it and EthernetIndustruino.h doesn't, or something along this line.

Stefano Ariel Bonvini
Stefano Ariel Bonvini
27
| 0 0 0
Answered on 1/18/17, 8:16 AM
1
vote

sorry have replied above in former message

Lorenzo Boncompagni
on 1/18/17, 9:18 AM

Hello,

I worked out a possible explanation and some solutions.

The fact is, as variably documented on other platforms, that the SD library does not use the public pin name advertised, but converts it into the AVR pin name.

So in Arduino digital pin 4 is mapped to PD4 on AVR, while in Industruino pin D4 is mapped to PC2 on AT90USB1286.

So please try to write (D4) instead of (4) in the SD.begin() function, or try A2, which corresponds to pin PC2 on AVR, as crazy as it might seem.

Please let me know if this solves the issue.

  

Stefano Ariel Bonvini
Stefano Ariel Bonvini
567
| 0 0 0
Answered on 1/20/17, 10:35 AM
0
vote

Thanks will try and let you know.

Lorenzo Boncompagni
on 1/21/17, 9:44 AM

Hi Lorenzo, i have just tested with this setup: IND.I/O with 1286 MCU, Ethernet module, Arduino IDE 1.6.5

Using the standard IDE library example, without modifying anything, it should work:

ReadWrite.ino give this output:

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

Can you try this standard example without modifying anything?

 

Tom
Tom
5150
| 0 0 1
Answered on 1/20/17, 1:19 AM
0
vote

OK thanks I did that but now it shows:
:Initializing SD card...initialization failed. Things to check:
* is a card inserted?
* is your wiring correct?
* did you change the chipSelect pin to match your shield or module?
Card is inserted and works in other arduino environments.

Thanks

18 Jan 2017 - 12h12 Kenyan Time
Am adding here the full code except the json part we use to send to server.
We use the normal SD.h library that is available by default in library manager in Arduide IDE v. 1.6.5
Thank you for your help.
SD Library https://1drv.ms/u/s!Aqi9ndZKgoSjjGfoRwADQ1eePdBn

/*
  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!");
    return;
  }
  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
}

 

Lorenzo Boncompagni
Lorenzo Boncompagni
7
| 0 0 0
Answered on 1/17/17, 2:06 PM
0
vote

Did you also copy the headers and install the library?

Stefano Ariel Bonvini
on 1/17/17, 3:05 PM

Actually you didn't post your libraries' defines

Stefano Ariel Bonvini
on 1/17/17, 3:06 PM

Also, didi you set pin D4 (not Indio) as output? What SD library are you using? Did you explicitly define SPI library?

Stefano Ariel Bonvini
on 1/17/17, 3:16 PM

Also, didi you set pin D4 (not Indio) as output? What SD library are you using? Did you explicitly define SPI library?

Stefano Ariel Bonvini
on 1/17/17, 3:16 PM

Thanks will check everything again with my colleague and get back to you. Would be nice to have a clear example on how to write on the SD card.

Lorenzo Boncompagni
on 1/17/17, 3:20 PM

We have set the pin to chipset 4 (D4) while writing to the SD, we are currently using the inbuilt SD Library example card info and readwrite example, on the header of the application we have defined SPI library as # include. The library we installed are EthernetIndustruino and Ethernet by Arduino version 1.1.2.Kindly clarify which Ethernet library also the link if possible so as we may install and get the header you mentioned. Thank you.

Lorenzo Boncompagni
on 1/18/17, 5:58 AM

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

31 follower(s)

Stats

Asked: 1/17/17, 1:12 AM
Seen: 242 times
Last updated: 1/20/17, 10:35 AM