Make Adafruit_SH1106.h to use les RAM


I have tried to solve my problem with help from difrent forums and in many ways. But now i am oute of options and need som help from you.

I using a OLED 1,3" SH1106. It works fine but the library <Adafruit_SH1106.h> is so damn large. Im only displaying a temperatur from a ds18b20 and it uses 56% storage space and 79% of dynamic memory.

One option is ofcourse buying a MEGA but i need to fit the build in a smal space so UNO or NANO is needed.

I have understand thats its posible to remove some of the library if Im only going to display text
Is it anyone out there that can help me how that is made?

Or ofcourse any other way of using that OLED whil useing less memmory (othe library etc)?

My code i run now is this:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
#define ONE_WIRE_BUS 4

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

int pushButton = 2;

void setup(void)
  // Start serial communication for debugging purposes
  Serial.begin(9600);     // initialize the Serial Monitor at a baud rate of 9600
  // Start up the library
  sensors.begin();        // initialize the DS18B20 temperature sensor:

  display.begin(SH1106_SWITCHCAPVCC, 0x3C);


void loop(void) {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  Serial.println(sensors.getTempCByIndex(0));     // get and print the temperature in degree Celsius
  float T = sensors.getTempCByIndex(0);  // let T be temperature in degC from sensor
// floating-point number, with a decimal point

  String sT = String(T, 1);



  display.setCursor(0, 0);
  display.println("Vatten: ");   // display temperature in deg Celsius
  display.setCursor(0, 20);
  display.display();    // for the changes to make effect

} (15.1 KB)

Have you googled

SH1106 arduino library

To find other libraries for your OLED display?

Tom.. :slight_smile:

Im new to this and only used the #include <Adafruit_SSD1306.h> fore my other project. there fore i goes this way.

So if you have another good sugestion fore driving the OLED i wold be happy!

Dump the Adafruit_SH1106 library perhaps ?

If you just want to display text, here is an example using the GitHub - olikraus/u8g2: U8glib library for monochrome displays, version 2. The exampe prints some text to the display, memory use on a Arduino Pro Mini is;

Sketch uses 7538 bytes (23%) of program storage space. Maximum is 32256 bytes.
Global variables use 579 bytes (28%) of dynamic memory, leaving 1469 bytes for local variables. Maximum is 2048 bytes.

  Programs for Arduino - Copyright of the author Stuart Robinson - 20/12/19

  This program is supplied as is, it is up to the user of the program to decide if the program is
  suitable for the intended purpose and free from errors.

  Program Operation - This program is a simple test program for the SSD1306 and SH1106 OLEDs. The program
  prints a short message on each line, pauses, clears the screen, and starts again.

  OLED address is defined as 0x3C.

  Screen write on 8Mhz Atmel 170mS.

  Serial monitor baud rate is set at 9600.

#include <U8x8lib.h>                                        //get library here > 
//U8X8_SSD1306_128X64_NONAME_HW_I2C disp(U8X8_PIN_NONE);      //use this line for standard 0.96" SSD1306
U8X8_SH1106_128X64_NONAME_HW_I2C disp(U8X8_PIN_NONE);     //use this line for 1.3" OLED often sold as 1.3" SSD1306

#define OLED_Address 0x3C

uint16_t writecount;
uint32_t startwritemS, endwritemS, timemS;

void loop()
  Serial.print(F(" Writing to display"));

  disp.setI2CAddress(OLED_Address << 1);                     //I2C address multiplied by 2, the lowest bit must be zero

  startwritemS = millis();
  endwritemS = millis();
  timemS = endwritemS - startwritemS;
  disp.setCursor(8, 4);

  Serial.print(F(" - done"));

  Serial.println(F("PowerSave display"));
  disp.setPowerSave(1);                                      //power save display, turns off
  disp.setPowerSave(0);                                      //display back to normal

void screen1()
  disp.setCursor(0, 0);
  disp.print(F("Hello World !"));
  disp.setCursor(0, 1);
  disp.print(F("Line 1"));
  disp.setCursor(0, 2);
  disp.print(F("Line 2"));
  disp.setCursor(0, 3);
  disp.print(F("Line 3"));
  disp.setCursor(0, 4);
  disp.print(F("Line 4"));
  disp.setCursor(0, 5);
  disp.print(F("Line 5"));
  disp.setCursor(0, 6);
  disp.print(F("Line 6"));
  disp.setCursor(0, 7);
  disp.print(F("0123456789012345"));                         //display is 8 lines x 16 charaters when using the
  //u8x8_font_chroma48medium8_r font

void setup()
  Serial.println(F("31_SSD1306_OLED_Checker starting"));

Tnx alot, It works just fine.

I guess I was stuck in using the SH1106 library becuse the OLED was named that on the store when i bought it.

Now i leard that its not hardly linked to its name :-[

As always you guys solved my problem. Sorry fore a nobb question. Have a nice day. ;D