This week we show you how to use an sd card with an arduino to log data. We also show you how read a file on the sd card, manipulate files and get details like card size, type and directory of files. 

 In our second segment we show you how to turn a raspberry pi into a digital signage device.  All for nothing more than the cost of the raspberry pi.

Downloads

HD Apple HD Apple SD Audio MP3
Additional Show Notes

Data Logger

/*############################## BASIC DATA LOGGER ##################################
       This sketch shows an example of how to log data to a file on an SD card.
      
       It reads the temperature every 60 seconds and writes the temperature to
       the SD card in Centigrade, Farenheit and also writes the humidity percentage.
      
       The SD card used in this example is a 2 GB card with 1975287808 bytes available.
       This sketch writes 9 bytes per reading to the card.  With taking a reading every
       60 seconds, the card can hold just over 417 years of logged data before it runs
       out of space.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 50 (http://letmakeit.tv)
      
       http://tech-zen.tv
      
       For the sample code, show notes, contact information and many more
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for
       a new Let's Make it episode, you can contact us via email or phone at
       the show information page.
################################################################################*/

#include <Wire.h>
#include <dht11.h>
#include <SD.h>

dht11 DHT11;

long nextLogTime = 0;
long logDelay = 60000;    //one minute

void setup()
{
  //DHT11.attach(4);                         
 
  Serial.begin(9600);
 
  //Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  String dataString = "";
 
  if (nextLogTime <= millis()) {
    File dataFile = SD.open("datalog.txt", FILE_WRITE);

    // if the file is available, write to it:
    if (dataFile) {
      int chk = DHT11.read(2);          // Read the sensor
      if (chk == 0) {
        dataString = String((int)DHT11.temperature,DEC);
        dataString += ",";
        dataString += String((int)DHT11.fahrenheit());
        dataString += ",";
        dataString += String(DHT11.humidity);
        dataString += "\r";
        dataFile.println(dataString);
      }
      dataFile.close();
     
      Serial.println(dataString);
    } else {
      Serial.println("error opening datalog.txt");
    }
    
    nextLogTime = millis() + logDelay;
  }
}


Data Reader

/*############################## READ FILE FROM SD CARD ##################################
       This sketch reads logged data from the log file on the SD card and outputs it to
       the serial port.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 50 (http://letmakeit.tv)
      
       http://tech-zen.tv
      
       For the sample code, show notes, contact information and many more
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for
       a new Let's Make it episode, you can contact us via email or phone at
       the show information page.
################################################################################*/

#include <SD.h>

void setup()
{
  Serial.begin(9600);

  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  File dataFile = SD.open("datalog.txt");

  // if the file is available, read from it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
    Serial.println("");
    Serial.println("");
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
 
  delay(30000);
}



Card Info

/*############################## SD CARD INFO ##################################
       This sketch shows an example of how to read the SD card information.  It
       will print the serial port the details about the SD card every 30 seconds.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 50 (http://letmakeit.tv)
      
       http://tech-zen.tv
      
       For the sample code, show notes, contact information and many more
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for
       a new Let's Make it episode, you can contact us via email or phone at
       the show information page.
################################################################################*/

#include <SD.h>

Sd2Card card;
SdVolume volume;
SdFile root;

File myFile;

void setup()
{                  
  Serial.begin(9600);
 
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  if (!card.init(SPI_HALF_SPEED, 4)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }
 
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
 
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

 
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
 
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
 
  Serial.println("");
  Serial.println("");
 
  delay(30000);
}


File Manipulation


/*############################## SD CARD FILE MANIPULATION ##################################
       This sketch is an example of how to create and delete files from an SD card.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 50 (http://letmakeit.tv)
      
       http://tech-zen.tv
      
       For the sample code, show notes, contact information and many more
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for
       a new Let's Make it episode, you can contact us via email or phone at
       the show information page.
################################################################################*/

#include <SD.h>

File myFile;

void setup()
{
  Serial.begin(9600);
 
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist.");  
  }

  // delete the file:
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");

  if (SD.exists("example.txt")){
    Serial.println("example.txt exists.");
  }
  else {
    Serial.println("example.txt doesn't exist.");  
  }
 
  Serial.println("");
  Serial.println("");
 
  delay(30000);
}