An OLED display with I2C (SSD1306) lets you show text and images on a small screen using just two wires. It is great for adding simple visual output to your Raspberry Pi projects.
OLED display with I2C (SSD1306) in Raspberry Pi
import board import busio from adafruit_ssd1306 import SSD1306_I2C # Create I2C interface i2c = busio.I2C(board.SCL, board.SDA) # Create display object with width and height display = SSD1306_I2C(128, 64, i2c) # Clear the display display.fill(0) display.show() # Write text on the display from PIL import Image, ImageDraw, ImageFont image = Image.new('1', (display.width, display.height)) draw = ImageDraw.Draw(image) # Draw text at position (x, y) draw.text((0, 0), 'Hello, Pi!', fill=255) # Display image on OLED display.image(image) display.show()
import board import busio from adafruit_ssd1306 import SSD1306_I2C i2c = busio.I2C(board.SCL, board.SDA) display = SSD1306_I2C(128, 64, i2c) display.fill(0) display.show()
from PIL import Image, ImageDraw image = Image.new('1', (128, 64)) draw = ImageDraw.Draw(image) draw.text((10, 20), 'Hi!', fill=255) display.image(image) display.show()
This program initializes the OLED display, clears it, draws the text 'Hello, Pi!' at the top-left corner, and shows it on the screen. It also prints a confirmation message to the console.
import board import busio from adafruit_ssd1306 import SSD1306_I2C from PIL import Image, ImageDraw, ImageFont # Setup I2C and display i2c = busio.I2C(board.SCL, board.SDA) display = SSD1306_I2C(128, 64, i2c) display.fill(0) display.show() # Create blank image for drawing image = Image.new('1', (display.width, display.height)) draw = ImageDraw.Draw(image) # Draw some text text = 'Hello, Pi!' draw.text((0, 0), text, fill=255) # Show image on display display.image(image) display.show() print('Text displayed on OLED screen.')
Make sure your Raspberry Pi I2C interface is enabled in the settings.
Use a 3.3V power supply for the OLED to avoid damage.
If the display does not show anything, check the I2C address with 'i2cdetect -y 1' command.
OLED displays with SSD1306 use I2C to communicate with Raspberry Pi using only two wires.
You can draw text and images by creating a small image and sending it to the display.
Remember to clear the display before drawing new content to avoid leftover pixels.
Practice
show() method when using an SSD1306 OLED display with I2C on a Raspberry Pi?Solution
Step 1: Understand the display buffer concept
The OLED display uses a buffer to store what will be shown on the screen before updating.Step 2: Role of
Theshow()methodshow()method sends the buffer content to the OLED, updating the visible display.Final Answer:
To update the display with the current buffer content -> Option AQuick Check:
show()updates screen [OK]
- Confusing show() with clear()
- Thinking show() initializes I2C
- Assuming show() changes brightness
Solution
Step 1: Identify the method to clear the buffer
The SSD1306 library usesfill(0)to fill the buffer with zeros, effectively clearing it.Step 2: Confirm other options are incorrect
Methods likeclear(),reset(), orerase()are not standard in this library.Final Answer:
oled.fill(0) -> Option CQuick Check:
Clear buffer = fill(0) [OK]
- Using non-existent clear() method
- Confusing fill(1) with clearing
- Trying reset() which is not in library
oled.fill(0)
oled.text('Hi', 0, 0)
oled.show()Solution
Step 1: Analyze the code sequence
The buffer is cleared withfill(0), then 'Hi' is drawn at coordinates (0,0).Step 2: Understand coordinate system and show()
Coordinates (0,0) place text at top-left. Callingshow()updates the display.Final Answer:
The text 'Hi' appears at the top-left corner -> Option DQuick Check:
Text at (0,0) shows top-left [OK]
- Assuming text() needs more arguments
- Thinking (0,0) is bottom-right
- Forgetting to call show() to update
oled.text('Hello', 10, 10)
oled.fill(0)
oled.show()
What is the problem?Solution
Step 1: Check the order of commands
Text is drawn first, thenfill(0)clears the buffer, erasing the text.Step 2: Understand correct sequence
Clear buffer first withfill(0), then draw text, then callshow().Final Answer:
Calling fill(0) after text() clears the buffer -> Option BQuick Check:
Clear after draw erases text [OK]
- Thinking coordinates are invalid
- Assuming init() is needed every time
- Believing text case matters
Solution
Step 1: Understand how to avoid leftover pixels
Clearing the buffer before drawing new text removes old pixels that would remain otherwise.Step 2: Proper update sequence for smooth scrolling
Each frame: clear buffer withfill(0), draw new text, then callshow()to update display.Final Answer:
Clear buffer with fill(0), draw new text, then call show() each frame -> Option AQuick Check:
Clear before draw + show() = smooth update [OK]
- Not clearing buffer causes ghosting
- Calling show() only once delays updates
- Using non-existent clear() method
Ready to prove it?
Pass the test and this pattern moves unknown
