How to use an Xbox or PS4 controller with a Raspberry Pi

Setting up RetroPie

Video games in the 1980s were quite different from the latest crop of frag -’em- till- you’re- dead point-and-shoot games. They were tastefully crafted 8- bit graphical masterpieces, with an intense storylines and gameplay that kept you engrossed for hours.

Setting up RetroPie

Video games in the 1980s were quite different from the latest crop of frag -’em- till- you’re- dead point-and-shoot games. They were tastefully crafted 8- bit graphical masterpieces, with an intense storylines and gameplay that kept you engrossed for hours.

If reading this makes you feel nostalgic, you can emulate the golden era of gaming consoles on your modern hardware and escape back to that golden era.

The new quad-core Raspberry Pi 3 has enough numbercrunching power to recreate the video game consoles of yesteryear virtually. Most of the software that creates the defunct platforms is available as open source software, which you can install on top of a Raspbian distribution (distro).

Check out our collection of fantastic Raspberry Pi projects

However, the easiest way to start playing vintage games on the Raspberry Pi is to install the purpose-made RetroPie distro, which packs a bundle of emulators.

We have an indepth guide to turning your Raspberry Pi into a retro games console, which will go through all the steps, but we will also quickly show you how to get RetroPie working here, before attaching a games console controller to your Raspberry Pi.

You can manually install RetroPie on top of an existing Raspbian distro but it’s more convenient to use the pre-baked image. In addition to Raspberry Pi 3, the distro works with the older models as well, so make sure you grab the correct image.

5 of the most popular Raspberry Pi distros

You need to transfer this image to at least a 4GB card, either using the dd command in Linux, such as

$ dd if=retropie-rpi2.img of=/dev/sdd

You also need a USB keyboard and mouse for some initial setup that you can’t do remotely via SSH. We’ll also hook up a compatible Wi-Fi adaptor to the Raspberry Pi, which won’t work straight out of the box, but we’ll get to that later.

How to get super-fast Wi-Fi in every room

Most important of all, make sure you grab some gaming controllers to enjoy the games to the hilt, and RetroPie can work with various controllers, from cheap no-names ones to PS4 and Xbox One controllers.

Once you’ve prepared the memory card with the RetroPie image, insert it into the Raspberry Pi, connect the controller, the Wi-Fi adaptor, the speakers and the USB input devices, hook it up to your HDMI monitor, and power it up.

The Raspberry Pi boots directly into Emulation Station, which is the graphical interface it uses to enable you to switch between emulators. The interface asks you to configure the controller. However, before we do that, we have to tweak a couple of settings.

Press the F4 key on the keyboard to exit the Emulation Station, then head to XTerminal.

Basic setup

The first order of business is to expand the image to take over the entire card. In order to do this, bring up Raspbian’s configuration utility with

$ sudo raspi-config

and select the first option to expand the filesystem. Once that’s done, head to the second option to change the default password for the pi user.

Next up, head to the Advanced Options and select the SSH option to enable remote access. To ensure you use the maximum memory for gaming, head to the Memory Split option. If you’re using a Raspberry Pi 3 or Raspberry Pi 2, allocate 512 to the GPU.

Users of the older B+ model should earmark 256. Finally, scroll down to the Overclock option, where users of the Raspberry Pi 2 should select the Pi2 option. Once you’ve made all the changes, head back to the main menu and select ‘Finish’ to restart the Raspberry Pi and save changes.

When you’re back up again, press F4 once more to exit out of Emulation Station. We’ll now get the Wi-Fi adaptor to work. Open the configuration file with:

$ sudo nano /etc/network/interfaces

and then change its contents to resemble the following:

Make sure you replace the text in the wpa-ssid line with the SSID and password for your Wi-Fi network. Press Ctrl+X to save the file and exit the text editor. Now reboot the Raspberry Pi with sudo reboot . Once it comes back up, your Wi-Fi adaptor connects you to your router.

From this point on, you can do the configuration remotely from another computer. Exit Emulation Station yet again and make a note of the IP address RetroPie has been assigned by your router.

Assuming it’s 192.168.3.111, you can now log in to it from another computer with sudo ssh pi@192.168.3.111 .

Irrespective of how you’re accessing the Raspberry Pi, the next order of business is to tweak some RetroPie-related settings. Change into the RetroPie-Setup directory with

$ cd /RetroPie-Setup

and execute the configuration script with

$ sudo ./retropie_setup.sh

The script fetches any required packages that are missing from your installation. When it’s ready, the script displays an Ncurses-based menu. First up, scroll down to the second-to-last option, which updates the RetroPie-Setup script itself.

Once it’s done, re-launch the script and scroll down to the third option, labelled Setup/Configuration. In here, scroll down and select ‘323’, which makes the necessary changes to display the RetroPie configuration menu in Emulation Station.

