1.3 Inch OLED Display Code: Complete Guide for Arduino & Raspberry Pi (2025)
1.3 Inch OLED Display Code: Complete Guide for Arduino & Raspberry Pi (2025)
For makers, engineers, and DIY enthusiasts, writing functional code for 1.3 inch OLED displays hinges on understanding driver compatibility, interface protocols, and platform-specific libraries. This guide provides ready-to-use code snippets, hardware setup tips, and troubleshooting solutions—optimized for OPL’s 1.3 inch OLED modules.
1. Key Preparations: Drivers & Hardware Basics
1.3 inch OLEDs primarily use SH1106 (most common) and SSD1306 driver chips ,with I2C/SPI interfaces dominating connectivity. OPL’s 1.3 inch lineup (e.g., OPL-M130) universally supports these standards, ensuring library compatibility.
Critical Hardware Checks
| Component | OPL Recommendation | Purpose |
|---|---|---|
| Driver Chip | SH1106 (OPL-M130/OPL-V130) | Controls pixel addressing |
| Interface | I2C (4 pins) / SPI (7 pins) | Balances simplicity/speed |
| Power Supply | 3.3V DC (20mA typical current) | Prevents burnout (1.3 寸功耗高于 0.96 寸) |
| Pull-Up Resistors | 4.7kΩ (for I2C SDA/SCL lines) | Stabilizes signal transmission |
2. Arduino Code Tutorial (SH1106/SSD1306)
Arduino is the most popular platform for 1.3 inch OLEDs—below are tested code examples for OPL modules.
Step 1: Library Installation
Install required libraries via Arduino Library Manager:
- SH1106 Modules: "Adafruit SH110X" (supports OPL-M130)
- SSD1306 Modules: "Adafruit SSD1306" + "Adafruit GFX"
Step 2: Basic Text Display (I2C Interface)
| OPL Pin | Arduino Pin | Function |
|---|---|---|
| VCC | 3.3V | Power |
| GND | GND | Ground |
| SDA | A4 | I2C Data |
| SCL | A5 | I2C Clock |
#include <Adafruit_SH110X.h>
#include <Wire.h>
// OPL-M130 I2C address: 0x3C (default)
#define OLED_ADDR 0x3C
Adafruit_SH1106G display(128, 64, &Wire, OLED_ADDR);
void setup() {
Serial.begin(9600);
// Initialize display (1.3寸SH1106需特殊初始化)
if(!display.begin(0x00)) {
Serial.println("Display init failed");
while(1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
// Display multi-language text
display.setCursor(20, 0); // X=20, Y=0 (1.3寸需偏移校正)
display.print("OPL OLED Test");
display.setCursor(35, 16);
display.print("你好,世界"); // 支持中文显示
display.setCursor(28, 32);
display.print("128x64 Resolution");
display.setCursor(15, 48);
display.print("SH1106 Driver");
display.display(); // Refresh screen
delay(5000);
}
void loop() {
// Toggle contrast (0-255)
for(int b=0; b<=255; b+=10) {
display.contrast(b);
delay(100);
}
}
Step 3: Image Display (BMP Format)
- Prepare Image: Resize to 128×64 (monochrome) and convert to byte array via Img2Lcd (horizontal scan, C language output) 。
- Code Implementation:
#include <Adafruit_SH110X.h>
#include <Wire.h>
Adafruit_SH1106G display(128, 64, &Wire, 0x3C);
// OPL logo (128x64 byte array, generated by Img2Lcd)
const unsigned char opl_logo[] PROGMEM = {
0X00,0X00,0X00,0X00,0X03,0XC7,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X1E,0XFF,0XFC,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
// Add full 1024-byte array (128x64=8192 bits = 1024 bytes)
};
void setup() {
display.begin(0x00);
display.clearDisplay();
// Draw bitmap (X=0, Y=0, bitmap, width=128, height=64, color=WHITE)
display.drawBitmap(0, 0, opl_logo, 128, 64, SH110X_WHITE);
display.display();
}
void loop() {}
Step 4: Dynamic Animation (PBM Frames)
#include <Adafruit_SH110X.h>
#include <Wire.h>
#include <framebuf.h>
#include <SD.h>
Adafruit_SH1106G display(128, 64, &Wire, 0x3C);
File frameFile;
void setup() {
display.begin(0x00);
SD.begin(4); // SD card CS pin = D4
}
void loop() {
// Load 6 frames (scatman.1.pbm to scatman.6.pbm)
for(int n=1; n<=6; n++){
frameFile = SD.open("scatman."+String(n)+".pbm", FILE_READ);
if(frameFile){
// Skip PBM header (3 lines)
frameFile.readStringUntil('\n');
frameFile.readStringUntil('\n');
frameFile.readStringUntil('\n');
// Read pixel data
byte data[1024];
frameFile.read(data, 1024);
// Create frame buffer
framebuf fb(data, 128, 64, framebuf.MONO_HLSB);
// Draw and refresh
display.blit(&fb, 0, 0);
display.display();
frameFile.close();
delay(100); // Animation speed
}
}
}
3. Raspberry Pi Code Tutorial (Python)
luma.oled library for OPL’s 1.3 inch modules.
Step 1: Install Dependencies
sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install luma.oled
Step 2: I2C Setup
sudo raspi-config -> Interface Options -> I2C -> Enable
sudo reboot
Step 3: Text & Graphics Code (SH1106)
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106
from luma.core.render import canvas
from PIL import ImageDraw, ImageFont
# Initialize OPL-M130 (I2C address 0x3C)
serial = i2c(port=1, address=0x3C)
device = sh1106(serial, width=128, height=64, rotate=0)
# Load font (16px, supports CJK)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)
with canvas(device) as draw:
# Draw text
draw.text((20, 0), "OPL 1.3\" OLED", font=font, fill="white")
draw.text((35, 20), "Raspberry Pi", font=font, fill="white")
draw.text((40, 40), "Test", font=font, fill="white")
# Keep display active
try:
while True:
pass
except KeyboardInterrupt:
device.clear()
4. Common Code Issues & Fixes
Issue 1: Display Offset/Glitching
Cause: SH1106 1.3 寸屏显存起始列地址为 X=2(0.96 寸为 X=0) 。
Fix: Correct cursor position in code:
// For SH1106 1.3 inch (OPL-M130)
void set_cursor(uint8_t x, uint8_t y) {
x += 2; // Add 2px offset
display.setCursor(x, y);
}
Issue 2: I2C Communication Failure
Cause: Missing pull-up resistors or incorrect address.
Fix:
- Add 4.7kΩ resistors between SDA/SCL and 3.3V.
- Verify address with I2C scanner:
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(9600);
for(byte addr=0; addr<128; addr++){
Wire.beginTransmission(addr);
if(Wire.endTransmission()==0) Serial.print("I2C Address: 0x");
if(addr<16) Serial.print("0");
Serial.println(addr,HEX);
}
}
void loop(){}
Issue 3: Image Distortion
Cause: Incorrect image resolution or scan mode.
Fix: Use 128×64 monochrome BMP/PBM and set Img2Lcd to "horizontal scan" .
5. OPL 1.3 Inch OLED Code Compatibility
| OPL Model | Driver Chip | Supported Platforms | Recommended Library |
|---|---|---|---|
| OPL-M130 | SH1106 | Arduino, ESP32 | Adafruit SH110X |
| OPL-V130 | SH1106 | Raspberry Pi, STM32 | luma.oled (Python) |
| OPL-C130 (Color) | SSD1351 | Arduino Mega, Due | Adafruit SSD1351 |
Download OPL Code Packages:
Visit www.opldisplaytec.com/support to get ready-to-run examples for all models.
6. Contact OPL for Technical Support
- Email: info@opldisplay.com (send code snippets for tailored fixes)
- Website: www.opldisplaytec.com (access video tutorials and FAQs)
- Support: 24-hour response for code-related issues.
Ms.Josey
Ms.Josey