0% found this document useful (0 votes)
3 views

Lab5-SD-Card

The document outlines a lab on SD card interfacing using Arduino, detailing objectives, required facilities, prerequisites, and the process for reading and writing data to an SD card. It includes hardware requirements, circuit diagrams, and example code for interfacing with an SD card, as well as applications like a temperature and humidity data logger using a DHT11 sensor. Additionally, it emphasizes the importance of using voltage dividers to prevent damage to the SD card when connecting to the Arduino.

Uploaded by

oanhoanh2004.vn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab5-SD-Card

The document outlines a lab on SD card interfacing using Arduino, detailing objectives, required facilities, prerequisites, and the process for reading and writing data to an SD card. It includes hardware requirements, circuit diagrams, and example code for interfacing with an SD card, as well as applications like a temperature and humidity data logger using a DHT11 sensor. Additionally, it emphasizes the importance of using voltage dividers to prevent damage to the SD card when connecting to the Arduino.

Uploaded by

oanhoanh2004.vn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab : SD card interfacing

1. Objectives
2. Facilities
3. Prerequest
4. Process
4.1.
5. Introduction
5.1. Introduction to SD card

This example shows how to read information about a SD card. The example
reports volume type, free space and other information using the SD library,
sending it over the serial port. Also Proteus simulation of the Arduino and SD
card is available with a small video.

Arduino has a very nice SD card library, with this library the interfacing is very
simple. The Arduino SD library allows for reading from and writing to SD cards.
It is built on sdfatlib by William Greiman. The library supports FAT16 and
FAT32 file systems on standard SD cards and SDHC cards. It uses short 8.3
names for files.

The communication between the microcontroller and the SD card uses SPI,
which takes place on digital pins 11, 12, and 13 (on most Arduino boards) or
50, 51, and 52 (Arduino Mega). Additionally, another pin must be used to
select the SD card. This can be the hardware SS pin – pin 10 (on most Arduino
boards) or pin 53 (on the Mega) – or another pin specified in the call
to SD.begin().

5.2. Hardware Required


- Arduino board
- SD card with FAT16 or FAT32 file system
- SD card socket (connector)
- 10K ohm resistor
- 3 x 3.3K ohm resistor
- 3 x 2.2K ohm resistor
- Breadboard
- Jumper wires
5.3. The circuit:
ARDUINO UNO

AREF

13 CLK
DO

SD Card
RESET 12
~11 DI
ARDUINO

5V ~10
~9 CS
8
POWER

GND
ATMEGA328P

SD
ATMEL

DIGITAL (PWM ~)

7
UNO

~6
RXD
A0 ~5 VT52, VT100, ANSI
A1 4
TXD
A2 ~3
ANALOG IN

A3 2
RTS
A4 TX > 1
A5 RX < 0 Xmodem, Ymodem, Zmodem
CTS

(Grounded terminals are connected together)

The SD card is supplied from the Arduino board with 3.3V.

In the circuit there are 3 voltage dividers, each one consists of 2.2K and 3.3K
resistors, they are used to step down 5V that comes from the arduino into 3V
which is sufficient for the SD card signals. The voltage dividers are used for SD
card signals: SCK (serial clock), MOSI (master out slave in) and SS (chip select).
The Arduino sends these signals from pins 13, 11 and 10 respectively. The SD
card MISO is connected directly to the arduino because this path is used by the
SD card to send data to the arduino (with voltage of 3.3V).
Connecting the SD card directly to the arduino without voltage level converters or
voltage dividers may damage it.

5.4. Arduino Code:


// Interfacing Arduino with SD card example (get SD card info)
// include the SD library:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init()) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.println();
Serial.print("Card 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");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted
the card");
while (1);
}
Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print("Volume size (Kb): ");
Serial.println(volumesize);
Serial.print("Volume size (Mb): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Gb): ");
Serial.println((float)volumesize / 1024.0);
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);
}
void loop(void) {
}
5.5. Result

I placed an empty (with no file) 2GB micro SD card with FAT16 file system and
I got the result shown below:

and the following image shows a 8GB micro SD card formatted with FAT32 file
system. There are 3 folders in this SD card: Images, Videos and Audio, the
Images folder contains 1 file named Photo (Photo.jpg with extension) and the
other folders are empty:

If there is no card or the Arduino can’t connect to it, serial monitor will display the
message below:

6. SD card example – Read and write files


6.1. Introduction
This example shows how to read and write data to and from an SD card.
Example will be tested in a real hardware circuit and simulated with Proteus
software.
6.2. Hardware Required:
- Arduino board
- SD card with FAT16 or FAT32 file system
- SD card socket (connector)
- 10K ohm resistor
- 3 x 3.3K ohm resistor
- 3 x 2.2K ohm resistor
- Breadboard
- Jumper wires
6.3. The circuit:

