SD Card Write - SPI problem help please

I am trying to integrate logging to an SD card in my project. I have tried both the DevicePrint and FileLogger libraries and have the same problem with either.

Basically, the problem is that after I call the function to write the card, I can no longer display on my led matrix.

Both the DevicePrint and FileLogger libraries use the mmc lib. to write to the card with SPI. For the matrix, I use the LedControl library which has it's own "soft" SPI. This means that I don't even have to share the clock, MOSI, and MISO pins between the two devices. (I started by sharing them and now I'm only sharing the MOSI pin and still have the same problem.)

I am using an ATmega328 and I checked SRAM with availableMemory() and I have over 600 bytes free.

Looking at the library sources, I see that LedControl::spiTransfer function simply uses digitalWrite for it's SPI. The mmc library uses the ports directly to get hardware SPI on pins D10-13.

To restate the problem, the LedControl library works fine until I write the card using either the DevicePrint or FileLogger. Then the card will write fine, but the led matrix quits working until I press reset.

What's odd is that the more "scary" stuff like I2C and external interrupts 0 & 1 continue to work after an SD write. Only the second "soft" SPI device quits working.

I suspect that the mmc lib might be leaving something in a weird state, but what its' doing is beyond me.

I write this with the fear it's too gnarly to answer, but I would sure appreciate any guidance or tips on how to isolate this problem. I'm really stumped!

Thank you, John

Hi, I could have a look at the "mmc-lib", but Google doesn't find it. Do you have a link to the mmc-lib source?

What happens if you use a different IO-port for LedControl? Could you for instance try connecting the matrix to some of the AnalogPins? (You can use them for digital IO just like the "normal" IO-pins. See the note at the bottom of this page if you didn't know that)

The analog-pins are on PORT-C so there should be no interference with the mmc-lib, if writing to a full port is causing the trouble.


Wow! Nice to hear from you.

You can get the mmc lib as part of the zip for FileLogger here . . .

That’s a good suggestion about switching to another port. I did try moving the CS and SCLK pins for LedControl but probably still on the same port. I will try them on PORT-C as soon as I can. (I’ve gotten pretty good at sliding just the pin out of the header! )

Thanks for that suggestion.

BTW - I’m trying to interface the SD card to this

Hi BroHogan,

I'll try to take a look into the mmc library to see if I can give you some clues on what's going wrong with the library in this setup.

Well, I took wayoda's advice and split the last of the shared SPI pins, and moved the matrix MOSI to pin 17. It works! :)

I spent a little time looking at the mmc-lib and looks to me to be well behaved, but I'm not experienced enough to be sure.

As I said, I have a lot going on - 2 pin interrupts, an overflow interrupt, I2C, and 2 SPIs (all working now!) but even so, something does not seem right after card write.

For example, I have a peizo on pin 8 that uses digitalWrite to make it's freq. If I call it directly after SD "append" I just get a quick click. Later the same function makes it work. I also had a tilt switch on pin 6 - pulled high. I could only write to the card when the switch was closed! (This problem went away when I got the matrix working.)

I certainly can live with what I have - writing to the card is so cool! - even though I used my last 2 free pins for the second SPI. However, others may have similar issues down the road.

If the mmc-lib looks OK, I'll make a simple sketch with just the matrix and FileLogger and put a scope on the pins.

Sorry this is so long, and thank you both for your help. :)



I don’t know if this can help, but I’ve uploaded a new release of the library, V05. I have removed from MMC the SPI related code, and I have coupled MMC to the Arduino SPI Library instead, so maybe this problems with SPI and this library are fixed.
By the way, if you are using SPI in your projects, you can remove Spi.cpp from FileLogger library, it will work.
Together with some small changes in code, we have won 163 bytes in Flash memory in this release, so total size is 3601 bytes now.

You can find the last release, together with some info and source code, here:

Enjoy it,

Eduardo García.

Thanks Eduardo! That seems to work great (although I haven't started re-sharing my SPI pins yet). Also, using the SPI-lib allows the SPI pins to be easily changed to any pins.

Just out of curiosity, did you find a problem with the way the mmc-lib handled the port?

Thanks again for creating a nice clean way to write an SD card. John


I changed to standard SPI library just to join the "main stream" if you know what I mean, just to assure that any improvement or bug fix that somebody do to this library will be automatically applied to my library. On the other hand, I find the code more stable and, best of all, more compact (smaller) after doing this change, so although I cannot assure that there was something faulty or wrong in the original mmc library related to SPI, I can tell you that everything works better now.

Thanks for your insterest and comments,