Lab5-SD-Card
Lab5-SD-Card
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().
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
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.
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:
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
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
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:
7.6.