members
Basics: Project 35a SD card or Micro SD card Module
of Acoptex.com in UNO
Basics: Project 035a
Project name: SD card or Micro SD card Module
Tags: Arduino, SD card Module, Micro SD card module
Attachments: cardinfosketch, writereadsdcardtestsketch
In this project, you needed these parts :
1.Aruduino Uno R3 (you can also use the other version of Arduino)
2.Arduino IDE ( you can download it from here )
3.Jumper cables
4. Micro SD card with adapter 1 pc
5. SD card module or Micro SD card module 1 pc
or
General
We will learn how to connect SD card module to Arduino board and use the SD card module with Arduino to read and write files on it.
Understanding the SD card module
Note: whenever referring to the SD card, it means SD and microSD sizes, as well as SD and SDHD formats.
In some Arduino applications, it is advantageous to be able to store and retrieve information locally. You can do this with a Secure Digital, or SD, card. An SD card is a non-volatile memory card used extensively in portable devices, such as mobile phones, digital cameras, GPS navigation devices, handheld consoles, and tablet computers. Another type of SD Card is the Micro SD card. Measuring only 15 mm x 11 mm x 1 mm, it is the smallest memory card available. It is about one-quarter the size of a normal-sized SD card, or about the size of a fingernail.
The SD card module is specially useful for projects that require data logging.
The Arduino board can create a file in an SD card to write and save data using the SD library. Some modules work with micro SD card.
There's an onboard ultra-low dropout regulator that will convert voltages from 3.3V-6v down to ~3.3V (IC2). There's also a level shifter that will convert the interface logic from 3.3V-5V to 3.3V. That means you can use this board to interact with a 3.3V or 5V microcontrollers.
There are different models from different suppliers, but they all work in a similar way, using the SPI communication protocol. Because SD cards require a lot of data transfer, they will give the best performance when connected up to the hardware SPI pins on a microcontroller. The hardware SPI pins are much faster than 'bit-banging' the interface code using another set of pins.
As shown in the figure below, a micro SD card has 8 pins. The table describes the function of each pin.
Pin Name Description
1 NC not connected
2 CS Chip Select/Slave Select (SS)
3 DI Master Out/Slave In (MOSI)
4 VDD Supply voltage
5 CLK Clock (SCK)
6 VSS Supply voltage ground
7 DO Master In/Slave Out (MISO)
8 RSV Reserved
If you were to try interfacing this SD card yourself, you would have to ensure that you connected the pins of the SD card to the appropriate pins of the Arduino. Since we are using a commercially-available shield, this is not an issue.
SD card module features:
- Support card type: Micro SD ( <= 2G ), Micro SDHC ( <= 32G )
- Operating voltage: 3.2V - 5.5V (Onboard 3.3v LDO Regulator)
- Interface logic voltage: 3.3V / 5V compatible(Onboard level shift circuit)
- Logic Interface Type: Standard SPI port.
- Working mode: SPI mode.
- Applications: DIY projects with breadboard, Storage module, MP3 player memory.
- Compatibility: can be directly connected to Arduino board or Raspberry Pi
See the more information about SD card here.
Signals and connections of the SD card module
Note: depending on the module you’re using, the pins may be in a different order.
VCC (5V) - connect to 5V pin Arduino Uno. If it is just VCC pin and no 3V3 pin connect VCC to 3.3V pin of Arduino Board
3V3 (or 3.3V) - connect to 3.3V pin Arduino Uno
CS (or SS or D3) (Chip Select or Slave Select) - the pin on each device that the master can use to enable and disable specific devices
MOSI (or DI or SI or CMD) (Master Out Slave In) - The Master line for sending data to the peripherals
CLK (or SCK) (Serial Clock) - The clock pulses which synchronize data transmission generated by the master
MISO (or DO or SO) (Master In Slave Out) - The Slave line for sending data to the master
GND (or G) - ground
CD - this is the Card Detect pin. It shorts to ground when a card is inserted. You should connect a pull up resistor (10K or so) and wire this to another pin if you want to detect when a card is inserted.
Build the circuit
SD card module -> Arduino Uno
VCC -> 3.3V or 5V (check module’s datasheet)
CS -> 4 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(). Note that even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work. Different boards use different pins for this functionality, so be sure you’ve selected the correct pin in SD.begin().
MOSI -> 11
CLK -> 13
MISO -> 12
GND -> GND
Note: different Arduino boards have different SPI pins. If you’re using another Arduino board, check the Arduino SPI documentation.
The following picture shows the needed connections with the Arduino Uno
Code
The SD library provides useful functions for easily write in and read from the SD card.
To write and read from the SD card, first you need to include the SPI and SD libraries:
#include
#include
You also have to initialize the SD card module at the Chip Select (CS) pin – in our case, pin 4.
SD.begin(4);
To open a new file in the SD card, you need to create a file object that refers to your data file. For example:
dataFile = SD.open("data.txt", FILE_WRITE);
The first parameter of this function is the name of the file, data.txt, and the FILE_WRITE argument enables you to read and write into the file.
This line of code creates a file called data.txt on your SD card. If the data.txt file already exists, Arduino will open the file instead of creating another one.
To write data to the currently open file, you use:
dataFile.write(data);
In which the dataFile is the file object created previously and the data is what you want to write in the file.
You can also use the print() or println() functions to print data into the file:
dataFile.print(data);
dataFile.println(data); // followed by a new line
To read the data saved on your file:
dataFile.read();
You can only write within a file at once, so you need to close a file before proceeding to the next one. To close the data.txt file we’ve just created:
SD.close("data.txt");
The argument of this function is the file you want to close, in this case data.txt.
File Naming
FAT file systems have a limitation when it comes to naming conventions. You must use the 8.3 format, so that file names look like “NAME001.EXT”, where “NAME001” is an 8 character or fewer string, and “EXT” is a 3 character extension. People commonly use the extensions .TXT and .LOG. It is possible to have a shorter file name (for example, mydata.txt, or time.log), but you cannot use longer file names
Step by Step instruction
-
Most SD cards work right out of the box, but it's possible you have one that was used in a computer or camera and it cannot be read by the SD library. Formatting the card will create a file system that the Arduino can read and write to.It's not desirable to format SD cards frequently, as it shortens their life span.Most SD cards work right out of the box, but it's possible you have one that was used in a computer or camera and it cannot be read by the SD library. Formatting the card will create a file system that the Arduino can read and write to. It's not desirable to format SD cards frequently, as it shortens their life span. You’ll need a SD reader and computer to format your card. The library supports the FAT16 and FAT32 filesystems, but use FAT16 when possible. See additional info here.
- Format the SD card as FAT16 or FAT32. Insert the SD card in your computer. Go to My Computer and right click on the SD card. Select Format...
- A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.
- Insert the formatted SD card in the SD card module.
- Connect the SD card module to the Arduino Uno board.
- Open Arduino IDE.
- Plug your Adruino Uno board into your PC and select the correct board and com port
- Open up serial monitor and set your baud to 9600 baud
- Verify and upload the the sketch to your Adruino Uno board.
- If everything is working properly you’ll see a similar message on the serial monitor.
Summary
We have learnt how to connect SD card module to Arduino board and use the SD card module with Arduino to read and write files on it.
Library:
- SD library included in Arduino IDE
- SPI library included in Arduino IDE
Sketch:
- See attachments on the begining of this project description.
Other projects of Acoptex.com










jobs.viewed