Menu Close

OLED 1306 Raspberry pi Pico Small Game

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.

 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)

Download Code

Leave a Reply