Python library evdev on Raspberry Pi to use a Gamepad in your DIY projects (servomotor, games, robotic…)

The Python evdev library makes it possible to decode the codes sent by the input devices (keyboard, mouse, analog joystick, gamepad …) to exploit them in any project. We can for example use a gamepad in a game project or drive the servomotors of a robotic arm as the ROT2U 6DOF presented in this article. In this article we will learn how to decode the codes of an analog Gamepad compatible with Nintendo consoles that are very commonly found for less than $2.5 / €2 on AliExpress. The Python evdev library works on Linux systems. We can develop projects on Raspberry Pi 3 (or RPIZero) or a Linux PC (Ubuntu for example). On the other hand, as it exploits Linux kernel events, it is not available on Windows or macOS.

Install python and the evdev library on Raspbian for Raspberry Pi 3

Python 2.7 is installed by default on the Raspbian distribution. To make sure, open a Terminal and run the following command

Now check that the pip package for easily installing libraries is installed

If pip is not installed, run this command

Then python-dev and the evdev librarie

That’s it, it remains only to update the system to have the latest versions of the libraries

Identify the USB port to which the Gamepad or joystick is connected

Input devices (keyboard, mouse) as well as joysticks, gamepads and joysticks are input devices under Linux. Run the ls / dev / input command a first time

Now connect the Gamepad (or a joystick) and run the command again to identify the port. Here, we can recover the Gamepad actions on the event2.

The evdev library comes with a script that decodes the input device codes connected to the Linux PC or Raspberry Pi. The script asks you which device to listen to. Here is the # 2 on the device  /dev/input/event2 . Press the buttons and arrows to retrieve the codes. As you can see, the library can detect the press and release of a button.

Presentation (fast) of the Python evdev library

The evdev library for Linux therefore allows to recover the code of the keys or the buttons as well as the position of each axis of the analog joysticks. All orders are detailed here. Some gamepad behave like analog joysticks. For example, for this Nintendo gamepad clone, the horizontal arrows constitute the X axis. The two vertical arrows, the Y axis. The central position returns the value 127. Each end returns 0 or 255. For example, at the top (Up ), the Y axis returns 255. Down (Down), the Y axis returns 0.

The library can retrieve three pieces of information for each event:

  • His type. EV_KEY for keys or buttons. EV_ABS to retrieve the position of an analog axis
  • His code
  • Its value. 1 (press, press) or 0 (release, release) for a button. A position coded on 8bits. It depends on each manufacturer. Here, the gamepad returns 0, 127 (center) or 255

snes gamepad joystick

Recover codes from a USB SNES Gamepad

Let’s go to practice now. For the tutorial, I used a USB Gamepad. It is very easy to find this clone of the Gamepad Nintendo Super NES for less than $2.5 / 2€ on AliExpress for example. It may also be suitable for making a Recalbox arcade terminal (shown here) or Retropie.

Create a new script ( nano joy.py ) and paste the code below. Change the Gamepad address to line 5

Save with CTRL + X then Y or O (in French). Launch the python script joy.py. To stop the script, press CTRL + C on the keyboard. As you can see, we now get the code for each button. When a button is pressed, the val (value) goes to 1. When released, the value goes to 0. The same applies for the X and Y directions tested successively. Each time the arrow is released, the axis returns to its central position 127.

Decode all Gamepad actions

Now that we know all the codes associated with the gamepad buttons, it only remains to test each event to retrieve the actions in a game or drive the servomotors of a robotic arm. Create a new script and paste the code below by editing the event.

Now we can decode each action on the gamepad very easily. In the next tutorial, we will see how to use the library to drive the robotic arm presented previously.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

DIY Projects