0
votes

I am trying to write some data to SD card and read it back to serial monitor as well as display it to the OLED.

Both the SD card and OLED work separately but they seem to be interfering with each other when combined. I have used Arduino SD and Adafruit OLED libraries.

Connections from Arduino Uno to Micro SD card module:

5V to SD VCC
GND TO SD GND
PIN 10 TO SD Chip Select
PIN 11 TO SD MOSI
PIN 12 TO SD MISO
PIN 13 TO SD SCK

Connections to OLED:

3.3V to OLED VCC
GND TO OLED GND
A4 TO OLED SDA
A5 TO OLED SCK

Here is the code:

#include <SPI.h>
#include <SD.h>

File myFile;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while(!Serial) {
    ;
  }

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
  }     
  display.clearDisplay();           
  display.setTextSize(1);             
  display.setTextColor(WHITE);       
  display.setCursor(29,29);
  display.print("INITIALISING");
  display.display();
  delay(5000);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  display.clearDisplay();

  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
      display.setCursor(0,0);
      display.print(myFile.read());
      display.display();
      delay(5000);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Code gets stuck at OLED initialization as mentiontioned above. If I replace these lines:

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // CODE GETS STUCK HERE. DISPLAY NEVER INITIALISES
    } 

To this:

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 

I have run I2C scanner code on OLED so the address "0x3C" is correct.

The OLED still doesn't work and SD card initialises but Arduino is writing wrong data to TXT file on SD card like this:

teóting 1,à2, ó® 

Instead of:

testing 1, 2, 3.

I have also tried using U8G2 library's sketches with SD card in case Arduino was running out of RAM but it still doesn't work. I have also changed SD chip select to Arduino digital pin 4 but still same results.

On browsing and experimenting more,I found MISO OR MOSI PIN of SD maybe interfering with SDA/SCL pins of OLED. Maybe wiring needs to change.

ANY SUGGESTIONS?

1
Arduino Uno with 2kB of RAM might have problems. Check the RAM requirements of both partial sketches and of the combined one. - datafiddler
This is the message that pops up on compilation - "Sketch uses 22152 bytes (72%) of program storage space. Maximum is 30720 bytes. Global variables use 1303 bytes (63%) of dynamic memory, leaving 745 bytes for local variables. Maximum is 2048 bytes." I think RAM is not an issue. Anyway thanks for looking into it. I have tried even a small program that had less than 45% program storage space . Still the same problem . - Rohan
The OLED library dynamically allocates 1024 bytes of memory. 745 won't be enough. - gre_gor
The above message was for the combined code. This one is for OLED code -"Sketch uses 13418 bytes (41%) of program storage space. Maximum is 32256 bytes. Global variables use 527 bytes (25%) of dynamic memory, leaving 1521 bytes for local variables. Maximum is 2048 bytes". And this is for SD code alone - "Sketch uses 10818 bytes (33%) of program storage space. Maximum is 32256 bytes. Global variables use 945 bytes (46%) of dynamic memory, leaving 1103 bytes for local variables. Maximum is 2048 bytes." - Rohan
You were right . I just switched to UG82 libarary and used its u8x8 examples which does not use any RAM. Its working. Thanks to both of you! - Rohan

1 Answers

0
votes

(I had the same issue) Just posting the suggestions of the comments as an answer, all credit to @gre_gor and @datafiddler for discovering this:

Both libraries together run out of RAM (main memory).

From my testing, the SD library might continue to work if it is initialized first in setup(). The solution is to use the U8G2 OLED driver, which is much more memory economic. The driver is also in the official Arduino library directive, so you can install and use it directly from the IDE.