Build it step by step
0 of 7 doneWire the joystick
VCC to 3V3(OUT), GND to GND, VRx to GP26, VRy to GP27, SW to GP15.
Wire the OLED
VCC to 3V3(OUT), GND to GND, SDA to GP0, SCL to GP1.
Install the ssd1306 module
Using Thonny packages.
Run the program
A three-square snake moves right.
Steer
Push the stick in the direction you want.
Eat the dot
The snake grows and the score goes up.
Press the stick after game over
A new game starts.
How do you build a pico joystick snake game?
A Pico reads a two-axis joystick and plays the snake game on an OLED, with a growing tail, a score and a game over screen. You need Raspberry Pi Pico H, Joystick module (dual axis), SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 7 steps and about 160 minutes.
- Level
- Beginner
- Time
- About 160 min
- Parts
- 4
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Joystick VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| Joystick GND | Pico GND pin 38 | Common ground |
| Joystick VRx | Pico GP26 pin 31 | Left and right (ADC0) |
| Joystick VRy | Pico GP27 pin 32 | Up and down (ADC1) |
| Joystick SW | Pico GP15 pin 20 | Stick press, restarts the game |
| OLED VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| OLED GND | Pico GND pin 3 | Common ground |
| OLED SDA and SCL | Pico GP0 pin 1 and GP1 pin 2 | I2C data and clock |
Code
A joystick module has two potentiometers, one for each direction, and a push switch. At rest each potentiometer gives about half the supply voltage, and pushing the stick moves the voltage up or down, which the Pico reads as a number from 0 to 65535 on two analog pins. The program treats a reading above 45000 or below 20000 as a push in that direction, and ignores the middle zone so a stick at rest does nothing. The snake is a list of grid squares: each step the head moves one square, a new head is added at the front, and the tail is removed unless the snake just ate, so it grows. Hitting yourself ends the game. The screen is split into a grid of 4 by 4 pixel squares, and the program speeds up a little with each point. The snake wraps around the edges of the screen instead of dying, which makes the game easier to learn.
import time
import random
from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
vrx = ADC(26)
vry = ADC(27)
button = Pin(15, Pin.IN, Pin.PULL_UP)
CELL = 4
GRID_W, GRID_H = 32, 14 # 128 by 56 pixels below the score line
TOP = 8
def new_game():
snake = [(16, 7), (15, 7), (14, 7)]
return snake, (1, 0), 0
def place_food(snake):
while True:
f = (random.randint(0, GRID_W - 1), random.randint(0, GRID_H - 1))
if f not in snake:
return f
def read_direction(current):
x = vrx.read_u16()
y = vry.read_u16()
d = current
if x > 45000:
d = (1, 0)
elif x < 20000:
d = (-1, 0)
elif y > 45000:
d = (0, 1)
elif y < 20000:
d = (0, -1)
if d[0] == -current[0] and d[1] == -current[1]:
return current # cannot turn straight back
return d
def draw(snake, food, score):
oled.fill(0)
oled.text("Score " + str(score), 0, 0)
oled.fill_rect(food[0] * CELL, TOP + food[1] * CELL, CELL, CELL, 1)
for i, (sx, sy) in enumerate(snake):
if i == 0:
oled.fill_rect(sx * CELL, TOP + sy * CELL, CELL, CELL, 1)
else:
oled.rect(sx * CELL, TOP + sy * CELL, CELL, CELL, 1)
oled.show()
while True:
snake, direction, score = new_game()
food = place_food(snake)
alive = True
while alive:
direction = read_direction(direction)
head = ((snake[0][0] + direction[0]) % GRID_W, (snake[0][1] + direction[1]) % GRID_H) # wraps at the edges
if head in snake:
alive = False
break
snake.insert(0, head)
if head == food:
score += 1
food = place_food(snake)
else:
snake.pop()
draw(snake, food, score)
time.sleep_ms(max(60, 160 - score * 4)) # speeds up as you score
oled.fill(0)
oled.text("Game over", 24, 16)
oled.text("Score " + str(score), 24, 32)
oled.text("Press stick", 20, 48)
oled.show()
while button.value() == 1:
time.sleep_ms(20)
time.sleep_ms(200)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | Snake moves right on its own |
| Push up | Snake turns up |
| Eat the dot | Score goes up by one |
| Run into the tail | Game over screen appears |
| Press the stick | A new game starts |
If it does not work
Directions are swapped
Your module may be turned, swap the pins or change the signs in read_direction.
Snake drifts when the stick is at rest
Widen the middle zone, for example 25000 to 40000.
Nothing on the screen
Check the I2C wires and that the OLED address is 0x3C.
ImportError for ssd1306
Install the micropython-ssd1306 package.
Too fast
Raise 160 in the delay line to slow it down.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico joystick snake game?
Raspberry Pi Pico H, Joystick module (dual axis), SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the pico joystick snake game take to build?
About 160 minutes for a beginner maker, in 7 steps.
How do I know the pico joystick snake game works?
Start: Snake moves right on its own. Push up: Snake turns up. Eat the dot: Score goes up by one.
Directions are swapped: what should I check?
Your module may be turned, swap the pins or change the signs in read_direction.
Snake drifts when the stick is at rest: what should I check?
Widen the middle zone, for example 25000 to 40000.
Nothing on the screen: what should I check?
Check the I2C wires and that the OLED address is 0x3C.
ImportError for ssd1306: what should I check?
Install the micropython-ssd1306 package.
