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.
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.
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.
Hi, for using the SD card, you can use the standard library that comes with the Arduino IDE but it may be necessary to set the CS of the Ethernet and FRAM high, as mentioned here: https://github.com/Industruino/libraries/blob/master/README.md#sd-card
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?
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
}
Also, didi you set pin D4 (not Indio) as output? What SD library are you using? Did you explicitly define SPI library?
Also, didi you set pin D4 (not Indio) as output? What SD library are you using? Did you explicitly define SPI library?
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.
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.
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!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 1/17/17, 1:12 AM |
Seen: 5338 times |
Last updated: 1/20/17, 10:35 AM |
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