(Grounded terminals are connected together)

The SD card is supplied from the Arduino board with 3.3V.

In the circuit there are 3 voltage dividers, each one consists of 2.2K and 3.3K
resistors, they are used to step down 5V that comes from the arduino into 3V
which is sufficient for the SD card signals. The voltage dividers are used for SD
card signals: SCK (serial clock), MOSI (master out slave in) and SS (chip
select). The Arduino sends these signals from pins 13, 11 and 10 respectively.
The SD card MISO is connected directly to the arduino because this path is
used by the SD card to send data to the arduino (with voltage of 3.3V). The
master device is the arduino and the slave device is the SD card.
Connecting the SD card directly to the arduino without voltage level converters
or voltage dividers may damage it.

6.4. Code
// Read and write data to and from an SD card using Arduino
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
}
6.5. Result

As a result, I placed a samsung 2 GB micro SD card formatted with FAT16 file


system and I got the result shown below (Arduino IDE serial monitor):

Opening the file with Microsoft windows:

7. logger using SD card


7.1. introduction
Building a data logger using Arduino and SD card is so easy, this topic shows how to
build a simple temperature and humidity data logger with DHT11 sensor.
The DHT11 sensor is used to sense the relative humidity and temperature and
the SD card is used to save the values of the humidity and the temperature
every 1 second. The values of the temperature and humidity are saved in .TXT
file which is stored in the SD card.
7.2. Hardware Required
- Arduino board
- DHT11 sensor
- SD card
- SD card socket (connector)
- 10K ohm resistor
- 4.7K ohm resistor
- 3 x 3.3K ohm resistor
- 3 x 2.2K ohm resistor
- Breadboard
- Jumper wires

7.3. Circuit
Arduino datalogger circuit diagrams are shown below, both circuits are well
working.
The first circuit consists of three voltage dividers to step down the 5V into 3V,
the voltage dividers are for: SS (chip select), MOSI (master in slave out) and
SCK (serial clock) signals.
ARDUINO UNO

AREF

13 CLK
DO

SD Card
RESET 12
~11 DI

ARDUINO
5V ~10
~9 CS
8
POWER

GND ATMEGA328P
SD
ATMEL

DIGITAL (PWM ~)
7

UNO
~6 4.7K
1
A0 ~5 VDD
2 > 80
A1 4 DATA
4 27
A2 ~3 GND
ANALOG IN

A3 2 %RH °C
A4 TX > 1
DHT11
A5 RX < 0

RXD
VT52, VT100, ANSI
TXD

RTS
Xmodem, Ymodem, Zmodem
CTS

(All grounded terminals are connected together)

and the second circuit uses micro SD card module, this module is powered with
5V (comes from the Arduino board), it has AMS1117 voltage regulator and a
voltage level converter (74LVC125A) which converts the 5V signals into 3.3V for
lines: SS, MOSI and SCK:

(All grounded terminals are connected together)


With the micro SD card module the connections are more simpler, the sd card
module is supplied with 5V which comes from the Arduino board.
The SD card module has 6 pins which are (from left to right): GND, VCC,
MISO, MOSI, SCK and CS (chip select).
7.4. Code
// Arduino data logger with SD card and DHT11 humidity and temperature
sensor
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
#include <DHT.h> // Include DHT sensor library
File dataFile;
#define DHTPIN 4 // DHT11 data pin is connected to Arduino pin 4
#define DHTTYPE DHT11 // DHT11 sensor is used
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT library
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for native USB port only
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
delay(2000);
}
uint16_t line = 1;
void loop() {
delay(1000);
// Read humidity
byte RH = dht.readHumidity();
//Read temperature in degree Celsius
byte Temp = dht.readTemperature();
dataFile = SD.open("DHT11Log.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (dataFile) {
Serial.print(line);
Serial.print(": Temperature = ");
Serial.print(Temp);
Serial.print("°C, Humidity = ");
Serial.print(RH);
Serial.println("%");
// Write data to SD card file (DHT11Log.txt)
dataFile.print(line++);
dataFile.print(": Temperature = ");
dataFile.print(Temp);
dataFile.print("°C, Humidity = ");
dataFile.print(RH);
dataFile.println("%");
dataFile.close();
}
// if the file didn't open, print an error:
else
Serial.println("error opening DHT11Log.txt");
}
7.5. Result
As a result, I powered my circuit and after few seconds I turned it OFF, I
removed my 2 GB FAT16 SD card from the circuit and placed it in the PC, I
opened my SD card with windows and I got the file shown below:

Arduino IDE serial monitor gave me the window shown below:

7.6.

You might also like