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() {
delay(500);
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");
dataFile.close();
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.");
dataFile.close();
//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(); }
delay(9000);
} //end loop
//-------------------------//
// function to print titles //
//-------------------------//
void printTitle(String title, int font) {
int middleX = oled.getLCDWidth() / 2;
int middleY = oled.getLCDHeight() / 2;
oled.clear(PAGE);
oled.setFontType(font);
// 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:
oled.print(title);
oled.display();
delay(1500);
oled.clear(PAGE);
} //end title function
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:
MicroOLED oled(rst, dc, cs)