I2C TFT with serial microSD problems

I am trying to use an I2C TFT display with a serial connected microSD. I can get either to work separately, but when they are both active in the program the TFT SSD1306 allocation fails. I have uploaded my stripped-down test program that has the failure. I see postings that indicate that it is possible to use I2C and serial devices together, but i am clearly doing something wrong. i have added 2.7k resistors to the SDA and SCL connections on the TFT SSD1306, but the TFT still fails to allocate. I would appreciate any thoughts on what to try. I am running the system with an old bootleg Nano.
thanks
chuck
[MicroSD_TFT__Test__Program_3.ino|attachment] (upload://xTdIOQo78rjOIo07mgc1eggeTHs.ino) (4.2 KB)

Check the notes on how to use the forum about properly posting code using code tags.

Nick: Thanks for your quick reply. I am new to this and have now read the guidelines. I am using a nano ATmega328p old bootlegger with an SSD1306 OLED display and a serial microSD card.


I have tried to copy the program using the </> button, but I don't see anything in my screen. The microSD part of the program works if I // blank out the for(;;); line so that the allocation failure does not hang up the program. I have used the SSD1306 OLED in other applications and it works without any issues. What is it that is hanging things up when I mix a serial device and an I2C device in the same program? Thanks again for any guidance.
chuck

How to post code:


I have repeated your instructions many times and the code will not be copied into the Forum. I can copy the code to notepad or word without any trouble using the copy to forum control and I get a message in the Arduino program that says "code formatted for the Arduino forum has been copied to the clipboard", but it will not appear in the forum app. Is there a problem with the forum app? Is right clicking on the mouse and clicking on the paste command not the correct way to paste to the Forum App?


This could be because you are using a phone or tablet, and for reasons that are incomprehensible, the code insertion facility is not apparent, even though the emoji insertion is. The prompt you want is </> as above, shown on a PC, and you then get the paste point shown below.

`type or paste code here`

I don't think you can post code without that prompt, i.e. just using the keys,and therefore cannot do it from a phone. Hopefully somebody knows better.

Getting back to the main point, I utterly fail to see why the two devices cannot co-exist, particularly since you can use them individually, and therefore do know what pins to use. There is a faint possibility that your code is actually kosher, and you have a power problem. I assume the OLED runs on practically nothing, but the same probably cannot be said for the card.

Those are instructions for posting code from the IDE installed on a computer. I do not know any thing about other types of IDE like on line ones. I assume the installed IDE unless informed differently.

Nick: I get the prompt after pressing <> and I am using a laptop running windows 10. The program is being copied since I can paste it into word or notepad. It just
Won’t paste into the forum app for some reason.
Thanks,
Chuck

I have had a breakthrough. It looks like there is a limit to the length of code that can be copied. If i break my code into two sections, I can insert it into the forum using the </> button. So here it is:

[code]
#include <Wire.h>
#include <Adafruit_GFX.h>          // Core graphics library
#include <Adafruit_SSD1306.h>      // Hardware-specific library for ST7735
#include <SD.h>                    // SD card & FAT filesystem library

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino NANO:      A4(SDA), A5(SCL)

#define SCREEN_WIDTH 128            // OLED display width, in pixels
#define SCREEN_HEIGHT 32            // OLED display height, in pixels
#define OLED_RESET     -1           // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C         // See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, OLED_RESET);

int chipSelect = 4;                   // CS Digial 4 pin for writing to MicroCD
File myFile;
int fileNo = 1;
String spec = "spec_";

void setup(void) {
  Serial.begin(9600);
  Serial.println(F("Hello! SSD1306 TFT & microSD Test..."));
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {       // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    Serial.println(F("SSD1306 allocation failed"));
    //    for(;;); // Don't proceed, loop forever
  }

  // Setup MicroSD

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

  //Search microSD for duplicate files and provide new name for next file.
  spec.concat(fileNo);
  Serial.println(spec);
  spec.concat(".txt");
  Serial.println(spec);
  while (SD.exists(spec)) {
    Serial.print(spec);
    Serial.println(" exists.");
    spec = "spec_";
    fileNo = fileNo + 1;
    spec.concat(fileNo);
    spec.concat(".txt");
  }

[/code]
[code]
  Serial.print(spec);
  Serial.println(" doesn't exist.");

  // open a new file
  Serial.print("Creating ");
  Serial.print(spec);
  Serial.println("...");
  myFile = SD.open(spec, FILE_WRITE);

  // Check to see if the file exists:
  if (SD.exists(spec)) {
    Serial.print(spec);
    Serial.println(" exists.");
  } else {
    Serial.print(spec);
    Serial.println(" doesn't exist.");
  }

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to ");
    Serial.print(spec);
    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.print("error opening ");
    Serial.println(spec);
  }

  // re-open the file for reading:
  myFile = SD.open(spec);
  if (myFile) {
    Serial.println(spec);

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
      delay(500);
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.print("error opening ");
    Serial.println(spec);
  }
  Serial.println("Looks good");
  delay(500);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(1, 1);
  display.println("  ");
  display.println(" IntTime(sec) ");
  display.print("     ");
  display.display();
  delay(3000);
}
void loop() {
  display.println(" display is working");
  delay(1000);
  display.clearDisplay();
  delay(500);
}
[/code]

Nick: Thanks for the suggestion about current draw. I have measured the current with the OLED and the microSD. The current going into the OLED is around 6 mA and the current going into the microSD is 17 mA, so when they are both on the total current from the 5v nano output is 23 mA. The literature seems to indicate that these current levels should not be a problem. When I // out the OLED section the microSD works and when I // the microSD section of the program the OLED works, so I remain at a loss to understand the problem.
thanks,
chuck

I think I have found the problem. The SSD1306 library apparently needs 1100 bytes free to load. My program uses 1314 bytes or 64% of the dynamic memory, so my SSD1306 display is not working because I don't have enough memory. Is there a stripped down SSD1306 library that I could use? I only need monochrome display with only letters and numbers. Is there another display that I could use that requires much less dynamic memory?

Try:

https://duckduckgo.com/?q=%22ssd1306%22+%2B+%22Atmega328%22+%2B+%22library%22&ia=web

Maybe:
GitHub - newgnu/ssd1306_mini_lib: mini-fied arduino library to talk to an ssd1306 with the lowest overhead. Has only been tested on attiny85 and atmega328

Markd833: Thanks very much for the library. With the reduced memory drain, I can now get both the microSD and the TFT display to work at the same time. Here is my current test code.
Preformatted text
I don't seem to understand how to program in the new SSD1306Ascii library. In the code I have a oled.print("bck") command, but the display only shows the letter k. The bc are not shown. I am using a TFT, not an OLED. Is this the problem. Is there a description of the commands somewher that i could read?
thanks
chuck

If you try a different string, does it also miss the first 2 characters? I seem to recall there might be a setting for the display size. You might want to check that against your display.

Yes. It doesn't matter how long the string is, it always just displays the last letter in the string. Is there someplace that list the commands?

The header file? What does the author say on github?

Thanks for your help. The problem was I was using a TFT rather than an OLED. When I switched to an OLED, all works. Thanks much for your help. All is working with the microSD, display and nano.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.