1

I am searched over the internet and found out a few similar questions, but non of them were helpfull, so I decided to open a new quetion.

I tried both SD and Adafruit_SSD1306 libraries and they both worked fine when they were separated - in different codes. The error came when I used them in one code.

In my programme I try to init SD card, write to it and then init dispaly (everything works fine). After that I do another write cycle into the SD, but I get an error in opening a file:

error opening test.txt

In other words I cant write anything into the SD card after oled display is initialized (display works then without any problems).

Here is my code:

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

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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32 

TwoWire Wire1(PB9, PB8);// Use STM32 I2C2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, -1);

const int chipSelect = PA15;
    
    
void setup() {
    
    Serial1.begin(9600); 
   if (!SD.begin(chipSelect)) {
    Serial1.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial1.println("card initialized.");

 for (int i = 0; i < 10; i++)
  {
  File dataFile = SD.open("test.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println("Test");
    dataFile.close();

    Serial1.println("Test");
  }
  else {
    Serial1.println("error opening test.txt");
  }
delay(30);
  }

  delay(2000);

 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial1.println(F("SSD1306 allocation failed"));
  }    

  delay(500);

  for (int i = 0; i < 10; i++)
  {
  File dataFile = SD.open("dat_test.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println("Test");
    dataFile.close();

    Serial1.println("Test");
  }
  else {
    Serial1.println("error opening test.txt");  //here is the error I get
  }
delay(30);
  }
}

void loop() {
}

Here is the output:

card initialized.
Test
Test
Test
Test
Test
Test
Test
Test
Test
Test
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt

I am using STM32F103CB.

5
  • and how do you connect the SD card? what dev board do you use? a Blue Pill?
    – Juraj
    Jul 28, 2020 at 18:57
  • Have you tried commenting out the display call between the two file-write sequences, and will the second write-sequence run then?
    – JRobert
    Jul 28, 2020 at 19:09
  • @JRobert, yes I tried that and both SD writings worked fine. Its very wierd, because it only happend after the OLED init.
    – LucasN
    Jul 29, 2020 at 6:27
  • @Juraj SD card is connected via SPI (PB3 - SCK, PB4 - MISO, PB5 - MOSI, PA15 - CS), OLED display uses I2C (PB8 - SCL, PB9 - SDA) and I am using my own custom board.
    – LucasN
    Jul 29, 2020 at 6:32
  • the experts are here stm32duino.com
    – Juraj
    Jul 29, 2020 at 7:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.