Arduino MicroPython Joystick

by DIYables in Circuits > Arduino

8 Views, 0 Favorites, 0 Comments

Arduino MicroPython Joystick

arduino-micropython-joystick-cover.jpg

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

Wiring

arduino-micropython-joystick-wiring-diagram.jpg

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.

"""
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 and code explanation: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-joystick