Arduino MicroPython Joystick
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-joystick
This guide shows you how to read a joystick with an Arduino Giga R1 WiFi running MicroPython. The joystick is built from two potentiometers and a pushbutton, giving an X position, a Y position, and a button state. You can use these three values to drive menus, games, or other projects, and this build walks through wiring it up and printing its readings over serial.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 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. Connect GND to ground and VCC to the Arduino's 3.3V supply. Connect VRX to an ADC pin on the board and VRY to another ADC pin, since both carry analog position values. Connect SW, the pushbutton pin, to a digital pin; the button is normally open, so this pin needs a pull-up to read HIGH when not pressed and LOW when pressed. In the example code, VRX goes to A0, VRY to A1, and SW to D2.
How It Works
Moving the joystick left or right changes the voltage on VRX: left drops it toward 0V, right raises it toward 3.3V. The Arduino's ADC converts that voltage into a value from 0 to 4095. VRY works the same way for up and down movement, with up near 0V and down near 3.3V. The two axes together give a 2-D coordinate that sits in the middle of the range when the joystick is centered, plus a separate ON/OFF reading from the button.
Code
The script uses the DIYables_MicroPython_Joystick library, creating a Joystick object on the X, Y, and button pins and setting a 100ms debounce time for the button. In the main loop it calls joystick.loop() to handle debouncing, then reads the X and Y values, checks is_pressed() and is_released() to report button transitions, and tracks get_press_count(). It prints the X position, Y position, and press count once every 0.1 seconds.
arduino-micropython-joystick.py - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-joystick