Arduino Nano with OLED SSD1306

Hi guys,

My Hardware:
Arduino Nano with ATmega328
OLED Display 128x32 with SSD1306

I used the code from here Arduino Nano with SSD1306 I2C OLED Analog Display with. The code works well. What I don't like is, that the code uses 49% of the arduino storage. Is there a better code for OLED with SSD1306 which does not use so much storage.

I don't need to display images on the OLED. I just want to write some letters and numbers on the OLED.

Here is the code:

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

#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);

void setup()
{
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) 
  {
    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(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  testdrawstyles();    // Draw 'stylized' characters
}

void loop() 
{
}

void testdrawstyles(void) 
{
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  display.println(F("Hello, world!"));

  display.setTextColor(SSD1306_WHITE);
  //(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println(3.141592, 6); //6-digit after decimal point

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(F("0x")); display.println(0xDEADBEEF, HEX);

  display.display();
  delay(2000);
}

For text-only displays, you need only this library: GitHub - greiman/SSD1306Ascii: Text only Arduino Library for SSD1306 OLED displays

2 Likes

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