by Floris Wouterlood – September 2, 2022
Summary
In some Arduino projects the necessity is felt to save collected data. One of the solutions, especially in mobile projects, is to store data on a SD card. We focus here on wiring an external SD card reader with, respectively, an Arduino Nano, ESP8266 Wemos D1 mini and ESP32 WROOM-32 microcontroller board.
Introduction
An Arduino is very well suited for data collection from a huge spectrum of sensors and for displaying this data attractively on LCD, OLED or TFT screens. In many projects there is demand for data storage. Arduinos lack permanent internal data storage. Power off = data lost. There are several options however to save valuable data. A user-friendly solution, especially in mobile situations where wifi-connections are unstable or unavailable, is a SD card shield that sticks right on top of an Arduino Uno. A derivative is the TFT display shield equipped with a SC card slot. The classical Arduino Nano lacks shield functionality and hence needs an external SD card reader. The same holds also for the more powerful ESP8266 microcontroller boards and for the mighty ESP32. We explore here therefore an external SD card reader in combination with these common microcontroller platforms: Arduino Nano, a member of the ESP3266 microprocessor chip family: the Wemos D1 mini and, finally, with an ESP32 WROOM microcontroller board.
Arduino Nano and external SD card reader
It should be noted first that all SD cards operate exclusively at 3.3V. This implies that the Arduino Uno and Nano with their 5V pin signals work properly only with SD card readers that are equipped with a low dropout linear voltage regulator (e.g. type AMS 1117) and a power logic voltage level shifter (a chip with identification VHC T125A – see figure 1, double asterisk).
A SD card reader communicates via the SPI protocol. Apart from power and GND a card reader needs four wires: chip select (CS), serial clock (SCK), data out (MOSI), and return data (MISO) (fig. 1). SCK, MOSI and MISO have fixed pins while CS is the variable pin, one that needs to be declared in the sketch code such that the controller knows which device to address (SCK, MOSI and MISO pins from multiple SPI devices can be connected paired with their designated hardware Arduino pin while the CS pins should have an unique pin).
SPI-hardware pins on the Nano are: pin 13 for SCK, pin 11 for MOSI and pin 12 for MISO. One is free to select a CS pin. The SD.h library supports the hardware SPI pins.
Figure 1 shows the wiring of a SD card reader to an Arduino Nano. CS goes in the current setup to pin D4 of the Nano, while other pins can be selected, to serve CS if necessary e.g., pin D8 or D10 depending on the board one is working with or with the requirements by peripheral devices such as sensors.
figure 1. External SD card reader wired to an Arduino Nano. Note the AMS 1117 voltage regulator (asterisk) and the VHC T125A control logic voltage shifter (double asterisk) on the SD card reader.
The reason that it is important to select a proper CS pin is that the SD card device is enabled by pulling its CS pin low. If that doesn’t happen the card will not be recognized and an error message will be returned.
Many SD cards, as long as they are formatted in FAT32 configuration, are recognized. While originally the capacity of SD cards was in the megabyte range, it has gone up incessantly to the point that that modern micro-SD cards address many, many gigabytes. I tested standard size SD cards up to 8Gb and micro-SD cards (inserted through a SD card adapter) up to 32 GB. All cards in this range were recognized, that is: files could be read and files written. Note that in all cases the file name convention ‘8×3’ that goes back to the very days that computers were running the CPM operating system.
A test sketch is available at the end of this article. The Arduino SD.h library supplies a number of examples that can be used to test your SD card and SD card operations: Arduino IDE, menu File, Examples, SD and then one of the selections.
ESP8266 and external SD card reader
We are discussing the Wemos D1 mini here because the small footprint of this microcontroller board makes it attractive to become incorporated in highly mobile projects that are independent from wifi connections. Connectivity of a SD card reader with the D1 mini is illustrated in figure 2: pin D5 for SCK, pin D7 for MOSI and pin D6 for MISO. CS is connected to pin D0. A test sketch is available at the end of this article.
figure 2. Wiring diagram and connectivity table for an external SD card reader and a Wemos D1 mini. The D1 mini has an ESP8266 chip on board.
Example: Wemos D1 mini, temperature sensor and data collection on SD card.
As soon as the combination ESP8266 and card reader has been made operational and testing has been completed successfully an external sensor can be added. In this example we are using a DS18B20. This sensor features in many of my projects, collecting data in a very handy, economical, accurate and reliable manner. It has been discussed in its own post (*). A DS18B20 operates without problems in both 5V and 3.3V environments and is compatible with all Arduino microcontrollers that I have so far tested. Its ‘single bus’ feature makes many sensors connected to one pin possible, with only one data wire necessary. Support of long wires is one of the strengths of this sensor. Distances of up to 50 meter wire length have been reported. I2C, and to a lesser degree SPI devices, are much more critical as regards wire length.
Each individual DS18B20 temperature probe has its unique ID consisting of an 8 byte identifier. The microcontroller calls this ID upon which the sensor reports with two bytes of data. This process takes about 750 msec. This rather long refractory period is a single disadvantage of this sensor. The device works properly when a 4.7K kΩ resistor is added in pull up configuration (figure 3).
figure 3. DS18B20 temperature sensor, Wemos D1 mini and external SD card reader combined. The data pin of the temperature sensor is wired to Wemos pin D2. The led is added to signal certain operations, e.g. writing to SD.
The sketch creates an array to store 16 temperature readings. Because temperature data is presented by the sensor in tenths of degrees a decimal is necessary and hence the temperature value must be a stored as a ‘float’ parameter. Following this the instructions in the sketch create a file to which the 16 temperatures are written. After a pause another set of instructions reads the temperature string from file, decodes the string into 16 ‘float’ values that fill the temperature value array.
ESP32 and external SD card reader
According to the official ESP32 documentation the ESP32 has four SPI peripherals of which the first two (SPI0 and SPI1) are being used internally. This leaves SPI2 and SPI3 free for general purpose applications. Thus at hand are two available hardware pin configurations while it is otherwise possible (according to TechOverflow, website) to use any pins for SPI connectivity. SPI2 designates pins as follows: SCK pin D14, MISO pin D12, MOSI pin D13 and CS pin D15. SPI3 uses SCK pin D18, MISO pin D19 and MOSI pin D23, with CS at pin D5. We are using here SPI3 (figure 4). A test sketch is available at the end of this article.
Because the ESP32 has 32-bit architecture the instructions to read and write to SD card are slightly different from those used in the ESP8266 and Arduino environments.
figure 4. Wiring diagram and connectivity table of a 30-pin ESP32 platform and an external SD card reader through the SPI3 wiring scheme.
References
(*) Floris Wouterlood – The DS18B20 temperature sensor – implementation with an Arduino. TheSolarUniverse, August 17, 2017.
Espressif documentation about SPI in their ESP32 processors:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html
Sketches
nano_DS18B20_SD_card.ino
ESP8266_wemos_d1_mini_DS18B20_SD_card.ino
ESP32_SD_card.ino
Download
arduino_and_ESP_SD_cards.zip.
This file contains the SD card sketches mentioned above supporting an external SD card reader with these different platforms.