Bar meter on a 1.3" OLED (SH1106)
Show a knob's value as a number and a bar on a 1.3" SH1106 OLED, with the library made for its controller.
Displays · Beginner
In a real product: Handheld test meter (Industrial): An OLED shows values and bars.
The 1.3" OLED looks like the common 0.96" SSD1306 display, only bigger, but its SH1106 controller is different: 132 columns of memory for a 128-pixel panel, and no way to send the whole picture in one go. With the SSD1306 library the picture comes out shifted; with its own library it is right.
What you'll learn
- Drawing into a frame buffer in RAM and sending it with
display() - Text and a bar graph with Adafruit GFX
- Why a display's controller, not its size, decides which library to use
How it works
display.begin(0x3C, true) initialises the SH1106. Drawing calls change a 1 KB copy of the screen in the Uno's RAM; display.display() sends it page by page (8 rows at a time), each starting at column 2 where the visible panel begins. The loop clears the buffer, prints the knob as a percentage in large text, draws a bar as wide as the value, and sends it, ten times a second.
Try this
- Turn the knob: the number and the bar follow.
- Swap in the SSD1306 library and its
Adafruit_SSD1306class: on a real SH1106 the picture is shifted two pixels. - Draw a small graph of the last 128 readings instead of the bar.
sketch.ino
// A 1.3" SH1106 OLED (I2C at 0x3C) shows the knob on A0 as a percentage and a bar, ten times a second.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
Adafruit_SH1106G display(128, 64, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!display.begin(0x3C, true)) {
Serial.println("No SH1106 at 0x3C");
while (true) {}
}
display.setTextColor(SH110X_WHITE);
}
void loop() {
int percent = analogRead(A0) * 100L / 1023;
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("KNOB");
display.setTextSize(3);
display.setCursor(0, 16);
display.print(percent);
display.print('%');
display.drawRect(0, 50, 128, 12, SH110X_WHITE);
display.fillRect(2, 52, percent * 124 / 100, 8, SH110X_WHITE);
display.display();
delay(100);
}