Built-in project · Raspberry Pi Pico W

Pico W joystick on GP27, GP26 and GP22

MicroPython on a Pico W: a joystick with X on GP27, Y on GP26 and its press switch on GP22 with a 10k pull-up, printed five times a second. The whole build, a Raspberry Pi Pico W and 2 more parts, 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 $7.55. 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
  • Using an external pull-up resistor on a switch
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 5

How it works

A joystick module has two potentiometers, one for each direction, plus a push switch. X goes to GP27 and Y to GP26, and both are read as analog values. The switch goes to GP22 with a 10k pull-up to 3.3 V.

Five times a second the script prints X, Y and pressed. The readings use read_u16, so they run from 0 to 65535. The switch reads 0 when pushed, so the script prints 1 minus the pin value, which makes pressed read 1.

Parts list

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

Kits that can build it

These 2 starter kits have 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 W pin GP27
Thumb stick pin VRYRaspberry Pi Pico W pin GP26
Thumb stick pin SWRaspberry Pi Pico W pin GP22
Resistor, 10k Ω pin 2Raspberry Pi Pico W pin GP22
Thumb stick pin GNDGround
Thumb stick pin VCC3.3 V
Resistor, 10k Ω pin 13.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 W
# Toggle the joystick: X on GP27 (ADC1), Y on GP26 (ADC0), and the press
# switch on GP22 with a 10k pull-up, so pressed reads 0.
from machine import ADC, Pin
import time

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

while True:
    print("X %d  Y %d  pressed %d" % (x.read_u16(), y.read_u16(), 1 - z.value()))
    time.sleep_ms(200)

Troubleshooting

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

  • Pressed always shows 0 This project
    Check that the 10k resistor runs from SW (GP22) to 3.3 V, and that you push the stick straight down.
  • X and Y seem swapped This project
    X is on GP27 and Y on GP26. Check that VRX and VRY go to those 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 LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

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