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));
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.