need help: oled cannot work with sd card module on Sparkfun samd21 mini breakout

Hi friends,

I tried many times and read many posts related to OLED and sd card modules. Some posts said it was due to the SRAM issue.

But the SAMD21 mini breakout has enough SRAM.

Screen(https://www.amazon.com/UCTRONICS-SSD130 … 451&sr=8-8):

GND -->GND

VCC → VCC

SCL–> SCL

SDA–> SDA

SD Card Reader(https://www.amazon.com/HiLetgo-Adater-I … C94&sr=1-3):

GND → GND

VCC->VCC

MISO → 12 (MISO)

SCK → 13 (SCK)

MOSI → 11 (MOSI)

CS → 10

Below is the code I am trying to use:

#include <SPI.h>

#include <SD.h>

#include <DHT.h>

#include <Wire.h>

#include <DS3231.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

//defining dht22

#define DHT22Pin 8

#define DHTType DHT22

DHT HT(DHT22Pin,DHTType);

//creating file object

File myFile;

float tempC;

String temp;

int pause=1000;

//defining ds3231 RTC

DS3231 clock;

RTCDateTime dt;

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET -1// Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {

Serial.begin(115200);

//SD

//for DHT22

HT.begin();

//for RTC

clock.begin();

// Set sketch compiling time

clock.setDateTime(DATE, TIME);

// for sd

Serial.print(“Initializing SD card…”);

// SD.begin(10);

if (!SD.begin(10)) {

Serial.println(“initialization failed!”);

while (1);

}

Serial.println(“initialization done.”);

myFile = SD.open(“data.txt”, FILE_WRITE);

if (myFile) {

myFile.println( “Temperature(°C) \r\n”);

myFile.close();

}

else {

Serial.println(“error opening data.txt”);

}

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64

Serial.println(F(“SSD1306 allocation failed”));

for(;;); // Don’t proceed, loop forever

}

display.display(); //display initial Adafruit logo

delay(2000);

// Clear the buffer

display.clearDisplay();

display.display();

}

String DayOfTheWeek(uint8_t Day){

String DayText;

if (Day==1) DayText=“Monday”;

if (Day==2) DayText=“Tuesday”;

if (Day==3) DayText=“Wednesday”;

if (Day==4) DayText=“Thursday”;

if (Day==5) DayText=“Friday”;

if (Day==6) DayText=“Saturday”;

if (Day==7) DayText=“Sunday”;

return DayText;

}

String DayMonthYear(uint8_t Day,uint8_t Month,uint16_t Year){

String DayMonthYearText;

if (Month==1) DayMonthYearText="JAN ";

if (Month==2) DayMonthYearText="FEB ";

if (Month==3) DayMonthYearText="MAR ";

if (Month==4) DayMonthYearText="APR ";

if (Month==5) DayMonthYearText="MAY ";

if (Month==6) DayMonthYearText="JUN ";

if (Month==7) DayMonthYearText="JUL ";

if (Month==8) DayMonthYearText="AUG ";

if (Month==9) DayMonthYearText="SEP ";

if (Month==10) DayMonthYearText="OCT ";

if (Month==11) DayMonthYearText="NOV ";

if (Month==12) DayMonthYearText="DEC ";

DayMonthYearText=DayMonthYearText+Day;

if (Day==1)DayMonthYearText=DayMonthYearText+"st ";

if (Day==2)DayMonthYearText=DayMonthYearText+"nd ";

if (Day>2)DayMonthYearText=DayMonthYearText+"th ";

DayMonthYearText=DayMonthYearText+Year;

return DayMonthYearText;

}

String AddLeadingZero(uint8_t x){

String AddLeadingZeroText;

if(x<10) AddLeadingZeroText=“0”;

else AddLeadingZeroText=“”;

AddLeadingZeroText=AddLeadingZeroText+x;

return AddLeadingZeroText;

}

String CurrentTime(uint8_t H, uint8_t I ){

String CurrentTimeText=“”;

CurrentTimeText=CurrentTimeText + AddLeadingZero(H) +“:”+AddLeadingZero(I);

return CurrentTimeText;

}

void data_logging()

{

temp = String(HT.readTemperature( ),2);

Serial.print("Save data: ");

Serial.println(temp);

myFile = SD.open(“data.txt”, FILE_WRITE);

if (myFile) {

Serial.print(“Writing to data.txt…”);

myFile.println(temp);

myFile.close();

Serial.println(“done.”);

} else {

Serial.println(“error opening data.txt”);

}

Serial.println();

// delay(2000);

}

void loop() {

//for rtc

dt = clock.getDateTime();

display.fillRect(0,0,128,16,SSD1306_WHITE);

display.fillRect(0,17,128,16,SSD1306_BLACK);

display.fillRect(0,31,128,33,SSD1306_WHITE);

display.setCursor(1,1);

display.setTextSize(2);

display.setTextColor(SSD1306_BLACK);

display.println(DayOfTheWeek(dt.dayOfWeek));

display.setCursor(1,18);

display.setTextSize(1);

display.setTextColor(SSD1306_WHITE);

display.println(DayMonthYear(dt.day,dt.month,dt.year));

display.setCursor(3,35);

display.setTextSize(3);

display.setTextColor(SSD1306_BLACK);

display.println(CurrentTime(dt.hour,dt.minute));

display.setCursor(100,35);

display.setTextSize(2);

display.setTextColor(SSD1306_BLACK);

display.println(AddLeadingZero(dt.second));

//clock.forceConversion();

display.setCursor(85,18);

display.setTextSize(1);

//for dht temp

tempC=HT.readTemperature();

display.setTextColor(SSD1306_WHITE);

display.print(tempC);

display.setCursor(117,16);

display.print(“o”);

//SAVE TO SD

if(isnan(HT.readTemperature())){

Serial.println(“dht22 sensor not working”);

}

else{

data_logging();

delay(1000);

}

display.display();

delay(1000);

}

When I run the above code, OLED is black and nothing writes to the sd card.

If I remove SD related part from the code, it works, the OLED display time.

I need the OLED display time and SD card can save data.

Please help.

Thanks in advance.

I believe you need to define 2 different serial buses; try declaring 2 separate ones (serial1 and serial2 , perhaps)

This https://docs.arduino.cc/built-in-exampl … SerialMega has something similar (though it uses ‘serial’ and ‘serial1’)

Thanks very much. Let me try it.

Looks like you’re using a 5 volt SD board with a 3.3 volt Arduino. Try a 3.3 volt SD board like the one below.

https://www.sparkfun.com/products/544

YellowDog:
Looks like you’re using a 5 volt SD board with a 3.3 volt Arduino. Try a 3.3 volt SD board like the one below.

https://www.sparkfun.com/products/544

Thanks for your reply. But it works fine with uno r3 on 3.3v. But on mini breakout it doesn’t work.

The Uno R3 is not a 3.3 volt board.