This helps you make changes to the distro without heading back to the command line. Now, depending on your audio gear and how it’s connected to the Raspberry Pi, you might need to handhold RetroPie before it can send audio output correctly.

Everything you need to know about Linux Commands

Select option ‘301’ to configure the audio settings. If the default auto option isn’t playing any sound, scroll down and select the output to which your speakers are connected. The menu also gives you the option to bring up the mixer to adjust the volume.

Configure the controllers

Now reboot the distribution one last time and this time continue with Emulation Station. If you’ve connected your controller, the distro will pick it up.

Press and hold any key on the controller to help the distro correctly identify the controller. You will then be asked to map the keys on the controller. Be aware that this basic mapping is only for navigating around the graphical interface and helping you switch between the emulated system and selecting a game.

Xbox Elite Wireless Controller review

Once you’ve set up the controller, you’re dropped into the main menu of the Emulation Station interface. Now, to set up the controller for gaming, head to the RetroPie menu in Emulation Station and select the ‘Configure RetroArch Keyboard/Joystick’ option.

Use the keyboard and select the first option, labelled ‘Configure Joystick/Controller’. Then follow the on-screen prompts to set up your controller. If your controller doesn’t have the buttons you’re being asked for, just wait for a few seconds and the setup will move on to the next button.

Controller drivers

If you use an Xbox 360 or a PS3 controller, you first have to install their drivers before RetroPie can pick them up. In earlier versions, this involved some hacking on the command line.

However, in the latest version of the distro, it’s a very simple and straightforward affair. Head to the RetroPie-Setup option in the RetroPie menu inside Emulation Station.

This brings you to the Ncurses menu of the RetroPie-Setup script we were in earlier. Use the keyboard to select the third option to configure the distro.

Scroll through the list and select the relevant option to install the driver for your controller – ‘318’ to install the PS3 driver and number ‘332’ to install the driver for the Xbox 360.

The Xbox360 script downloads the xboxdrv driver and edits the /etc/rc.local file to start the driver on boot. The script adds entries for wired 360 controllers. If you are using wireless controllers, open the /etc/rc.local file in a text editor, hunt for the lines that begin with xboxdrv and replace the –id option with –wid .

If you are using PS3 controllers, once you’ve installed the drivers using the script as described earlier, you’re prompted to plug in the Bluetooth adaptor for the controllers.

Even after you do so, RetroPie will fail to detect your controllers. This is to be expected, according to the developers. Exit the script and out of Emulation Station. Once you’re back on the command line, switch to the /opt/retropie/supplementary/ps3controller/ directory and type

$ sudo ./sixpair

This nifty little utility should detect the Bluetooth adaptor and make it known to RetroPie.

How to get Wi-Fi and Bluetooth working on Raspberry Pi 3

Now reboot the Raspberry Pi and, once it’s back up, change to the /dev/input directory and list its contents with ls . If your controller has been detected, it’s listed as js0. You can test the controller by using:

$ jstest /dev/input/js0

which brings up the jstest program designed to test the features of a controller. Now head back to the RetroPie menu in Emulation Station and use the ‘Configure RetroArch Keyboard/Joystick’ option to set up your controller.

And that’s it – your controllers are now all set up and ready to go. You can do this with all your controllers and RetroPie saves the configuration and automatically loads it whenever you plug the controller in.

You can now scroll through Emulation Station and play the pre-installed games with your controllers.

Use a virtual gamepad

Don’t sweat if you don’t have a gaming controller – you can create and use a virtual one from within your phone or tablet instead. To create the virtual gamepad, head to the XTerminal and enter the following commands to install the required components:

$ sudo apt-get update

$ sudo apt-get upgrade

$ wget http://node-arm.herokuapp.com/node_latest_armhf.deb

$ sudo dpkg -i node_latest_armhf.deb

Once you have the components, switch to the root user with the su command. You’re prompted for the root user’s password (raspberry). Once authenticated, enter

# git clone https://github.com/miroof/nodevirtual-gamepads

# cd node-virtual-gamepads

# npm install

# npm install pm2 -g

The above steps take a little time to complete. Once they’ve finished, you can launch the controller and enable it to start up automatically at boot:

# pm2 start main.js

# pm2 startup

# sudo pm2 save

Now grab your phone or tablet, open the web browser (the developers recommend Google Chrome for best results) and enter the IP address of the Raspberry Pi in the address bar. You should now see a virtual controller on the page.

Note that you need to configure your controller with and RetroArch just as you would with a physical controller. The game controller web application also provides haptic feedback – if you find it irritating, you can deactivate it by taking your device off vibration.

Enjoyed this article? Expand your knowledge of Linux, get more from your code, and discover the latest open source developments inside Linux Format.Read our sampler today and take advantage of the offer inside.

Leave a Reply

Your email address will not be published. Required fields are marked *