I decided to have some fun with it again. Wondered about getting the joystick data up to the Arduino side, so I got that working today... So far just for testing...
If anyone wants to play along, current PIP is up at:
Arduino_UNO_Q/gamepad_test at main · KurtE/Arduino_UNO_Q
Note: Readme could use a bit of work, so far just put in the command lines to remind me how to build and install the sketch and how to start up the python with the uv run command...
Here is a snapshot of it:
Python:
# SPDX-FileCopyrightText: Copyright (C) ARDUINO SRL (http://www.arduino.cc)
#
# SPDX-License-Identifier: MPL-2.0
from arduino.app_utils import *
import time
import pygame
import inputs
led_state = False
# This dict can be left as-is, since pygame will generate a
# pygame.JOYDEVICEADDED event for every joystick connected
# at the start of the program.
joysticks = {}
pygame.init()
pygame.joystick.init()
joystick_count = pygame.joystick.get_count()
print(joystick_count)
#joystick = pygame.joystick.Joystick(0)
#joystick.init()
def loop():
global led_state
event_processed = False
#axis_motions: list[int] = [65536, 65536, 65536, 65536, 65536, 65536]
axis_motion_list: list[int] = []
for event in pygame.event.get() :
event_processed = True
if event.type == pygame.JOYAXISMOTION :
axis_motion_list.append(event.axis)
axis_motion_list.append(int(event.value * 32767))
#axis_motions[event.axis] = event.value
if event.axis == 0 :
print("Left stick X: {}".format(event.value))
elif event.axis == 1:
print("Left stick y: {}".format(event.value))
elif event.axis == 2:
print("Right stick x: {}".format(event.value))
elif event.axis == 3:
print("Right stick y: {}".format(event.value))
else :
print("Other axis {}: {}".format(event.axis,event.value))
elif event.type == pygame.JOYHATMOTION :
Bridge.notify("joy_hat_motion", event.value[0], event.value[1])
print("Hat {}: {}".format(event.hat, event.value))
elif event.type == pygame.JOYBUTTONDOWN :
Bridge.notify("joy_button_down", event.button)
print("button down: {}".format(event.button))
elif event.type == pygame.JOYBUTTONUP :
Bridge.notify("joy_button_up", event.button)
print("button up: {}".format(event.button))
# Handle hotplugging
elif event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
Bridge.notify("joy_device_added", event.device_index)
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
elif event.type == pygame.JOYDEVICEREMOVED:
Bridge.notify("joy_device_removed", event.instance_id)
del joysticks[event.instance_id]
print(f"Joystick {event.instance_id} disconnected")
else :
print("Other Event: {}".format(event))
if event_processed :
if len(axis_motion_list) :
Bridge.notify("joy_axis_motion", axis_motion_list)
print("---")
#time.sleep(1)
#led_state = not led_state
#Bridge.call("set_led_state", led_state)
App.run(user_loop=loop)
And the sketch:
// SPDX-FileCopyrightText: Copyright (C) ARDUINO SRL (http://www.arduino.cc)
//
// SPDX-License-Identifier: MPL-2.0
#include "Arduino_RouterBridge.h"
#include <vector>
uint32_t last_led_update_time = 0;
#define BLINK_TIME 500
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Bridge.begin();
Monitor.begin();
Bridge.provide("joy_button_down", joy_button_down);
Bridge.provide("joy_button_up", joy_button_up);
Bridge.provide("joy_axis_motion", joy_axis_motion);
Bridge.provide("joy_hat_motion", joy_hat_motion);
Bridge.provide("joy_device_added", joy_device_added);
Bridge.provide("joy_device_removed", joy_device_removed);
}
void loop() {
if ((millis() - last_led_update_time) >= BLINK_TIME) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
last_led_update_time = millis();
}
}
void joy_button_down(int btn) {
printk("BDN: %d\n", btn);
}
void joy_button_up(int btn) {
printk("BUP: %d\n", btn);
}
void joy_axis_motion(std::vector<int> motions) {
for (int i = 0; i < motions.size(); i += 2) {
printk("%d:%d ", motions[i], motions[i+1]);
}
printk("\n");
}
void joy_hat_motion(int x, int y) {
printk("Hat: %d %d\n", x, y);
}
void joy_device_added (int index) {
printk("\nJoystick device added: %d\n", index);
}
void joy_device_removed (int index) {
printk("\nJoystick device removed: %d\n", index);
}
Some python output from run:
arduino@Uno-q2:~/gamepad_test$ uv run python/main.py
pygame 2.6.1 (SDL 2.28.4, Python 3.13.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
0
======== App is starting ============================
2026-04-19 20:11:50.615 INFO - [MainThread] App: App started
Other Event: <Event(4352-AudioDeviceAdded {'which': 0, 'iscapture': 0})>
---
Joystick 0 connencted
---
button down: 12
---
button up: 12
---
button down: 4
---
button up: 4
---
button down: 3
---
button up: 3
---
button down: 1
---
button up: 1
---
Hat 0: (0, 1)
---
Hat 0: (0, 0)
---
Left stick y: -0.005554368724631489
Left stick y: 0.027802362132633443
---
Left stick y: 0.032654805139317
---
Left stick y: 0.03451643421735282
---
Left stick y: 0.029389324625385297
---
Left stick y: 0.027436140018921477
---
Left stick y: 0.023194067201757866
---
Left stick y: 0.021454512161626027
---
Left stick y: 0.017120883816034424
---
Left stick y: 0.015381328775902585
---
Left stick y: 0.03488265633106479
---
Left stick X: 0.022339548936429945
Left stick X: 0.044709616382335886
---
Left stick X: 0.04934842982268746
---
Left stick X: 0.053529465620899074
Left stick y: 0.03088473158970916
---
Left stick X: 0.05526902066103091
Left stick y: 0.02359080782494583
---
Left stick y: -0.010162663655507066
---
Left stick y: -0.015900143436994536
---
Left stick y: -0.032227546006653035
---
Left stick y: -0.03842280343028046
---
Left stick y: -0.05694753868221076
---
Left stick X: 0.059297463911862545
Left stick y: -0.11700796533097324
---
Left stick X: 0.06457716605121006
Left stick y: -0.1567430646687216
---
Left stick X: 0.0913113803521836
Left stick y: -0.18939786980803858
---
Left stick X: 0.10901211584826197
Left stick y: -0.2141178624835963
---
Left stick X: 0.1141392254402295
Left stick y: -0.24414807580797754
---
Left stick X: 0.1882381664479507
Left stick y: -0.3395184179204688
---
Left stick X: 0.2087466048158208
Left stick y: -0.36423841059602646
---
Left stick X: 0.23483993041779838
Left stick y: -0.42429883724478895
---
Left stick X: 0.2599871822260201
Left stick y: -0.48432874538407544
---
Left stick X: 0.26557206946012757
Left stick y: -0.56556901760918
---
Left stick X: 0.28235724967192605
Left stick y: -0.6750694296090579
---
Left stick X: 0.3075350199896237
Left stick y: -0.73863948484756
---
Left stick X: 0.3447981200598163
Left stick y: -0.7942747276223029
---
Left stick X: 0.36997589037751394
Left stick y: -0.8313547166356395
---
Left stick X: 0.40537736136967073
Left stick y: -0.8613849299600208
---
Left stick X: 0.45011749626148256
Left stick y: -0.9072847682119205
---
Left stick X: 0.45570238349559006
---
Left stick X: 0.46128727072969755
---
Left stick y: -0.8825647755363628
---
Left stick X: 0.46687215796380505
Left stick y: -0.8128299813837092
---
Left stick X: 0.47105319376201665
Left stick y: -0.7651295510727256
---
Left stick y: -0.6944792016357921
---
Left stick y: -0.620319223609119
---
Left stick y: -0.5682241279335917
---
Left stick y: -0.3845637379070406
---
Left stick y: -0.32715842158268993
---
Left stick y: -0.22296823023163548
---
Left stick y: -0.12936796166875209
---
Left stick y: -0.02868739890743736
---
Left stick y: 0.21326334421826837
---
Left stick y: 0.3280434583574938
---
Left stick y: 0.42429883724478895
---
Left stick y: 0.5152439954832606
---
Left stick y: 0.5850093081453902
---
Left stick X: 0.47233497116000855
Left stick y: 0.7183446760460219
---
Left stick X: 0.46586504715109717
Left stick y: 0.7536545915097507
---
Left stick X: 0.4174932096316416
---
Left stick X: 0.37183751945554977
Left stick y: 0.7086092715231788
---
Left stick X: 0.2786339915158544
Left stick y: 0.5267189550462356
---
Left stick X: 0.026062807092501604
Left stick y: 0.0004272591326639607
---
Left stick X: 0.024201178014465773
Left stick y: -0.0011291848506118961
---
Left stick y: -0.0029602954191717277
---
Left stick y: -0.007446516312143315
---
Left stick y: -0.00924710837122715
---
Left stick y: -0.013245033112582781
---
Left stick X: -0.01120029297769097
Right stick x: 0.021698660237434005
Right stick x: 0.04031495101779229
---
Left stick X: -0.017273476363414413
Right stick x: 0.06338694418164617
---
Left stick X: -0.1295815912350841
Right stick x: 0.1369975890377514
---
Left stick X: -0.23114719077120274
Left stick y: -0.01834162419507431
Right stick x: 0.22214423047578355
---
Left stick X: -0.3532517471846675
Left stick y: -0.02264473403118992
Right stick x: 0.325907162694174
---
Left stick X: -0.4641560106204413
Left stick y: -0.02655110324411756
Right stick x: 0.42701498458815274
---
Left stick X: -0.6020996734519486
Right stick x: 0.5494247260963775
Right stick y: 0.03247169408246101
Right stick y: 0.007019257179479354
---
Left stick X: -0.859309671315653
Right stick x: 0.7844477675710319
Right stick y: -0.0684835352641377
---
Left stick X: -1.000030518509476
Right stick x: 0.869594409009064
Right stick y: -0.10623493148594623
---
Right stick x: 0.9937742240668965
Right stick y: -0.1554002502517777
---
Right stick x: 0.9953306680501725
Right stick y: -0.2054506057924131
---
Right stick y: -0.20676290169988099
---
Right stick y: -0.23880733664967804
---
Right stick y: -0.24628437147129734
---
Left stick X: -0.9988708151493881
---
Left stick X: -0.695303201391644
Left stick y: -0.03250221259193701
---
Left stick X: -0.01120029297769097
Left stick y: -0.03811761833552049
Right stick x: 0.8953215124973296
---
Left stick X: 0.00836207159642323
Left stick y: -0.002197332682271798
Right stick x: -0.0696432386242256
Right stick y: 0.016693624683370465
---
Left stick X: 0.01022370067445906
Right stick x: 0.003967406231879635
Right stick y: 0.021057771538438064
---
button down: 12
---
button up: 12
---
Left stick X: 0.022339548936429945
Left stick y: -0.005554368724631489
Right stick x: 0.021698660237434005
Right stick y: 0.03247169408246101
Joystick 0 disconnected
---
And output on Serial1 from Arduino sketch (printk)
Joystick device added: 0
BDN: 12
BUP: 12
BDN: 4
BUP: 4
BDN: 3
BUP: 3
BDN: 1
BUP: 1
Hat: 0 1
Hat: 0 0
1:-182 1:911
1:1070
1:1131
1:963
1:899
1:760
1:703
1:561
1:504
1:1143
Next up, hook up Robotis Dynamixel shield and maybe put on Turtle Burger and see if I can drive the wheels with the joystick input...
Mainly just having some fun.