0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use I2C OLED Display with Raspberry Pi: Step-by-Step Guide

To use an I2C OLED display with a Raspberry Pi, first enable I2C in the Raspberry Pi settings and connect the display's SDA and SCL pins to the Pi's corresponding pins. Then, install the Adafruit SSD1306 Python library and use a simple Python script to display text or images on the OLED screen.
📐

Syntax

Here is the basic Python syntax to initialize and use an I2C OLED display with the Adafruit SSD1306 library on Raspberry Pi:

  • import board: Access Raspberry Pi pins.
  • import busio: Use I2C communication.
  • from adafruit_ssd1306 import SSD1306_I2C: Import OLED driver.
  • i2c = busio.I2C(board.SCL, board.SDA): Create I2C interface.
  • oled = SSD1306_I2C(width, height, i2c): Initialize OLED display.
  • oled.fill(0): Clear display.
  • oled.text('Hello', x, y, color): Write text at position.
  • oled.show(): Update display to show changes.
python
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C

# Create I2C interface

i2c = busio.I2C(board.SCL, board.SDA)

# Initialize OLED display (128x64 pixels)

oled = SSD1306_I2C(128, 64, i2c)

# Clear the display

oled.fill(0)

oled.show()

# Write text

oled.text('Hello, Pi!', 0, 0, 1)

oled.show()
💻

Example

This example shows how to display the message "Hello, Raspberry Pi!" on a 128x64 I2C OLED screen connected to the Raspberry Pi.

Make sure you have enabled I2C on your Raspberry Pi and installed the required libraries.

python
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C

# Initialize I2C interface

i2c = busio.I2C(board.SCL, board.SDA)

# Initialize OLED display

oled = SSD1306_I2C(128, 64, i2c)

# Clear display

oled.fill(0)

oled.show()

# Display text

oled.text('Hello, Raspberry Pi!', 0, 0, 1)

oled.show()
⚠️

Common Pitfalls

  • I2C not enabled: Forgetting to enable I2C in Raspberry Pi configuration will prevent communication.
  • Wrong wiring: SDA and SCL pins must be connected correctly; swapping them causes failure.
  • Missing libraries: Not installing adafruit-circuitpython-ssd1306 or dependencies leads to import errors.
  • Incorrect I2C address: Some OLED displays use different addresses; check with i2cdetect -y 1.
  • Not clearing display: Always clear the display before writing new content to avoid overlapping.
python
### Wrong way: Not enabling I2C or wrong pins
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C

i2c = busio.I2C(board.D5, board.D6)  # Wrong pins
oled = SSD1306_I2C(128, 64, i2c)

# This will cause an error or no display output

### Right way: Use correct pins and enable I2C
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C

i2c = busio.I2C(board.SCL, board.SDA)  # Correct pins
oled = SSD1306_I2C(128, 64, i2c)

oled.fill(0)
oled.text('Correct setup', 0, 0, 1)
oled.show()
📊

Quick Reference

Summary tips for using I2C OLED with Raspberry Pi:

  • Enable I2C via sudo raspi-config under Interface Options.
  • Connect OLED SDA to Pi SDA (GPIO 2) and SCL to Pi SCL (GPIO 3).
  • Install libraries: pip3 install adafruit-circuitpython-ssd1306 and dependencies.
  • Use i2cdetect -y 1 to find your OLED address (usually 0x3C).
  • Clear display before writing new content with oled.fill(0).
  • Call oled.show() to update the screen after changes.

Key Takeaways

Enable I2C on Raspberry Pi before connecting the OLED display.
Connect SDA and SCL pins correctly to GPIO 2 and GPIO 3 respectively.
Install the Adafruit SSD1306 Python library to control the OLED.
Always clear the display before writing new text or images.
Use i2cdetect to verify the OLED's I2C address if connection issues occur.