Hardware Reference
In-Depth Information
Writing to an SD Card
Arduino's SD library makes it easy to write to an SD card,
and there are several SD card-mount options available. All
of them use the SPI synchronous serial protocol that the
Arduino Ethernet shield and Ethernet board use to com-
municate with the Ethernet chip. As you saw in Chapter
4, this protocol can be used with a number of different
devices, all attached to the same serial clock and data
pins, but each device will need its own Chip Select pin. The
Arduino communicates with all SPI devices through pin 11
for MOSI (Master Out, Slave In), Pin 12 for MISO (Master
In, Slave Out), and pin 13 for Clock. The Arduino Ethernet
shield and board use pin 10 for the Ethernet module's Chip
Select, and pin 4 for the SD card's Chip Select. SD boards
from other companies use other pins for chip select.
Spark Fun's SD card shield uses pin 8, and Adafruit's SD
card breakout board (shown in Figure 10-9) uses pin 10.
Whenever you're using multiple SPI devices in a project,
like the Ethernet and the SD card together, you need to
check to see that they all have individual Chip Select pins.
MADE
IN I TALY
DIGITAL (PWM ~ )
- +
L
UNO
TX
RX
ON
ARDUINO
RESET
ICSP
POWER
ANALOG IN
Figure 10-9
The Adafruit SD card shield connected to the
Arduino using the SPI pins 11 (MOSI), 12 (MISO), and
13 (Clock). Different SPI card boards use different
pins for the Chip Select pin, however. This one uses
pin 10.
SD cards operate on 3.3V only! So if you're using a socket
that attaches your SD card directly to the pins of any
microcontroller operating 5 volts, you need to make sure
the input pins receive only 3.3V.
Good SD Card Practice
To get the most reliable results out of an SD card shield
or an adapter with an Arduino, there are some habits you
should develop.
are not allowed. So, datalog1.txt and mypage.htm are OK, but
really long file name or someArbitraryWebPage.html are not.
Writing to the card takes time. Normally, the file write() ,
print() , and println() operations saves data in a volatile
buffer on the card, which is lost when the card is removed.
Only the flush() or close() operations save to the card per-
manently, but they take time. To keep up with user inter-
action, use flush() and close() the way you would to save
a file on a regular computer: do it frequently, but not too
frequently, and preferably when the user or other devices
aren't doing anything else.
Never insert or eject the card while the sketch is
running. You'd never pull a card out of your computer
while it's in use, and the same goes for the Arduino. Make
sure the microcontroller's not using the card when you
insert or remove it. The simplest way to do this is to hold
the reset button of the Arduino whenever you're inserting
or removing the card.
Format cards as FAT16 or FAT32. The SD card library
works only with these two file formats. Fortunately, every
operating system can format disks this way. Check your
operating system's disk-utility application for how to
format your card as FAT16 or FAT32.
Practice safe file management. The SD library gives
you some helpful tools for managing your files. filename.
exists() lets you check whether a file already exists.
if (filename) lets you check whether the file can be
accessed. filename.remove() lets you remove a file. size() ,
position() , seek() , and peek() let you see how big a file
is, where you're at in the file, and to move around in the
file. mkdir() and rmdir() let you make and remove whole
directories. Use these methods, especially those that let
Filenames have to be in 8.3 format. The SD library uses
the old DOS file-naming convention that all filenames are a
maximum of eight-characters long with a three-character
exension. All file names are case-insensitive and spaces
Search WWH ::




Custom Search