Step 1: What You Need?
1 x Arduino Board ( Arduino UNO used in this tutorial)
1 x Arduino Ethernet Shield (or other board with an SD slot)
1 x Formatted SD card
1 x Arduino Ethernet Shield (or other board with an SD slot)
1 x Formatted SD card
1 x USB Type-B Cable
Don't have components? Don't worry. Just click the component's name.
Step 2: Build Your Circuit.
Step 3: Upload The Code.
1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used.
2. Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3.
Select the right port: Tools >> Port >> Select the port number.
3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> SD >> Files
Click press the "upload" button (see the button with right arrow mark).
/*
SD card basic file example This example shows how to create and destroy 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 <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."); if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
} // open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close(); // Check to see if the file exists:
if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
} // delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt"); if (SD.exists("example.txt")) {
Serial.println("example.txt exists.");
} else {
Serial.println("example.txt doesn't exist.");
}
} void loop() {
// nothing happens after setup finishes.
}
The code above is configured for use with an Ethernet shield, which has an onboard SD slot. In the setup(), SD.begin() names pin 4 as the CS pin. This pin varies depending on the make of shield or board you are using.
In the setup(), open a new file with SD.open() named "example.txt". FILE_WRITE enables read and write access to the file, starting at the end. In this example though, immediately close the file by calling myFile.close().
After checking to make sure the file exists with SD.exists(), delete the file from the card with SD.remove.
0 comments:
Post a Comment