A joystick is just two potentiometers and a button wired together, but it is a handy way to get 2D input into a microcontroller project. This guide walks through wiring a joystick to an Arduino Giga R1 WiFi and reading its position and button state with MicroPython, using the DIYables_MicroPython_Joystick library.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MicroPython Joystick
What you need
- Arduino Giga R1 WiFi
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Joystick
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega/Giga
- Sensors/Servo Expansion Shield for Arduino Mega/Giga
- Breadboard Shield for Arduino Mega/Giga
- Enclosure for Arduino Giga
- Power Splitter for Arduino Giga
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The joystick has five pins. GND goes to ground, and VCC connects to the Arduino's 3.3V supply. VRX and VRY are analog outputs for the horizontal and vertical position, and each connects to an ADC-capable pin on the Arduino. SW is the pushbutton pin; it's normally open, so a pull-up resistor keeps it HIGH when the button is untouched and pulls it LOW when pressed, giving you a clean digital signal instead of a floating pin.
In the example code, VRX goes to A0, VRY to A1, and SW to D2.
How it works
The joystick is really two potentiometers arranged at right angles, one for each axis. Moving it left or right changes the voltage on VRX between 0V and 3.3V, and the Arduino's ADC converts that into a value from 0 to 4095. Moving it up or down does the same thing on VRY. When the joystick sits in its neutral position, both readings center around the middle of that range, which is how you detect direction and magnitude of movement.
Code
The script creates a Joystick object from the VRX, VRY and SW pins, then calls joystick.loop() in every iteration, which handles debouncing the button (you can tune the debounce time with set_debounce_time()). From there it reads the X and Y axis values with read_x() and read_y(), checks is_pressed() and is_released() to catch button transitions, and keeps a running count of presses with get_press_count(). The loop prints all of this to the Thonny shell so you can see the raw coordinates and button activity as you move the stick.
arduino-micropython-joystick.py
"""
This Arduino MicroPython script was developed by newbiely.com
This Arduino MicroPython script is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-joystick
"""
from DIYables_MicroPython_Joystick import Joystick
from machine import Pin, ADC
import time
VRX_PIN = 'A0' # The Arduino Giga R1 WiFi's pin connected to VRX pin of joystick
VRY_PIN = 'A1' # The Arduino Giga R1 WiFi's pin connected to VRY pin of joystick
SW_PIN = 'D2' # The Arduino Giga R1 WiFi's pin connected to SW pin of joystick
joystick = Joystick(pin_x=VRX_PIN, pin_y=VRY_PIN, pin_button=SW_PIN)
# Configure the debounce time if necessary (default is 50ms)
joystick.set_debounce_time(100) # debounce time set to 100 milliseconds
while True:
joystick.loop() # Must be called frequently to process button debouncing
# Read the analog values from the X and Y axes
x_value = joystick.read_x()
y_value = joystick.read_y()
press_count = joystick.get_press_count()
# Check if the button has been pressed or released
if joystick.is_pressed():
print("Button Pressed")
if joystick.is_released():
print("Button Released")
# Print the joystick's X and Y coordinates, and pressed count
print(f'Joystick Position - X: {x_value}, Y: {y_value}, pressed count: {press_count}')
time.sleep(0.1) # Delay to reduce the output frequency, note that, long delay can make it miss the thump press
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-joystick

Top comments (0)