Bird
0
0
Raspberry Piprogramming~10 mins

OLED display with I2C (SSD1306) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - OLED display with I2C (SSD1306)
Start Raspberry Pi
Initialize I2C bus
Create SSD1306 display object
Clear display buffer
Draw text or graphics
Send buffer to OLED via I2C
Display updated
End or repeat
The Raspberry Pi starts, sets up I2C communication, creates the OLED display object, clears the screen, draws content, sends it over I2C, and updates the display.
Execution Sample
Raspberry Pi
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.text('Hello!', 0, 0, 1)
display.show()
This code initializes the OLED display over I2C and shows the text 'Hello!' on the screen.
Execution Table
StepActionI2C Bus StateDisplay Buffer StateOutput
1Import librariesI2C bus not startedEmptyNo output
2Initialize I2C busI2C bus readyEmptyNo output
3Create SSD1306 objectI2C bus readyEmpty bufferNo output
4Clear display buffer (fill 0)I2C bus readyBuffer cleared (all pixels off)No output
5Draw text 'Hello!' at (0,0)I2C bus readyBuffer updated with text pixelsNo output yet
6Send buffer to OLED (display.show())I2C bus active, data sentBuffer unchangedText 'Hello!' appears on OLED
7End or repeatI2C bus readyBuffer unchangedDisplay shows 'Hello!'
💡 Display updated and text shown; program ends or waits for next command.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6Final
i2cnullI2C object readyI2C object readyI2C object readyI2C object readyI2C object ready
displaynullnullSSD1306 object with cleared bufferSSD1306 object with text bufferSSD1306 object with text bufferSSD1306 object with text buffer
Key Moments - 2 Insights
Why does the text not appear immediately after calling display.text()?
Because display.text() only updates the buffer in memory; the display.show() call sends the buffer over I2C to update the OLED screen, as shown in step 6 of the execution_table.
What happens if the I2C bus is not initialized before creating the display object?
The display object cannot communicate with the OLED, so no display updates occur. This is why step 2 (I2C initialization) must happen before step 3 (creating the display object).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the display buffer state after step 5?
ABuffer unchanged from start
BBuffer cleared with all pixels off
CBuffer updated with text pixels
DBuffer sent to OLED
💡 Hint
Check the 'Display Buffer State' column at step 5 in the execution_table.
At which step does the OLED screen actually show the text 'Hello!'?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the 'Output' column in the execution_table to see when the text appears.
If the I2C bus was not ready at step 3, what would happen?
AThe display object could not communicate with the OLED
BThe display object would still work normally
CThe display buffer would clear automatically
DThe text would appear without calling display.show()
💡 Hint
Refer to the key_moments explanation about I2C initialization before creating the display object.
Concept Snapshot
Initialize I2C bus on Raspberry Pi
Create SSD1306_I2C display object with width, height, and I2C
Clear display buffer with fill(0)
Draw text with text(string, x, y, color)
Update OLED screen with show()
Always initialize I2C before display object
Full Transcript
This example shows how to use an OLED display with I2C on a Raspberry Pi. First, the I2C bus is initialized using busio.I2C with the board's SCL and SDA pins. Then, the SSD1306_I2C display object is created with the display size and the I2C bus. The display buffer is cleared with fill(0), which turns off all pixels in memory. Next, text is drawn into the buffer using display.text with the string 'Hello!' at position (0,0). The text does not appear on the screen yet because it is only in the buffer. Finally, display.show() sends the buffer data over I2C to the OLED, making the text visible. The program can then end or update the display again. Key points include initializing I2C before creating the display object and calling show() to update the screen after drawing.