If your electronics project requires the use of an SD card, the solution seems to be pretty easy: purchase a very cheap SD Card Module, wire it to your controller, probably an Arduino, load the libraries and that's it.
Well, not really. In this article, we shall examine the case and understand why we need to spend some more effort to get the job done.
SD cards by themselves provide an SPI operation mode, meaning that read and write operations can be performed using the standard SPI wires (MISO, MOSI, SCK) plus a selection wire. What else is expected from an SD card module to integrate it properly and adequately into a project? I am presenting below a collection of such criteria:
Let us examine the SD card modules existing in the market. I have found and used two modules, both having very low prices. Unfortunately, the module schematics are not available in the seller web sites. Therefore, one would need to reverse engineer the module by tracing the PCB, in order to have a complete view about how the interface to the controller will be designed and implemented.
For the impatient, here is a comparison table:
Catalex | LC Studio | |
---|---|---|
SD card size | micro SD | Standard SD |
Voltage | 5V | 5V, 3.3V |
Level shifter | Yes | No |
SPI | Partial. Does not allow other devices on SPI bus | Exposes SD card pins |
Write-enable detection | No | No |
This module is advertised as an Arduino compatible and SPI compatible module. It can accomodate only microSD cards, so if your project needs to use standard SD cards, this module is not suitable for your purposes.
It contains internally a 5V to 3.3V converter, but it does not expose the 3.3V to the module connectors. The module is equipped with a 74LV125 chip which operates as a level shifter. The module schematic is shown below:
In all aspects, its design seems superior to the LC Studio module, at least for 5V powered projects. You can wire it to an Arduino, load the libraries and run the examples. It will behave properly and read and write to an SD card. All will be fine….unless your development project has another SPI module wired to the same controller!!!!
Examining the board schematic which I have recreated by tracing the wires on the PCB, we find a major flaw in the module design. SPI is supposed to be a multi-participant bus where several “slaves” can interface to one or multiple “masters”. When a participating module is not transmitting information to the bus, it should maintain a high-impedance (or else “high-Z”) state to avoid interfering with the active participants. Unfortunately, in this module, pin 13 of the 74125 is permanently tied to 0V, keeping the driver active and passing to the MISO output the logical state of the MISO SD card pin, whether that is 0 or 1.
As a result, the module fails miserably in criterion 4, meaning that the module will not operate if another SPI device shares the same bus. Moreover, it will also block the other device from operating properly. Could we ask a refund from the supplier? Well, probably no. The module specs do not state that the board can co-exist with other SPI devices, only that it can operate by itself in SPI mode, which is actually a function of the SD card.
Could we tamper with the module to make it operate properly? Well, in design terms, yes. After all, the only thing required is to remove pin 13 from ground and connect it to pin 8, which is the module selection signal. (You can see below the schematic of the LC Studio extension circuit to see exactly how it is done.) But in operational terms, this means that we need to desolder very carefully pin 13 of an SMD chip, find a way to raise it in the air without breaking, keep it in the air without touching the board, solder a very small wire to it and solder the other end of the wire on the chip's pin 8. Another “hack” would be to de-solder the SMD chip, provided that you have the proper equipment, cut the trace to pin 13, resolder the chip and then connect a wire between pins 8 and 13. Both these arrangements have strong problems of mechanical nature, therefore you either find a good way to do the above actions and subsequently enclose the entire fix into a thick layer of resin to protect it from vibrations and similar wear, or just forget it.
It would be expected that the manufacturers will have identified this flaw and make the correction, but even after several years I have not yet seen a revised version!
This module can accommodate standard sized SD cards, meaning that we can use microSD cards with the help of an adapter. It is powered by a 5V source and contains internally an AMS1117-3.3 linear regulator which converts to 3.3V. A bonus(?) is that the 3.3V output of the converter is available to the module connectors and can be used to power devices external to the module, provided that the consumption does not exceed the AMS1117-3.3 specs. From my point of view, using this regulator as a power source for external devices may cause problems that will be very difficult to locate in the future. It is better not to use it, unless you know exactly what you are doing (who admitts it, is another story….).
The 3.3V pin can also be used as an input to the module, if a 3.3V external source is available. The AMS1117-3.3 will not complain if a 3.3V is applied to its output. In such a case, we should make sure that the 5V pin is left floating, without any other power source connected to it.
The board provides only the socket and the voltage converter, while all wiring of the SD card itself and the connector is direct. The few resistors serve only to keep the pins at a steady state and the few capacitors are utilized in the voltage converter.
The module interfaces with the world with a double set of headers. The two pins of each row are wired together, so you can use any of the two. If our project is based on a 3.3V power supply and we do not need to use the level shifter below, we could utilize the double pins to continue our wiring to other SPI devices, connecting the appropriate signals in parallel, except the CS pin of course, since each device should be selected individually.
A schematic of the module can be found online. J1 is the SD card socket and the named signals are connected from the socket to the headers, which is only implied in the schematic.
No copyright is indicated on the schematic. If otherwise, please let me know.
It is obvious that this SD Card Module complies only with criteria 1 and 2 set in our Introduction section. The module will of course operate if wired to a 3.3V Raspberry Pi, but shall it survive the 5V Arduino voltages? One can find several internet pages which advice that this is safe, because “several” SD cards can tolerate 5V signals. My approach is that if your project requires the use of an SD card, the type of SD card and its specifications are totally outside the control and the original intentions of the designer. Even users who had been warned about specific precautions, will (and shall) behave eratically at some point in time. We must therefore assume that our project will receive various SD cards from several origins and manufacturers. I need to point out that the module does not buffer in any way the SD card pins, so whatever voltage we send, goes directly to the SD card. Such wiring will probably have no effect on the controller but will rather damage the SD card itself and risk the loss of its valuable data. This is absolutely not acceptable for a reasonable design. An addition of a level shifter, external to the module, is mandatory for safe operation in all circumstances where a 5V architecture is implemented. In the case where the project architecture is based on 3.3V, the module can be used without any problem.
Can we expand the module with a level shifter, while at the same time allow the board to work in a multi-participant SPI bus? I have developed the circuit that is depicted in the following diagram for that purpose. We are adding an external chip which operates both as a voltage level shifter and as a chip selector for the SPI bus. This chip is the 74LV125 or 74HCT125N. This is the same chip type used in the Catalex module, in a DIP version, so that we can work with it easily. (I was not able to find in the market a PDIP 74LV125, so I used an 74HCT125 instead. It is specified at 5V, whereas the 74LV125 works from 2V to 5.5V. Yet it worked, but I cannot guarantee that it will work at all circumstances.) In this circuit, the wiring allows multiple participation to the SPI bus, because the MISO output (as seen from the module side) is activated only when the controller has explicitly indicated that it intends to communicate with the module, activating the CS pin, which subsequently activates pin 8 of the 74xx125 chip and finally brings the buffer handling the MISO signal out of the high impedance state. Whenever CS is not active, MISO output is kept at the high impedance state, leaving other modules and devices free to use the SPI bus. We could also wire pin 8 to pins 4 and 10 instead of wiring 4 and 10 to ground, but I just wanted to point out in its simplest form the correction that would be required to the Catalex module to make it operate properly. When CS is not active, the SD card by itself ignores the signals in its inputs, so it is OK to have pins 4 and 10 connected to ground.
The chip is powered by the 3.3V which is present in the module connector, so, although we hated it above, we are exploiting it to our benefit and avoid the need of an extra power source. The schematic is shown below:
I have implemented and tested this configuration where the SD card shared the SPI along with an Ethernet ENC28J60 module. The SD card used pin D4 as Chip Select and the Ethernet module used pin D10. Everything was operating properly.
Of course the addition of one chip and some resistors in a project increases the board space and wiring effort. But, it is the only solution in order to construct a proper module.
Both modules fail to complying with criteria 5 and 6. Yet, if your project may encounter un-suspected users who believe that setting the “Lock” tab protects their valuable data from overwriting, you should think otherwise about using such a module!!!
The Catalex module would be the module of choice for 5V projects with its internal level shifter, but only if it would be the only single device on the SPI bus and if only microSD cards are supported.
The LC Studio module is no more than a standard-sized socket and a 3.3V power source. This can be good and bad. Good because you can build whatever you like around the module and even use microSD card, using of course an adapter. Bad, because you need to use discrete components to implement a level shifter.
I have spotted some modules in the internet shops which contain only a socket. If no 3.3V converter is required because there is another such source, probably that would be the best choice. I am afraid that I have not worked with any such module and therefore I cannot express an opinion.
One would reasonably ask, if we build everything externally, why do we need a module and not use directly a simple SD card socket? Check the SD card specifications or visit the Wikipedia page on SD Cards, notice the non-standard spacing of the pins (at least in the standard-SD card), consider the electro-mechanical issues dealing with sockets and connectors in hobby projects and you will understand why. Yet, for hobby projects and due to the market logic, the cost of the LC Studio module may be lower than the cost of a single socket. So, perhaps it is better to go with the LC Studio module and endure the additional burden of external level shifters.