In this video, I build a joystick mouse controller using an STM32 NUCLEO-G474RE board and a standard Arduino joystick module.
The joystick is powered from 3.3 V and connected to STM32 analog inputs. The STM32 reads the X and Y joystick positions using ADC2, reads the joystick push button using a GPIO input with internal pull-up, and sends the values to a Linux computer through UART over the ST-LINK Virtual COM Port.
On the Linux side, a Python script reads serial data from /dev/ttyACM0 and converts the joystick movement into real mouse cursor movement. The joystick button is also used as a left mouse click.
This project connects real electronics, STM32 firmware, UART communication, Linux serial ports, and Python automation into one complete embedded systems experiment.
Wiring used in this video:
Joystick GND to STM32 GND
Joystick 5V pin to STM32 3V3
Joystick VRx to PA0 / ADC2_IN1
Joystick VRy to PA1 / ADC2_IN2
Joystick SW to PB0 / GPIO input with pull-up
Important:
Even though the joystick module pin is labeled 5V, I powered it from 3.3 V because STM32 ADC inputs must stay within the 0 V to 3.3 V range.
STM32CubeMX configuration:
PA0 as ADC2_IN1
PA1 as ADC2_IN2
PB0 as GPIO Input with Pull-up
ADC2 with two regular conversions
Rank 1 uses Channel 1
Rank 2 uses Channel 2
Sampling time is 47.5 cycles
Serial output format:
X = 1980, Y = 1977, SW = 1
Linux serial test:
picocom -b 115200 /dev/ttyACM0
Python packages:
pip3 install pyserial pynput
Note for Linux users:
If mouse movement does not work, check your desktop session:
echo $XDG_SESSION_TYPE
In my case, pynput did not move the mouse under Wayland. Switching to an X11 session fixed the issue.
What you will learn:
How joystick modules work electrically
How to power a 5-pin joystick safely from 3.3 V
How to read analog voltage with STM32 ADC
How to configure ADC channels in STM32CubeMX
How to read a button using GPIO input with pull-up
How to print STM32 data over UART
How to read /dev/ttyACM0 on Linux
How to control the mouse cursor with Python
Why Wayland can block direct mouse control
Chapters:
00:00 Intro: STM32 joystick mouse controller
00:05 Electrical schematic and 3.3 V power
00:22 Real hardware wiring
00:56 Serial data from STM32
01:17 ADC values and joystick center
01:46 Python converts serial data to mouse movement
02:08 STM32CubeMX configuration
02:46 ADC2 ranks and sampling time
03:10 GPIO pull-up for joystick button
03:42 STM32 HAL code in CubeIDE
04:46 UART output through ST-LINK Virtual COM Port
05:34 Python script for mouse control
06:14 Dead zone, speed, and center calibration
06:54 Mouse click from joystick button
07:26 Wayland vs X11 note
07:42 Final embedded-to-PC control chain
This is part of my practical embedded systems learning series, where I build real circuits, write firmware, measure signals, and connect microcontrollers to useful real-world applications.
#STM32 #EmbeddedSystems #ADC #UART #Python