Bird
0
0
Raspberry Piprogramming~20 mins

OLED display with I2C (SSD1306) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OLED Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output on the OLED display?
Given the following Python code snippet for an SSD1306 OLED display connected via I2C on a Raspberry Pi, what text will appear on the screen?
Raspberry Pi
from PIL import Image, ImageDraw, ImageFont
import board
import busio
import adafruit_ssd1306

i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
display.fill(0)
display.show()

image = Image.new('1', (display.width, display.height))
draw = ImageDraw.Draw(image)

font = ImageFont.load_default()
draw.text((0, 0), 'Hello, Pi!', font=font, fill=255)
display.image(image)
display.show()
AThe OLED shows the text 'Hello, Pi!' at the top-left corner
BThe OLED shows the text 'Hello, Pi!' centered on the screen
CThe OLED remains blank with no text displayed
DThe OLED shows an error message about I2C communication
Attempts:
2 left
💡 Hint
Look at the coordinates used in draw.text and the display size.
🧠 Conceptual
intermediate
1:30remaining
Why is the display.fill(0) method called before drawing?
In the SSD1306 OLED display code, what is the purpose of calling display.fill(0) before drawing new content?
AIt clears the display by turning all pixels off
BIt sets the brightness of the display to zero
CIt initializes the I2C communication bus
DIt saves the current display content to memory
Attempts:
2 left
💡 Hint
Think about what 'fill(0)' might mean for a pixel-based screen.
🔧 Debug
advanced
2:30remaining
Identify the error causing the display to remain blank
This code is intended to display 'Test' on the OLED, but the screen stays blank. What is the cause?
Raspberry Pi
from PIL import Image, ImageDraw, ImageFont
import board
import busio
import adafruit_ssd1306

i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
display.fill(0)
display.show()

image = Image.new('1', (display.width, display.height))
draw = ImageDraw.Draw(image)

font = ImageFont.load_default()
draw.text((0, 0), 'Test', font=font, fill=255)
# Missing display.image(image) here
display.show()
AThe font is not loaded correctly, causing no text to render
BThe display size 128x64 is not supported by the OLED
CThe I2C bus is not initialized properly
DThe display.image(image) call is missing before display.show()
Attempts:
2 left
💡 Hint
Check if the image is sent to the display before calling show().
📝 Syntax
advanced
2:00remaining
Which code snippet correctly initializes the SSD1306 display?
Choose the correct Python code to initialize a 128x32 SSD1306 OLED display over I2C on a Raspberry Pi.
A
i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_SPI(128, 32, i2c)
B
i2c = busio.I2C(board.SDA, board.SCL)
display = adafruit_ssd1306.SSD1306_I2C(32, 128, i2c)
C
i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
D
i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
Attempts:
2 left
💡 Hint
Check the order of pins and display dimensions.
🚀 Application
expert
3:00remaining
How many pixels are lit after this drawing code runs?
Consider this code drawing a white rectangle on a 128x32 SSD1306 OLED display. How many pixels are lit (set to 1) after running?
Raspberry Pi
from PIL import Image, ImageDraw
import board
import busio
import adafruit_ssd1306

i2c = busio.I2C(board.SCL, board.SDA)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
display.fill(0)
display.show()

image = Image.new('1', (display.width, display.height))
draw = ImageDraw.Draw(image)

draw.rectangle((10, 5, 20, 15), outline=255, fill=255)
display.image(image)
display.show()
A110 pixels
B121 pixels
C100 pixels
D1210 pixels
Attempts:
2 left
💡 Hint
Calculate width and height of the rectangle and multiply.