Unable to put text on an OLED display, but am able to render anything else

I followed this page to try to render text, but the display keeps showing black and it is connected properly and is able to render other things such as triangles and rectangles. This is the code I am using to render the text:

void loop() {
  display.clearDisplay();
  display.print(timermin);
  display.print(F(":"));
  display.print(timersec);
  display.display();
  timersec++;
  if(timersec > 60){
    timersec = 0;
    timermin++;
  }
  delay(1000);
}

What am I missing? This should work but the print and println functions just don't work for me.

Post all of your code.

#include <SPI.h>
#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 SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

int timersec = 0;
int timermin = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(700);

  // Clear the buffer
  display.clearDisplay(); 
}

void loop() {
  display.clearDisplay();
  display.print(timermin);
  display.print(F(":"));
  display.print(timersec);
  display.display();
  timersec++;
  if(timersec > 60){
    timersec = 0;
    timermin++;
  }
  delay(1000);
}

Does the Adafruit splash screen show for 0.7 seconds?

Yes, it does.

I believe you need to set the cursor position before displaying text.

Begin with setCursor(x, y), which will place the top left corner of the text wherever you please. Initially this is set to (0,0) (the top-left corner of the screen). Then set the text color with setTextColor(color) — by default this is white. Text is normally drawn “clear” — the open parts of each character show the original background contents, but if you want the text to block out what’s underneath, a background color can be specified as an optional second parameter to setTextColor(). Finally, setTextSize(size) will multiply the scale of the text by a given integer factor. Below you can see scales of 1 (the default), 2 and 3. It appears blocky at larger sizes because we only ship the library with a single simple font, to save space.

1 Like

I tried setting the cursor position but that still doesn't work.

There were a few other steps detailed on that page and in the text I quoted.
Try all of them and post your updated code.

1 Like

That worked! I can display text now. Thanks!

Post your updated code to help others in the future. Then select an answer and mark it as the solution.

1 Like

This is the solved code:

#include <SPI.h>
#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 SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

int timersec = 0;
int timermin = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }


  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(700);

  // Clear the buffer
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextColor(WHITE);
  display.setTextSize(3);
}

void loop() {
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print(timermin);
  display.print(F(":"));
  display.print(timersec);
  display.display();
  timersec++;
  if(timersec >= 60){
    timersec = 0;
    timermin++;
  }
  delay(1000);
}
1 Like

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