Install an SD card reader
published: 1 November 2020 / updated 2 November 2020
Preamble
An SD card reader allows an ARDUINO card to access mass storage:
- to read execution instructions or data;
- to save data.
Take the case of a temperature and atmospheric pressure recorder. With an SD card, you will be able to record readings at high density, for example every minute.
The SD card is installed in a card reader of this type:
This card reader uses the SPI interface.
You will find the program for managing the SPI interface here:
Words to drive the SPI module on the ATmega2560
This version works for the ARDUINO MEGA card. It must be adapted for NANO and UNO cards.
SD card reader pins
The GND terminal is connected to 0 Volts (ground).
The VCC terminal is connected to 5 Volts.
Meaning of the other terminals:
- MISO: Master In, Slave Out. Other abbreviations: SDI, DI, SI
- MOSI: Master Out, Slave In. Other abbreviations: SDO, SDA, DO, SO
- SCK Serial ClocK. Other abbreviations: SCLK, SCL
- CS Chip Select. Other abbreviations: SS (Slave Select), nCS, CS, nSS, STE, CSN
In the case of the SDI/SDO naming convention, the SDO of the master must be connected to the SDI of the slave and vice versa. To avoid confusion when wiring, it is therefore often recommended to use MISO-MOSI names which avoid some ambiguity.
Schémas de connection pour les cartes ARDUINO
Pour commencer, on va créer le mot ?\
qui permet de gérer la
compilation conditionnelle:
: ?\ ( fl --- ) 0= if postpone \ then ; immediate \ set the card you are using to true true constant MEGAcard false constant NANOcard false constant UNOcard
Ici, nous avons mis true dans la constante MEGAcard
. Si vous utilisez
une autre carte, mettre à true votre carte et mettre à false les autres cartes.
Pour les cartes ARDUINO MEGA, NANO et UNO, l'interface SPI est disponible sur le port B:
\ Registers of interest $24 constant DDRB \ Port B Data Direction Register $25 constant PORTB \ Port B Data Register $4c constant SPCR \ SPI Control Register $4d constant SPSR \ SPI Status Register $4e constant SPDR \ SPI Data Register
Raccordement à une carte ARDUINO MEGA
ARDUINO | SD CARD | ||
---|---|---|---|
pin | sig. | pin | sig. |
50 | PB3 MISO | MISO | |
51 | PB2 MOSI | MOSI | |
52 | PB1 SCK | SCK | |
53 | PB0 SS | CS |
\ bit masks for ARDUINO MEGA MEGAcard ?\ %00000001 constant mSS1 ( PB0 ) \ -> CS MEGAcard ?\ %00000010 constant mSCK ( PB1 ) \ -> CLK MEGAcard ?\ %00000100 constant mMOSI ( PB2 ) MEGAcard ?\ %00001000 constant mMISO ( PB3 )
Raccordement à une carte ARDUINO NANO
ARDUINO | SD CARD | ||
---|---|---|---|
pin | sig. | pin | sig. |
17 | PB5 SCK | MISO | |
16 | PB4 MISO | MOSI | |
15 | PB3 MOSI | SCK | |
14 | PB2 SS | CS |
\ bit masks for ARDUINO NANO --- à vérifier et tester NANOcard ?\ %00000100 constant mSS1 ( PB2 ) \ -> CS NANOcard ?\ %00010000 constant mSCK ( PB5 ) \ -> CLK NANOcard ?\ %00000100 constant mMOSI ( PB3 ) NANOcard ?\ %00001000 constant mMISO ( PB4 )