Attempting to change references of SSD1306 to SH1106

Hello Everyone,

I am fairly new to Arduino and have been attempting my first project using a sensor and display.

I have a DHT22 sensor that I am trying to use with a SH1106 display. The problem is, the example I found online is written for a SSD1306 dsiplay.

My difficulty is changing the code to allow it to work with a SH1106 rather than the SSD1306.

The code provided is:

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

#define DHTTYPE DHT22
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
unsigned long delayTime;
uint8_t DHTPin = 2;
DHT dht(DHTPin, DHTTYPE);
float Temperature;
float Humidity;
float Temp_Fahrenheit;

  void setup() {
    Serial.begin(115200);
    pinMode(DHTPin, INPUT);
    dht.begin();
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.display();
    delay(100);
    display.clearDisplay();
    display.display();
    display.setTextSize(1.75);
    display.setTextColor(WHITE);
  }


void loop(){
  Humidity = dht.readHumidity();

  // Read temperature as Celsius (the default)
  Temperature = dht.readTemperature();

  // Read temperature as Fahrenheit (isFahrenheit = true)
  Temp_Fahrenheit = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(Humidity) || isnan(Temperature) || isnan(Temp_Fahrenheit)) {

    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  Serial.print(F("Humidity: "));
  Serial.print(Humidity);
  Serial.print(F("%  Temperature: "));
  Serial.print(Temperature);
  Serial.print(F("°C "));
  Serial.print(Temp_Fahrenheit);
  Serial.println(F("°F "));
  display.setCursor(0, 0);
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0, 10);
  display.print(Temperature);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(Humidity);
  display.print(" %");
  display.display();
  delay(1000);
}

Some of the steps I have taken so far are:
Change: #include <Adafruit_SSD1306.h> to #include <Adafruit_SH110X.h>

Change: Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1); to
Adafruit_SH110X display = Adafruit_SH110X(128, 64, &Wire, -1);

When I tried to compile the sketch after making those changes, the error message I receive is:
Compilation error: 'SH110X' was not declared in this scope

I'm really confused about what I really need to change in order to get the sketch to compile. Or if it's just not possible to use a SH1106 display in this particular situation and I'll need to order a SSD1306 and try again?

If any of you could give me any ideas of what I can do going forward, I'd appreciate it so much.

Cheers,

Amy

You're almost there.

Change

#include <Adafruit_SSD1306.h>

to

#include <Adafruit_SH110X.h>

Change

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);

to

Adafruit_SH1106G display = Adafruit_SH1106G(128, 64, &Wire, -1);

Change

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

to

display.begin(0x3C, true);

Change

display.setTextColor(WHITE);

to

display.setTextColor(SH110X_WHITE);

That will get the code to compile; whether it runs or not is another story. :slight_smile:

Thank you! That worked perfectly. Don't really understand all of the changes, but at least I know it works.

As an exampe, you mentioned to change:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
to
display.begin(0x3C, true);

Why doesn't the type of display need to be referenced such as is being done in the original code?

Thank you so much again!

Cheers,

Amy

The begin() methods for the 2 display types differ in what they expect for parameters. I just looked at one of the SH1106G examples to see what it expected.

SSD1306_SWITCHCAPVCC isn't referencing the type of display, but rather how an SSD1306 should generate its display voltage. Either there is no such option for the SH1106G or it isn't available as a user choice.

Glad to hear it worked. Go forth and experiment!

Hi all,
I'm French please excuse my poor english.
The solution seems not work for me.
I got a DHT and a SH1106 one a Arduino Uno.
For each code I try, I can't have both serial datas and display data.
I got no error in the compilation, but the program seems to stop at the moment of displaying in the screen...
I try a lot of topics, examples... but no one work.
If I try an example to test the screen, all is good.
If I try an example to test the DHT, it's also good.
Thanks for your help,

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