Built-in project · Raspberry Pi Pico

MicroPython joystick on a Pico: X, Y and the press

MicroPython on a Raspberry Pi Pico: a joystick’s X on GP28 and Y on GP27 read as analog, and its switch on GP26, printed ten times a second. The whole build, a Raspberry Pi Pico and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $5.50. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading analog inputs with machine.ADC
  • A joystick as two potentiometers and a switch
  • 16 bit readings, 0 to 65535
  • Reading a switch with Pin.PULL_UP
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 3

How it works

A joystick module has two potentiometers, one for each direction, plus a push switch. X goes to GP28 and Y to GP27 as analog inputs. The switch goes to GP26 as a digital input with the Pico's pull-up turned on.

Ten times a second the script prints X, Y and Z. X and Y come from read_u16, so they run from 0 to 65535. Z is 1 normally and 0 when the stick is pushed down.

Parts list

3 parts, plus the jumper wires. They retail for about $5.50; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Thumb stick pin VRXRaspberry Pi Pico pin GP28
Thumb stick pin VRYRaspberry Pi Pico pin GP27
Thumb stick pin SWRaspberry Pi Pico pin GP26
Thumb stick pin GNDGround
Thumb stick pin VCC3.3 V

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A joystick: X on GP28 (ADC2), Y on GP27 (ADC1), and the press switch
# on GP26, read as a digital pin with its pull-up on, so pressed is 0.
from machine import ADC, Pin
import time

x = ADC(28)
y = ADC(27)
z = Pin(26, Pin.IN, Pin.PULL_UP)

while True:
    print("X %5d  Y %5d  Z %d" % (x.read_u16(), y.read_u16(), z.value()))
    time.sleep_ms(100)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Z always shows 1 This project
    Z drops to 0 only while the stick is pushed down. Check that SW goes to GP26.
  • X and Y are swapped This project
    X is on GP28 and Y on GP27. Check that VRX and VRY go to the right pins.
  • The axis reads about half scale at rest instead of exactly zero, and the values drift a little, so the cursor or motor creeps. Thumb stick
    Center is about half of the ADC range (about 512 on a 10 bit Arduino). Read it at startup and subtract that, then add a dead zone of a few counts.
  • The push button reads random values or always looks pressed. The switch pin is a plain contact to ground and has no pull-up on the stick. Thumb stick
    Set the switch pin to INPUT_PULLUP and treat LOW as pressed. Debounce it in code. Power the stick from the same voltage as the ADC reference.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.