i am working on a project in which data from sd card is displayed on the oled screen both using SPI communication, but it is not displaying anything.

i commented out the sd part and then oled works fine.

i got to know that problem orgnates from "SD.begin(4)".

please help me..

#include <Wire.h>
#include "U8glib.h"
#include <SD.h>
static U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);
String text;

File myFile;
void setup() {
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  Serial.print("Initializing SD card...");

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("a.txt", FILE_WRITE);

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

  // re-open the file for reading:
  myFile = SD.open("a.txt", FILE_READ);
  if (myFile) {

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      text = myFile.readString();

    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");


void loop() {
  do {
  } while( u8g.nextPage() );


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here 
} ```

  • 2
    many SD card 5 V adapter modules don't allow other devices to use the SPI bus – Juraj May 4 '20 at 17:13
  • 2
    The 13 and 11 as parameters in the constructor for the U8GLIB object makes it appear as if you are using software SPI. Usually if using hardware SPI you only need to pass in the CS pin, but it already knows where SCK and MOSI are. If you're trying to use software SPI and hardware SPI (the SD card) on the same pins I think that's not going to work. – Delta_G May 4 '20 at 21:48
  • thanks everyone :) – Dev Pratap May 5 '20 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.