Advertisement
microrobotics

Pololu Breakout Board for microSD Card

Apr 5th, 2023 (edited)
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To use the Pololu Breakout Board for microSD Card with 3.3V Regulator and Level Shifters, you'll need a microcontroller like Arduino. The breakout board can be easily interfaced with an Arduino using the SPI protocol. Here's an example code for reading and writing to a microSD card using the SD library in the Arduino IDE:
  3.  
  4. This code initializes the microSD card, writes "Hello, World!" to a file named "test.txt", and then reads the content of the file and prints it to the Serial monitor. Remember to connect the breakout board's pins to the corresponding Arduino pins (MOSI, MISO, SCK, and CS) and supply 3.3V power to the board.
  5. */
  6.  
  7. #include <SPI.h>
  8. #include <SD.h>
  9.  
  10. // Pin connections to the breakout board
  11. const int chipSelect = 4; // CS pin connected to digital pin 4
  12.  
  13. void setup() {
  14.   Serial.begin(9600);
  15.   while (!Serial) {
  16.     ; // Wait for the serial monitor to open
  17.   }
  18.  
  19.   Serial.print("Initializing SD card...");
  20.  
  21.   if (!SD.begin(chipSelect)) {
  22.     Serial.println("Card failed, or not present");
  23.     // Don't do anything more
  24.     while (1);
  25.   }
  26.  
  27.   Serial.println("Card initialized.");
  28.  
  29.   // Write data to the file
  30.   File dataFile = SD.open("test.txt", FILE_WRITE);
  31.  
  32.   if (dataFile) {
  33.     dataFile.println("Hello, World!");
  34.     dataFile.close();
  35.     Serial.println("Data written.");
  36.   } else {
  37.     Serial.println("Error opening test.txt");
  38.   }
  39.  
  40.   // Read data from the file
  41.   dataFile = SD.open("test.txt");
  42.  
  43.   if (dataFile) {
  44.     Serial.println("Reading data:");
  45.     while (dataFile.available()) {
  46.       Serial.write(dataFile.read());
  47.     }
  48.     dataFile.close();
  49.   } else {
  50.     Serial.println("Error opening test.txt");
  51.   }
  52. }
  53.  
  54. void loop() {
  55.   // Nothing to do in the loop
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement