Conrol OLED display with driver SH1106 withpout online library

Dear all,

I hope this community can help me out. Recently I bought an OLED display for my Arduino Uno.

Link to amazon product.

I got the datasheet from the distributor along with a short start-up manual. The distributor suggests operating the display with the u8g2 library by Oli Kraus.

GitHub link to library distribution.

I managed to install the library and get the display running. I can modify the example sketches provided with the library and draw custon characters or figures with the display.

However, I want to write my own library for the fun of it. This is where my troubles start.

The display is drived by a SH1106 IC. I dowloaded the SH1106 datasheet and checked the IIC communication section as well as the command section.

Datasheet for SH1106

What I want to do for now is:

  1. Turn the display ON.
  2. Set the entire display ON.

This is my code. I’m using the arduino Wire.h library for the IIC interface.

#include <Wire.h>

#define ledPin 13
#define adress 0x3c

#define displayON 0xAF
#define allPixelsOn 0xA5

bool ledOut=false;
byte resp;

void setup() {
  // put your setup code here, to run once:
  Wire.begin();

  Serial.begin(9600);

  delay(100);
}

void loop() {
  
  Wire.beginTransmission(adress);
  Wire.write(0x80);  
  Wire.write(displayON);  
  resp=Wire.endTransmission();

  Serial.println(resp);
  
  Wire.beginTransmission(adress);
  Wire.write(0x80);
  Wire.write(allPixelsOn);  
  resp=Wire.endTransmission();

  Serial.println(resp);

  delay(1000);
}

When I look at the u8g2 library the commands are transmitted in the same way. The commands are the same as in the u8g2 library. More importantly they are as described in the datasheet.

The wiring is correct scince there is no problem operating the display with the u8g2 library.

Yesteday I ran out of ideas for coding and google searches. I hope someone here has more experience with IIC and the SH1106 and can point me to my error.

Best regards,
mouthbag

sketch_jul06b.ino (1.16 KB)

How many libraries have you written before? How complex were they and what did they do?

I haven't written any libraries for Arduino so far. However, I have some experience with software and hardware interfaces.

I am currently working on my PhD thesis on image recognition and automation with CNN's. The OLED display is just for fun at home.

I suppose that I have missed something in the SH1106 datasheet on the topic of initialization or maybe there is some issue with initializing the Arduino wire library?

Best regards,
mouthbag

Okay, I took a closer look at the u8g2 library. The command for the SH1106 have a zero prepanded.

According to the data sheet the command for "Display On/Off" ist 0xAF/0xAE respectively. However, when I use 0x0AF and 0x0AE I can turn the display ON/OFF.

I am still confused where the prepanded zero comes from. Maybe someone has an idea?

Best regards,
mouthbag

mouthbag:
the command for "Display On/Off" ist 0xAF/0xAE respectively. However, when I use 0x0AF and 0x0AE I can turn the display ON/OFF.

No, I believe there must be some other reason. Prepending a zero makes no difference, except perhaps that the compiler will assume the value is 16-bit rather than 8 bit. But as Wire.write() expects an 8-bit value anyway, and if given a 16-bit value will just ignore the upper 8 bits, I can't see a way that prepending the zeroes can have an effect. Maybe if you posted your full sketch, we could spot a reason. Use code tags please! If you're sketch is more than 9000 characters, make a cut-down, minimal version to illustrate the problem and post that.