Make your own SD shield
Here is how I built my own "SD shield" for another project I was working on, using a solder iron and a MircoSD adapter.
Components and supplies
1
Flash Memory Card, SD Card
4
Through Hole Resistor, 1 kohm
1
5 mm LED: Green
1
Arduino UNO
3
Through Hole Resistor, 2 kohm
4
Tactile Switch, SPST-NO
3
5 mm LED: Red
1
Jumper wires (generic)
1
Breadboard (generic)
3
Through Hole Resistor, 3 kohm
1
Through Hole Resistor, 10 kohm
Tools and machines
1
Solder Wire, Lead Free
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
SD card I/O
arduino
1// include the SD library: 2#include <SPI.h> 3#include <SD.h> 4 5// Enable button at pin 6 6const int Enable = 6; 7int Data_Counter = 0; 8 9// Input buttons at pin 5, 4, 3 10const int Input_1 = 5; 11const int Input_2 = 4; 12const int Input_3 = 3; 13 14// set up variables using the SD utility library functions: 15Sd2Card card; 16SdVolume volume; 17SdFile root; 18 19const int chipSelect = 10; 20 21// Create a file to store the data 22File myFile; 23 24void setup() { 25 // Open serial communications and wait for port to open: 26 Serial.begin(9600); 27 while (!Serial) { 28 ; // wait for serial port to connect. Needed for native USB port only 29 } 30 31 // Initialize inputs 32 pinMode(Enable, INPUT); 33 pinMode(Input_1, INPUT); 34 pinMode(Input_2, INPUT); 35 pinMode(Input_3, INPUT); 36 37 // setup for the SD card 38 Serial.println("Initializing SD card..."); 39 40 if(!SD.begin(chipSelect)) { 41 Serial.println("initialization failed!"); 42 return; 43 } 44 Serial.println("initialization done."); 45 Serial.println(); 46 47 //open file 48 myFile = SD.open("DATA.csv", FILE_WRITE); 49 50 // if the file opened ok, write to it: 51 if (myFile) { 52 Serial.println("File opened ok"); 53 Serial.println("Waiting for inputs"); 54 Serial.println(); 55 // print the headings for our data 56 myFile.println("Entry,Input_1,Input_2,Input_3"); 57 } 58 myFile.close(); 59} 60 61void loop(void) { 62 if (digitalRead(Enable) == 1) { 63 // Write to terminal for debugging 64 Serial.print("Reading data entry "); 65 Serial.print(Data_Counter); // The first is 0 66 Serial.println(":"); 67 Serial.print("Input_1 = "); 68 Serial.println(digitalRead(Input_1)); 69 Serial.print("Input_2 = "); 70 Serial.println(digitalRead(Input_2)); 71 Serial.print("Input_3 = "); 72 Serial.println(digitalRead(Input_3)); 73 Serial.println(" "); 74 // Write to SD card 75 myFile = SD.open("DATA.csv", FILE_WRITE); 76 if (myFile) { 77 myFile.print(Data_Counter); 78 myFile.print(","); 79 myFile.print(digitalRead(Input_1)); 80 myFile.print(","); 81 myFile.print(digitalRead(Input_2)); 82 myFile.print(","); 83 myFile.print(digitalRead(Input_3)); 84 myFile.println(","); 85 } 86 myFile.close(); 87 Serial.println("Data written to SD"); 88 Serial.println(); 89 Data_Counter++; 90 delay(5000); // 5000 = 5 seconds 91 } 92}
SD card I/O
arduino
1// include the SD library: 2#include <SPI.h> 3#include <SD.h> 4 5// 6 Enable button at pin 6 7const int Enable = 6; 8int Data_Counter = 0; 9 10// 11 Input buttons at pin 5, 4, 3 12const int Input_1 = 5; 13const int Input_2 = 4; 14const 15 int Input_3 = 3; 16 17// set up variables using the SD utility library functions: 18Sd2Card 19 card; 20SdVolume volume; 21SdFile root; 22 23const int chipSelect = 10; 24 25// 26 Create a file to store the data 27File myFile; 28 29void setup() { 30 // Open 31 serial communications and wait for port to open: 32 Serial.begin(9600); 33 while 34 (!Serial) { 35 ; // wait for serial port to connect. Needed for native USB port 36 only 37 } 38 39 // Initialize inputs 40 pinMode(Enable, INPUT); 41 pinMode(Input_1, 42 INPUT); 43 pinMode(Input_2, INPUT); 44 pinMode(Input_3, INPUT); 45 46 // 47 setup for the SD card 48 Serial.println("Initializing SD card..."); 49 50 51 if(!SD.begin(chipSelect)) { 52 Serial.println("initialization failed!"); 53 54 return; 55 } 56 Serial.println("initialization done."); 57 Serial.println(); 58 59 60 //open file 61 myFile = SD.open("DATA.csv", FILE_WRITE); 62 63 // if the 64 file opened ok, write to it: 65 if (myFile) { 66 Serial.println("File opened 67 ok"); 68 Serial.println("Waiting for inputs"); 69 Serial.println(); 70 71 // print the headings for our data 72 myFile.println("Entry,Input_1,Input_2,Input_3"); 73 74 } 75 myFile.close(); 76} 77 78void loop(void) { 79 if (digitalRead(Enable) 80 == 1) { 81 // Write to terminal for debugging 82 Serial.print("Reading 83 data entry "); 84 Serial.print(Data_Counter); // The first is 0 85 Serial.println(":"); 86 87 Serial.print("Input_1 = "); 88 Serial.println(digitalRead(Input_1)); 89 90 Serial.print("Input_2 = "); 91 Serial.println(digitalRead(Input_2)); 92 93 Serial.print("Input_3 = "); 94 Serial.println(digitalRead(Input_3)); 95 96 Serial.println(" "); 97 // Write to SD card 98 myFile = SD.open("DATA.csv", 99 FILE_WRITE); 100 if (myFile) { 101 myFile.print(Data_Counter); 102 myFile.print(","); 103 104 myFile.print(digitalRead(Input_1)); 105 myFile.print(","); 106 myFile.print(digitalRead(Input_2)); 107 108 myFile.print(","); 109 myFile.print(digitalRead(Input_3)); 110 myFile.println(","); 111 112 } 113 myFile.close(); 114 Serial.println("Data written to SD"); 115 116 Serial.println(); 117 Data_Counter++; 118 delay(5000); // 5000 = 5 seconds 119 120 } 121}
Downloadable files
Breadboard
Breadboard

Breadboard
Breadboard

Prototype
Prototype

Comments
Only logged in users can leave comments