This is the core project, a basic data logger.
The SD is 3.3v and hooked up accordingly.
The OLED and RTC are 5v and also hooked up to the correctly. [omitted for clarity]
The OLED and RTC w/flash interfaces with I2C.
The SD card reader interfaces with SPI. Using pin 10 for CS.
The individual modules have been tested individually. They are work as intended.
The problem begins when the SPI begin is invoked. At that time the I2C bus appears to no longer have reliable communications. I would say none, but a follow-on test while writing this shows some attempt at activity.
[ A side note - for whatever reason the I2C devices must be connected as shown. I originally had everything off J1-A4/A5. The OLED will not initialize. Ran through a selection of pull-up resistors to no effect. Also attempted to chain through the RTC, same results. Per the schematic The pins J1-A4/A5 and J2-17/18 are the same. So that will remain a mystery unless anyone knows what is up with that. I may chase it around later, this config works. ]
My theory is a conflict between the "Wire.h" and either "SPI.h" and/or "SD.h". I have not delved into the libraries yet.
I cannot be the only one with this problem, I just haven't found the answer yet.
Any advice would be appreciated.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS3231 rtc;
String time;
String hour;
String min;
String sec;
File logHandle;
String logName;
void setup() {
Serial.begin(9600);
// start-setup OLED
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
// start-setup RTC
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (true); }
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
time.reserve(10);
hour.reserve(2);
min.reserve(2);
sec.reserve(2);
// start SD
Serial.print("Initializing SD card...");
if (!SD.begin(10)) { // <-- problem begins
Serial.println("initialization failed!"); }
else {
Serial.println("initialization done."); }
// format-create log file name
DateTime now = rtc.now();
logName = "";
logName += now.year();
logName = logName.substring(logName.length()-2,logName.length());
logName += now.month();
logName += now.day();
logName = "L" + logName;
} // end setup
void loop() {
DateTime now = rtc.now();
// makes a clock - extra code for making the time look pretty
time = "";
hour = "";
min = "";
sec = "";
hour = now.hour();
time += hour;
min = now.minute();
if ( min.length() == 1 ) { time += ":0"; } else { time += ":"; }
time += min;
sec = now.second();
if ( sec.length() == 1 ) { time += ":0"; } else { time += ":"; }
time += sec;
oledDisplayCenter(time);
Serial.println(time); // debug echo to serial monitor
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
oled.clearDisplay(); // clear display
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text); // text to display
oled.display();
}
