Logging data. SD card slot. Part 13.

Logging data. SD card slot.

If you do not intend to write to the SD card, connect the SD card can be skipped. The controller will work without this feature. If you want to log all data, you will need the SD card module for Arduino. In fact, all just connected. You need 6 wires to make it.

SD card module

SD card module

For SD module is used voltage of 3.3V. It can be taken either directly from the board arduino (pin marked 3V3), or from the same module. I used 3.3V from arduino board. You can take a standard 5V but it should be connected to pin 5 on the module. In this case, SD module itself will reduce from 5V to 3.3V

For the first time, when I plugged SD module I have spent a few days and have broken one SD card :) I can say that I have been successfully working mini SDHC card to 4GB via an adapter that came in the kit. As a result of experiments it was found that the larger cluster size is better for stable work. With a small cluster size card was not detected. The cluster size is specified during formatting the SD card on your computer. The file system is FAT32.

Formatting SD card

Open SD in the explorer. Right click and choose Format...

You can check the result of connecting the module SD performing this one example. It detects SD card, and creates a file called house4u.txt with the text hello SD

Text of sample:

// include the library code:
#include <LiquidCrystal.h>

#include <SPI.h>
#include <SD.h>

#define PWM_LED_PIN 3 
#define PIN_LCD_RS 39
#define PIN_LCD_EN 41
#define PIN_LCD_D4 43
#define PIN_LCD_D5 45
#define PIN_LCD_D6 47
#define PIN_LCD_D7 49
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd( PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7 );
File myFile;  
const int chipSelect = 53; // MEGA CS
boolean HaveSDCard = false;

void setup() {   
  analogWrite( PWM_LED_PIN, 255 );
  lcd.begin(16, 2); 
  lcd.print("House4u sol.proj"); 
  lcd.setCursor(0, 1); 
  pinMode(chipSelect, OUTPUT);
  if (SD.begin(chipSelect) )
    HaveSDCard = true;
    lcd.print("SD Card - OK");   
    lcd.print("SD Card - FAIL");  
  delay( 2000 );

void loop() 
  if ( HaveSDCard )
    lcd.setCursor(0, 1); 
    lcd.print("                ");
    lcd.setCursor(0, 1);
    myFile = SD.open("house4u.txt", FILE_WRITE);
    if ( !myFile )
      for ( int i = 0; i < 5; i++ )
           if ( !myFile )
              delay( 500 );
              myFile = SD.open("house4u.txt", FILE_WRITE );    
    if ( myFile )
      myFile.println("hello SD");
      // close the file:
      lcd.print("Writing OK. DONE");          
      lcd.print("Writing FAIL!"); 
  HaveSDCard = false;

Result of execution:

Result of execution

#1 - SD card was not found. #2 - SD card was found. #3 - successful writing!

<< BackNext >>

Share with friends: