Transcription

Arduino Ethernet SD CardCreated by Mike BarelaLast updated on 2019-04-09 02:45:52 PM UTC

OverviewThis tutorial provides the basics for using a WIZ5500-based Ethernet Shield with an Arduino compatible. The WiznetWIZ5500 is a modern Ethernet interface chip and is used in the Ethernet Shield W5500 (https://adafru.it/BQY) and theFeather Ethernet Wing (https://adafru.it/wiB) available at Adafruit.The shield form factor works well for ATmega328 based Arduino compatibles like the Arduino Uno R3 and the AdafruitMetro 328 (https://adafru.it/METROXMETR) (Classic). The shield contains both an Ethernet connection for networks andan SD Card slot for storing data.The Feather wing works well with a Feather with a SD card slot. The Feather 32u4 Adalogger (https://adafru.it/tUd) hasan Arduino Leonardo compatible 32u4 processor and a micro-SD card slot. With the Ethernet Feather Wing, it providesthe same functionality as the Ethernet Shield Metro/Uno in a much smaller package. SD and micro-SD are the samefor all purposes but the latter is smaller. In this tutorial, when it says do something to an SD card and you are using theFeathers, think micro-SD.This tutorial assumes you already know the basics of the Arduino IDE, code generation and the Arduino interface. Ifyou are not so familiar with the Arduino IDE, you might check the tutorials in the Adafruit Learn ArduinoSeries (https://adafru.it/BQZ).As of the date of this revised tutorial, Arduino notes their Ethernet Shields are retired (https://adafru.it/BQ-). If you use aretired or third-party Ethernet shield, you may have to use a different Arduino library that supports the chipset theboard uses.Note that the Ethernet Shield and Feather Wing Adafruit uses is based on the WIZ5500 chip, not the older WIZ5100chipset or others. The WIZ5500 requires the Ethernet2 library, not the older Ethernet library on Arduino. If you usethe older hardware, just be sure you change libraries back from Ethernet2 to Ethernet and double check things.This tutorial will go through preparing then using the SD card, both in general then reading the files on a card. The finalexample will show how to access the SD card remotely over Ethernet. This capability could be the basis for a remote Adafruit net-sd-cardPage 3 of 13

file storage or other program that access a remote Arduino compatible. Adafruit net-sd-cardPage 4 of 13

SD Card PreparationWARNING: DO NOT FORMAT YOUR HARD DISK(S) TRYING TO FORMAT YOUR SD CARD.Ensure you use a computer to pre-format your SD card as FAT16. FAT16 was introduced by Microsoft a long time agobut it is simple and microcontrollers like simple. Many formatting programs support FAT16. Windows supports FAT16natively with the format command and in the File Explorer.It is suggested that you use the sdcard.org SD Memory Card Formatter app. It is available for PC and Mac.https://adafru.it/B2qhttps://adafru.it/B2qHere is a shot of SD Card Formatter with a card inserted. Adafruit net-sd-cardPage 5 of 13

Adafruit net-sd-cardPage 6 of 13

Reading SD Card InformationThe program below is a slightly modified version of the Arduino example SdFatInfo program. The main change is todefine the SPI select of the WIZ5500 Ethernet chip and set it high (unselected) so the sketch can talk to the SD cardonly.For the Ethernet shield, put your formatted SD card into the SD card slot (top side up, don't force it, gentle). Put yourshield on your Arduino-compatible board. Power the boards with a suitable power supply recommended for theArduino. Connect the microcontroller board to your computer with a suitable USB cable.For the Feathers, place the formatted micro-SD card into the micro-SD card slot in the Adalogger Feather. Plug theboard into a USB port on your computer.Using the Arduino IDE software and select the correct type of Arduino-compatible board and the serial port for theboard in the Tools menu. You may have to push the reset button for the operating system to see the serial port. Loadthe following sketch, compile, and run.The information about the SD card should be displayed on the serial monitor in the IDE./*SD card test for WIZ5500 Compatible Ethernet BoardsThis example shows how use the utility libraries on which the'SD library is based in order to get info about your SD card.Very useful for testing a card when you're not sure whether its working or not.The circuit:* SD card attached to SPI bus as follows:** MOSI - pin 11 on Arduino Uno and Adafruit Metro** MISO - pin 12 on Arduino Uno and Adafruit Metro** CLK - pin 13 on Arduino Uno and Adafruit Metro** CS - depends on your SD card shield or module (see below)created 28 Mar 2011 by Limor Friedmodified 9 Apr 2012 by Tom Igoemodified 12 Apr 2018 by Mike Barela*/// include the SD library:#include SPI.h #include SD.h // set up variables using the SD utility library functions:Sd2Card card;SdVolume volume;SdFile root;// change this to match your SD shield or module;// Arduino Ethernet shield: pin 4// Adafruit #2971 W5500 by Seeed Studio: Pin 4// Sparkfun SD shield: pin 8// Arduino Mega: Pin 53// MKRZero SD: SDCARD SS PINconst int chipSelect 4;//// Chip Select for W5500 Ethernet (must be set as output in initialization)const int W5500 SS 10; Adafruit net-sd-cardPage 7 of 13

const int W5500 SS 10;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("\nInitializing SD card.");// we'll use the initialization code from the utility libraries// since we're just testing if the card is working!pinMode(W5500 SS, OUTPUT);// set the Ethernet SS pin as an output (necessary!)digitalWrite(W5500 SS, HIGH); // but turn off the W5500 chip for nowif (!card.init(SPI HALF SPEED, chipSelect)) {Serial.println("initialization failed. Things to check:");Serial.println("* is a card inserted?");Serial.println("* is your wiring correct?");Serial.println("* did you change the chipSelect pin to match your shield or module?");return;} else {Serial.println("Wiring is correct and a card is present.");}// print the type of cardSerial.print("\nCard type: ");switch (card.type()) {case SD CARD TYPE SD1:Serial.println("SD1");break;case SD CARD TYPE SD2:Serial.println("SD2");break;case SD CARD TYPE rintln("Unknown");}// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32if (!volume.init(card)) {Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");return;}// print the type and size of the first FAT-type volumeuint32 t volumesize;Serial.print("\nVolume type is FAT");Serial.println(volume.fatType(), DEC);Serial.println();volumesize volume.blocksPerCluster();volumesize * volume.clusterCount();volumesize * 512;Serial.print("Volume size (bytes): ");Serial.println(volumesize);Serial.print("Volume size (Kbytes): ");volumesize / 1024;Serial.println(volumesize); Adafruit Industries// clusters are collections of blocks// we'll have a lot of clusters// SD card blocks are always 512 d-cardPage 8 of 13

Serial.println(volumesize);Serial.print("Volume size (Mbytes): ");volumesize / Files found on the card (name, date and size in bytes): ");root.openRoot(volume);// list all files in the card with date and sizeroot.ls(LS R LS DATE LS SIZE);}void loop(void) {}The serial monitor output below is for a micro-SD card formatted in Windows 10 for FAT. It is set for 60MB (it doesn't fillthe card, it was an old Raspberry Pi Zero system card). It has several text files and a directory with a couple of files in it.Your Arduino Serial Monitor should display something similar. Adafruit net-sd-cardPage 9 of 13

Listing FilesOnce you are reliably reading SD card information, you can move on to looking at what files might be on the SD card.The code below is the Arduino SD Card Example Listfiles. It is modified to put the WIZ5500 chip select high so the SPIbus is only talking to the SD card.At this point, please place a few files on the SD card from your computer to get an interesting output rather than usinga blank card. It is suggested to put two or three text files with the filenames being no more than 8 characters beforethe period then end in .txt. An example would be README.txt or todo.txt. You can put the code text files on the cardalso, having them end in .ino will be fine also.Temporarily unable to load content:The screen shot below shows information on the same card as in the last sketch but lists the files and their sizes.Note in Windows, filenames like WPSETT 1.DAT refer to a longer filename (such as WPSETTINGS.DAT). Microsoft, forcompatibility with older software, truncates filenames greater than 8 characters (not including the 3 character fileextension) at 6 characters, a tilde ( ) character, and a number (in case multiple truncated files exist). The older softwarecat read and write to the file the same as an 8.3 character file.The extra information for the long file name is hidden from older software but perfectly safe for newer software. Thereare no worries opening the shortened filename will somehow corrupt the capability to use the long filenames as longas using the file name per the name you see (and not changing the file name) is observed. Adafruit net-sd-cardPage 10 of 13

Serving Files over EthernetNow that all the SD card functionality appears to be working, the program below allows you to view the files on an SDcard over the Ethernet interface and download any file you wish.The program will provide a directory listing of the SD card to a web page. If a directory is clicked, the web page willshow the contents of the subdirectory. If a file is clicked, the file will be sent to your computer. If you want to go backup the directory tree, use the browser back button.The library which provides the WIZ5500 Ethernet functionality is called Ethernet2. The Adafruit version of Ethernet2has been maintained and should be used instead of the Ethernet2 library available in the Arduino Library Manager.You can get the library code on GitHub at https://github.com/adafruit/Ethernet2 (https://adafru.it/plf).It is suggested that you put the Ethernet2 code from the Adafruit GitHub repository into your Arduino sketch folder, inthe libraries subdirectory, in a subdirectory called Ethernet2.For the source code below, you should review the following in the code:Byte array mac[] is set to a generic MAC address - if you have another device on your home network with thesame address, change things a bit. Each value is an 8 bit value in hexadecimal (from 00 to FF).Byte array ip[] is the Internet Protocol (IP) address of an unused device on your home network. Having theEthernet board way up at 177 (decimal) is fairly safe - if you know networking and this will not work, if you haveyour network on a network other than 192.168.1.xxx (say at 192.168.0.xxx or 10.0.0.xxx) change those also. Youcan find information like this from your Internet router.If you want to use DHCP to get an address from your router, uncomment the version of the Ethernet.begin callwith only the mac address. It may be more difficult to get the address and the address might change is the routergives out a new address. So the default is a fixed address.Temporarily unable to load content: Adafruit net-sd-cardPage 11 of 13

How it Works and Wrap-upThe main interface for the Ethernet uses code from the standard Arduino example WebServer and the SD card codeused earlier in the tutorial.The program implements the very bare bones of an HTML server. The code lists directories then files as an unorderedHTML list.Ensure the project is powered up and connected via an Ethernet cable to your home network. The yellow networkactivity light should blink now & then on one side of the connection, green on the other. If the lights are not on, checkthe Ethernet connection.Open a web browser on a computer on your network and go to address http://192.168.1.177 or to the alternate addressyou coded in your sketch. If you changed the sketch to use DHCP to get a free address from your router, the addressreceived will be print on the serial monitor.The program should output the heading Files: then a list of files on the card. You did put a few test files on the cardearlier, yes? If not, no worries, power down the project, eject the SD card, put some text-based files on it, reinsert, andpower the project up again.If you click on a file name, the sketch will send the file and the browser will ask you for a location to save the file.If you click on a directory name, a new web page will appear displaying the files in the subdirectory.Use the browser back button to go from the text listing screen back to the file list screen.If there is a request for a file that is not on the SD card, the sketch will return an HTTP 404 error (file not found). Anyother error will also return a 404 error. Adafruit net-sd-cardPage 12 of 13

Wrap-UpWhen the original tutorial for using the Ethernet shield and sketch was written 7 years ago, serving files via Ethernetwas pretty novel (read: wicked). Ethernet shields are not as common now. It is likely due to the proliferation of Wi-Fiboards.Hopefully this updated tutorial will give you the basics for using a microcontroller to access an SD card (or somethingelse) via Ethernet. The same methods can be used if you wire up a SD card breakout to the your microcontroller pins,just be sure to check for any necessary code changes.Most newer microcontroller boards have more memory, so a more robust web server capability may considered.Perhaps you can look at this code and consider a new implementation with an Adafruit Express board andCircuitPython. The future awaits our innovation.If you come up with great designs, please share them in the Adafruit forums (https://adafru.it/dYq) and in the AdafruitDiscord chat (). Adafruit IndustriesLast Updated: 2019-04-09 02:45:52 PM UTCPage 13 of 13