SD card example

In this example we connect an SD card to our Arduino, we will log analog readings to a file on the SD card.

SD card

Here is the layout


arduino and sdcard bb

#include <SD.h>

const int chipSelect = 4;

void setup()

while (!Serial) {
; // wait for serial port to connect.
Serial.print(“Initializing SD card…”);
pinMode(10, OUTPUT);

//iniot SD card
if (!SD.begin(chipSelect))
Serial.println(“Card failed, or not present”);
Serial.println(“card initialized.”);

void loop()

String dataString = “”;

// read three sensors and append to the string
for (int analogPin = 0; analogPin < 3; analogPin++)
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2)
dataString += “,”;

// open the file.
File dataFile =“data.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile)
// if the file isn’t open
Serial.println(“error opening data.txt”);


sd card breakout at Amazon

sd card breakout on Amazon UK

Related posts

Arduino and 4051 LED example

Interfacing to a 24LC256 EEPROM

Arduino and MCP23017 example

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More