This is how it looks.

I'm working on a project that will log data to an SD card and display a user interface on an OLED. The SparkFun OLED breakout was super easy to set up. I was also able to get the SD-Card library working independently, though that was harder.

The trouble is when I combine these examples the SD card only writes during "setup" after that it isn't writing to the SD card as the loop continues. The OLED still works just fine. I've read up on SPI a bit and tried various things to get "chip select" to work (I suspect this is the problem) I even tried setting the CS pins to LOW or HIGH using digitalWrite, but this had no impact.

My pins are connected as follows:

          SD CARD      OLED  
MOSI        A5            A5     same pin
MISO        A4            --     just SD connected
SCK         A3            A3     same pin
CS          A1            A2     different pins

The SD card writes in the main loop if I comment out all of the code that runs the OLED. The OLED works either way. What could I try next?

Here is the code:

 #include "application.h"
 #include "sd-card-library/sd-card-library.h"
 #include "SparkFunMicroOLED/SparkFunMicroOLED.h"
 #include "math.h"

 // MicroOLED Object Declaration //
 // Declare a MicroOLED object. If no parameters are supplied, 
 // default pins are used, which will work for the Photon Micro 
 // OLED Shield (RST=D7, DC=D6, CS=A2)
 // Since I use A1 as the chip select pin for the SD card these
 // defaults seem fine.

    MicroOLED oled;

 // Declare a file object?

    File myFile;

 // These are the pins for the SD card. A5, A4, and A3 are all 
 // shared by the OLED.

    const uint8_t chipSelectSD = A1;
    const uint8_t mosiPin = A5;
    const uint8_t misoPin = A4;
    const uint8_t clockPin = A3;

 // setup //
 void setup() {

  SD.begin(mosiPin, misoPin, clockPin, chipSelectSD);
  File dataFile = SD.open("data.txt", FILE_WRITE);
  dataFile.println("this text was printed to the SD card during setup");

  oled.begin();  // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  delay(1000);  // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.
  randomSeed(analogRead(A0) + analogRead(A7)); //seed to make test pattern random.

               }//end setup

// main loop //
void loop()  {

  SD.begin(mosiPin, misoPin, clockPin, chipSelectSD);
  File dataFile = SD.open("data.txt", FILE_WRITE);
  dataFile.println("The text was added during the loop.");

//test the display with text and pixels. 
  printTitle("Pixels", 1);
  for (int i=0; i<512; i++)
  { oled.pixel(random(oled.getLCDWidth()),  random(oled.getLCDHeight()));
  oled.display(); }

               } //end loop

// function to print titles //
  void printTitle(String title, int font) {
  int middleX = oled.getLCDWidth() / 2;
  int middleY = oled.getLCDHeight() / 2;
  // Try to set the cursor in the middle of the screen
  oled.setCursor(middleX - (oled.getFontWidth() * (title.length()/2)),
  middleY - (oled.getFontWidth() / 2));
  // Print the title:
                                           } //end title function 

enter image description here

This is how the SD card is hooked up.

  oled.begin();  // Initialize the OLED

Any attempt to use the SD card after this line will not work. I've looked for something like "oled.end()" but have not found a way to reset to the state where the SD card works.

I have tried commenting out all of the OLED code and then the SD card writes fine in the main loop. So, I started adding the OLED code back in. This line of code breaks the SD card writing in the main loop:

  • Try interfacing with the OLED using I2C. That might avoid the contention with the SD card.
    – Nick Gammon
    Aug 15, 2015 at 5:43
  • Try explicitly setting the CS pin used by MicroOLED oled(rst, dc, cs)
    – Gerben
    Aug 15, 2015 at 9:16
  • I2C is notoriously buggy on this micro. I'll see if anyone has it working. I added "MicroOLED oled(MODE_SPI,D7, D6, A2);" it still has the same error.
    – futurebird
    Aug 15, 2015 at 10:13
  • There are devices that "break" the bus by not being able to work on systems that have more than one SPI slave. In that case you can do nothing to fix it but use devices that don't have this behaviour.
    – Avamander
    Jan 2, 2016 at 12:53

1 Answer 1


I wanted to write to 2 SD cards simultaniously with one Arduino Mega. Used all the relevant pins including CS on 53 for the first SD card and 13 for the second SD card. Can be any digital pin I think but this one makes the LED blink too). Before the SD.begin(13) of the second SD card I do a digitalWrite(53,HIGH). That de-selects the first SD card. and digitalWrite(13,HIGH) when switching back to SD.begin(53) I do a digitalWrite(13,HIGH). This works real fine.

Now I tried to impelment this on a D1-Mini. Cannot get it to work.
SD.begin(D8) starts the first SD fine. By doing a digitalWrite(D8,HIGH) I tried to de-select SD1 and with SD.begin(D4) opened the second card. Strangely enough it still opens the first SD card again regardless of the pinnumber-parameter.
Maybe the SD library for ESP8266 remembers the first setting of the CS and later settings og the parameter with fifferent pinnembers are ignored???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.