Arduino SD Card Data Logger: Wiring, Code and Simulator

To build this Arduino SD Card Data Logger project you need an Arduino Uno and a microSD card module and a potentiometer. The SD library reads README.TXT from a FAT16 card, then appends the A0 reading to LOG.CSV every second. Make the 9 connections in the wiring table, upload the Arduino C++ code, and turn the knob, then open the Modules tab: LOG.CSV is on the card, ready to download and open in a spreadsheet.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1microSD card moduleSD card on SPI with a 3.3 V regulator and level shifter: VCC 5 V, GND, CS to any pin (4 or 10), SCK/MOSI/MISO to the board’s SPI pins. The card is a 32 MB FAT16 card with a few files; files your sketch writes show in the Modules tab, ready to download.
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.

Wiring

#FromTo
1Arduino Uno 5VmicroSD card module VCC
2Arduino Uno GNDmicroSD card module GND
3Arduino Uno pin 4microSD card module CS
4Arduino Uno pin 13microSD card module SCK
5Arduino Uno pin 11microSD card module MOSI
6Arduino Uno pin 12microSD card module MISO
7Arduino Uno A0Potentiometer SIG
8Arduino Uno 5VPotentiometer VCC
9Arduino Uno GNDPotentiometer GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a microSD card module and a potentiometer from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to microSD card module VCC.
  4. Wire 2. Connect Arduino Uno GND to microSD card module GND.
  5. Wire 3. Connect Arduino Uno pin 4 to microSD card module CS.
  6. Wire 4. Connect Arduino Uno pin 13 to microSD card module SCK.
  7. Wire 5. Connect Arduino Uno pin 11 to microSD card module MOSI.
  8. Wire 6. Connect Arduino Uno pin 12 to microSD card module MISO.
  9. Wire 7. Connect Arduino Uno A0 to Potentiometer SIG.
  10. Wire 8. Connect Arduino Uno 5V to Potentiometer VCC.
  11. Wire 9. Connect Arduino Uno GND to Potentiometer GND.
  12. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  13. Run it. Press Run. Turn the knob, then open the Modules tab: LOG.CSV is on the card, ready to download and open in a spreadsheet.

Arduino C++ code

// microSD data logger with the SD library.
// CS -> 4, SCK -> 13, MOSI -> 11, MISO -> 12, VCC 5V, GND. A potentiometer on A0.
// Open the Modules tab to see LOG.CSV grow, and download it.
#include <SPI.h>
#include <SD.h>

const int CS = 4;

void setup() {
  Serial.begin(9600);
  Serial.print(F("SD card... "));
  if (!SD.begin(CS)) {
    Serial.println(F("failed. Is the card in and CS on pin 4?"));
    while (true) {}
  }
  Serial.println(F("ready."));

  File f = SD.open("README.TXT");
  if (f) {
    Serial.println(F("README.TXT says:"));
    while (f.available()) Serial.write(f.read());
    f.close();
  }
  f = SD.open("LOG.CSV", FILE_WRITE);
  if (f) { f.println(F("seconds,A0")); f.close(); }
}

void loop() {
  static unsigned long n = 0;
  int v = analogRead(A0);
  File f = SD.open("LOG.CSV", FILE_WRITE);
  if (f) {
    f.print(n);
    f.print(',');
    f.println(v);
    f.close();
    Serial.print(F("Logged "));
    Serial.print(n);
    Serial.print(F(" s: "));
    Serial.println(v);
  } else {
    Serial.println(F("Could not open LOG.CSV"));
  }
  n++;
  delay(1000);
}

What to try

Turn the knob, then open the Modules tab: LOG.CSV is on the card, ready to download and open in a spreadsheet.

Frequently asked questions

How do I wire the SD Card Data Logger project on an Arduino Uno?

Make 9 connections: Arduino Uno 5V to microSD card module VCC; Arduino Uno GND to microSD card module GND; Arduino Uno pin 4 to microSD card module CS; Arduino Uno pin 13 to microSD card module SCK; Arduino Uno pin 11 to microSD card module MOSI; Arduino Uno pin 12 to microSD card module MISO; Arduino Uno A0 to Potentiometer SIG; Arduino Uno 5V to Potentiometer VCC; Arduino Uno GND to Potentiometer GND.

What parts do I need for this Arduino project?

An Arduino Uno and a microSD card module and a potentiometer.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

The SD library reads README.TXT from a FAT16 card, then appends the A0 reading to LOG.CSV every second.

Why does SD.begin() fail?

The card is not FAT16/FAT32, CS is on a different pin than in SD.begin(), or the module is not powered. Pin 10 must stay an output on an Uno even when CS is elsewhere.

How do I log data to a CSV file on an SD card?

Open the file with FILE_WRITE (it appends), print comma-separated values with println, and close it after each write so the data reaches the card.

More Arduino Uno projects