Breadboard
docs
Example project · Displays & sensors

Analog joystick reader

Reads the two joystick axes on the ADC and the push switch, printing the direction. Move the stick with the X/Y sliders in the Inspector, or press its button.

The Analog joystick reader circuit as rendered by the simulator

What's on the bench

  • Battery
  • ESP32-C3
  • Joystick
  • Resistor

The code

This MicroPython script runs on the emulated board every boot; edit it in the Code tab.

from machine import Pin, ADC
import time
x = ADC(Pin(0)); x.atten(ADC.ATTN_11DB)
y = ADC(Pin(1)); y.atten(ADC.ATTN_11DB)
sw = Pin(2, Pin.IN)
while True:
    xv = x.read_u16() / 65535
    yv = y.read_u16() / 65535
    d = ''
    d += 'R' if xv > 0.6 else 'L' if xv < 0.4 else ''
    d += 'U' if yv > 0.6 else 'D' if yv < 0.4 else ''
    if sw.value() == 0: d += '*'
    print('x %.2f y %.2f %s' % (xv, yv, d or 'center'))
    time.sleep_ms(300)