Devices & Components
Arduino Uno Rev3
SmartQ C307 USB 3.0 Portable Card Reader for SD, SDHC, SDXC, MicroSD, MicroSDHC, MicroSDXC, with Advanced All-in-One Design
Resistor 1k ohm
Arduino USB Cable
TWTADE Rocker Switch Toggle On/Off 2Pin Pre-Wired 6A/250V 10A/125V AC
HiLetgo Micro SD TF Card Adater Reader Module 6Pin SPI Interface Driver Module with chip Level Conversion for Arduino UNO R3 MEGA 2560 Due
LED (generic)
Jumper wires (generic)
128GB Micro SD Card for Nintendo Switch & Switch Lite, U3 V30 Memory Card Compatible with Mobile Device Storage Phone Tablet Drone, Class 10 MicroSD Card with High Speed Up to 95MB/s
Resistor 220 ohm
Hardware & Tools
Solder Wire, Lead Free
Soldering iron (generic)
Software & Tools
Arduino IDE
Project description
Code
Toggle Switch LED microSD Card Code
c_cpp
Upload this code to your Arduino Uno R3 board after wiring the LED, toggle switch, and SD card
1//Code By: Bo Bowman 2//This code connects toggle switch, LED, and SD card (microSD card) together on a breadboard. 3//Includes Fritzing diagram 4 5//Connect wiring as follows: https://create.arduino.cc/projecthub/GeneralSpud/toggle-switch-3763a2 6//Connect switch pin to 3 (instead of pin 4 from the above url) 7//Connect led pin to 9 (instead of pin 5 from the above url) 8//Use a 220 Ohm resistor for the LED 9//Use a 1k Ohm resistor for the toggle switch 10 11//Used Arduino Uno R3 12//Used 128GB SD card 13//Used an SD Card Reader (microSD card reader), purchased here: https://www.amazon.com/gp/product/B06ZYXR7DL/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 14//Used a standard MicroSD card adapter board with CS (chipselect), SCK, MOSI, MISO, VCC pins on it. You can also buy one from adafruit here: https://www.adafruit.com/product/254?gclid=CjwKCAjwu5yYBhAjEiwAKXk_eB9RXsR5mKURYSPj84TBgerp9w3ubaqiiiWZdjVea9gIhsvzKWevyBoCCk4QAvD_BwE 15//Used these instructions for wiring of the SD card: https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial/arduino-library 16//IMPORTANT: You must format your 128GB SD card prior to using it!! 17//Initialize the SD card to FAT 16 using the following instructions for Mac OSX: 18//http://arquivo.splat-n.com/scripts/format-a-flash-card-as-fat16-in-mac-os-x/ 19//See here for additional code on reading and writing data to a MicroSD card: https://create.arduino.cc/projecthub/electropeak/sd-card-module-with-arduino-how-to-read-write-data-37f390 20 21//Used a modified version of this code, for reading/writing to/from the SD card: https://create.arduino.cc/projecthub/electropeak/sd-card-module-with-arduino-how-to-read-write-data-37f390 22 23//Wiring for the SD card: 24//Connect CS to pin 10 25//Connect SCK to pin 13 26//Connect MOSI to pin 11 27//Connect MISO to pin 12 28//Connect VCC along the + line of the breadboard, to the right of the wire that connects the + line to 5V pin on the Arduino (the left side should go to the toggle switch) 29//Connect GND to pin GND 30 31// Rocker on/off switch (basic toggle switch) from Amazon 32// https://www.amazon.com/gp/product/B07XC5KB8D/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 33 34//Initialization Code for LED and toggle switch 35int switch_pin = 3; 36int led_pin = 9; 37 38//Initialization Code for ID card 39#include <SPI.h> 40#include <SD.h> 41File myFile; 42 43void setup() { 44 //setup code runs only once 45 46 //Setup Code for switch & LED 47 pinMode(switch_pin, INPUT); //set mode for pin switch 3 to input 48 pinMode(led_pin,OUTPUT); //set mode for led pin 9 to output 49 50 //Setup Code for SD Card 51 52 // This code helps prevent error during SD card initiliazation. Note that you also must reformat your MicroSD card to FAT 16 format with a USB MicroSD adapter prior to using this code! 53 pinMode(10,OUTPUT); // set pin (the CS pin for the MicroSD card adapter board) to pin 10, and set it as Output mode 54 digitalWrite(10,HIGH); //initialize the CS pin of the MicroSD card adapter board to HIGH 55 56 // Open serial communications and wait for port to open: 57 Serial.begin(9600); 58 59 while (!Serial) { 60 ; // wait for serial port to connect. Needed for native USB port only 61 } 62 Serial.print("Initializing SD card..."); 63 if (!SD.begin(10)) { 64 Serial.println("initialization failed!"); 65 while (1); 66 } 67 Serial.println("initialization done."); 68 // open the file. note that only one file can be open at a time, 69 // so you have to close this one before opening another. 70 myFile = SD.open("test.txt", FILE_WRITE); 71 // if the file opened okay, write to it: 72 if (myFile) { 73 Serial.print("Writing to test.txt..."); 74 myFile.println("This is a test file :)"); 75 myFile.println("testing 1, 2, 3."); 76 for (int i = 0; i < 20; i++) { 77 myFile.println(i); 78 } 79 // close the file: 80 myFile.close(); 81 Serial.println("done."); 82 } 83 else { 84 // if the file didn't open, print an error: 85 Serial.println("error opening test.txt"); 86 } 87 88 89} 90 91void loop() { 92 // main code here runs repeatedly: 93 94 if (digitalRead(switch_pin) == HIGH){ 95 digitalWrite(led_pin,LOW); 96 97 } 98 if (digitalRead(switch_pin)==LOW){ 99 digitalWrite(led_pin,HIGH); 100 } 101}
Downloadable files
Fritzing Diagram
This is a diagram to help you on how to wire the project.
Fritzing Diagram

Comments
Only logged in users can leave comments