Hi. I would like to use the built in sd card on the ethernet shield. Use them in the same sketch. I tried making pin 4 high or low but no luck so far. Is there a right way to do that? Thanks
Welcome back, it has been a while!
The Ethernet Shield has a W5100 chip and uses pin 4 for the SD card CS (Chip Select).
The Ethernet chip uses pin 10 for its CS.
Always set pin 10 as OUTPUT, even if you’re not using Ethernet, to keep the SPI bus happy.
Never use both Ethernet and SD at the same time without managing CS pins make sure to deselect one before using the other.
Here is some code I found, I have not tried it.
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4; // SD card CS pin on Ethernet Shield
void setup() {
Serial.begin(9600);
while (!Serial) ; // Wait for serial monitor on Leonardo/Micro
// Deselect the Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
}
Serial.println("SD card is ready to use.");
// Test writing to a file
File dataFile = SD.open("test.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Hello from Ethernet Shield!");
dataFile.close();
Serial.println("File written.");
} else {
Serial.println("Error opening file.");
}
}
void loop() {
// Nothing here
}
Hi. Thanks for the reply. What I want to be able to do is use the ethernet then switch and save data to the sd card then alternate back to the ethernet and so on.
I have not seen a sketch that can do that.
Mark
yes you can use Ethernet and SD card in one sketch without problems.
I have many such projects.
here an example
the housekeeping of the CS pins is done by the libraries. At least it is working for the Ethernet and the sd library.
another example can be found on my page.
The demo implements
- a small webserver
- get time via NTP
- logging of data on the SD
- file download of the logged data
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.