To create a simple interactive game using Raspberry Pi Pico with an OLED SSD1306 display and two buttons. The game moves a square left or right on the screen based on button input, demonstrating the use of I2C OLED interfacing and GPIO button control with Micro Python.
Buy Basic Raspberry Pi Pico Kit
Items Required:
- Raspberry Pi Pico
- OLED Display (SSD1306, I2C)
- 2 x Push buttons
- Breadboard and jumper wires
- USB Micro B Cable
- Micro Python firmware loaded on Pico
Note:
Buttons use internal pull-up resistors.
Connections:
OLED Display (SSD1306 I2C):
VCC to Pico 3.3V (Physical pin 36)
GND to Pico GND (Physical pin 38 or 3)
SDA to Pico GPIO0 (Physical pin 1)
SCL to Pico GPIO1 (Physical pin 2)


Code:
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import time
# I2C Setup
i2c = I2C(0, scl=Pin(1), sda=Pin(0)) # Pico Pin 2 = SCL, Pin 1 = SDA
oled = SSD1306_I2C(128, 64, i2c)
# Button Setup
button_left = Pin(4, Pin.IN, Pin.PULL_UP)
button_right = Pin(5, Pin.IN, Pin.PULL_UP)
# Box position
x = 64
y = 30
# Draw initial box
oled.fill(0)
oled.fill_rect(x, y, 8, 8, 1)
oled.show()
while True:
if not button_left.value(): # button pressed (active LOW)
x = max(0, x - 2)
elif not button_right.value():
x = min(120, x + 2)
oled.fill(0)
oled.fill_rect(x, y, 8, 8, 1)
oled.text("Move!", 0, 0)
oled.show()
time.sleep(0.05)
Result
when the code runs, a square appears on the OLED screen. Pressing the left button moves the square left, and pressing the right button moves it to the right. This verifies the correct working of OLED display control via I2C and button input detection through GPIO pins using Micro Python on Raspberry Pi Pico.