To check the battery status of a PS4 (DualShock 4) controller connected via USB on a Linux PC, you can use tools that interact with the controller's input device or the system's power supply information. Below are the steps to achieve this:
1. Install Required Tools
Ensure you have the necessary tools installed to interact with the controller. You may need jstest-gtk or evtest for joystick input and upower for battery information.
-
Install jstest-gtk (optional, for testing joystick):
sudo apt install jstest-gtk # On Debian/Ubuntu-based systems
sudo dnf install jstest-gtk # On Fedora-based systems
-
Install evtest (for event debugging):
sudo apt install evtest # On Debian/Ubuntu-based systems
sudo dnf install evtest # On Fedora-based systems
-
Install upower (for battery status):
sudo apt install upower # On Debian/Ubuntu-based systems
sudo dnf install upower # On Fedora-based systems
2. Identify the Controller
When you connect the PS4 controller via USB, it is typically recognized as a joystick or input device. You can check the connected devices using the following commands:
-
List input devices:
ls /dev/input/
Look for entries like js0 (joystick) or eventX (event device).
-
Use evtest to identify the controller:
sudo evtest
This will list all input devices. Look for something like "Sony Interactive Entertainment Wireless Controller" or similar. Note the device path (e.g., /dev/input/eventX).
3. Check Battery Status Using upower
The upower tool can display battery information for devices, including game controllers, if supported by the kernel.
-
List all power devices:
upower -e
This will list all power devices. Look for an entry related to your PS4 controller, such as /org/freedesktop/UPower/devices/battery_sony_controller_battery_xx_xx_xx_xx_xx_xx.
-
Check the battery status of the controller:
Replace sony_controller_battery_xx_xx_xx_xx_xx_xx with the actual device name from the previous step:
upower -i /org/freedesktop/UPower/devices/battery_sony_controller_battery_xx_xx_xx_xx_xx_xx
This will display detailed information, including the battery percentage, state (charging/discharging), and more.
Example output:
native-path: /sys/devices/.../sony_controller_battery_xx_xx_xx_xx_xx_xx
vendor: Sony Interactive Entertainment
model: Wireless Controller
power supply: no
updated: Wed 01 Jan 2023 12:00:00 PM UTC (10 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: discharging
energy: 5.6 Wh
energy-full: 7.8 Wh
energy-full-design: 7.8 Wh
energy-rate: 0.5 W
voltage: 3.7 V
percentage: 72%
capacity: 100%
4. Alternative: Check Battery via /sys/class/power_supply
If upower does not work or is not installed, you can directly read the battery information from the kernel's sysfs interface.
-
Navigate to the power supply directory:
ls /sys/class/power_supply/
Look for an entry like sony_controller_battery_xx_xx_xx_xx_xx_xx or similar.
-
Check the battery capacity (percentage):
cat /sys/class/power_supply/sony_controller_battery_xx_xx_xx_xx_xx_xx/capacity
This will output a number (e.g., 72), which represents the battery percentage.
-
Check the battery status (charging/discharging):
cat /sys/class/power_supply/sony_controller_battery_xx_xx_xx_xx_xx_xx/status
This will output something like Discharging or Charging.
5. Automate the Process (Optional)
If you want a quick way to check the battery status regularly, you can create a simple script. For example:
#!/bin/bash
# Replace with your controller's actual battery device name
DEVICE="sony_controller_battery_xx_xx_xx_xx_xx_xx"
if [ -d "/sys/class/power_supply/$DEVICE" ]; then
PERCENTAGE=$(cat /sys/class/power_supply/$DEVICE/capacity)
STATUS=$(cat /sys/class/power_supply/$DEVICE/status)
echo "PS4 Controller Battery: $PERCENTAGE% ($STATUS)"
else
echo "PS4 Controller not found or not connected."
fi
Save this script as check_ps4_battery.sh, make it executable, and run it:
chmod +x check_ps4_battery.sh
./check_ps4_battery.sh
Notes
- The PS4 controller must be connected via USB or Bluetooth for the battery information to be available. However, USB is more reliable for battery reporting on Linux.
- If the battery information is not available, ensure your kernel supports the controller's battery reporting. You may need a recent kernel version or additional drivers.
- If you're using Bluetooth, ensure the controller is properly paired and recognized by your system.
Let me know if you encounter any issues!