Connecting Wiimote Controllers to the Raspberry Pi

PDF24    Send article as PDF   
In this Raspberry Pi Wiimote controllers guide, we will walk you through the process of setting up a Wiimote on your Raspberry Pi and show you how to setup both cwiid and wminput.
Utilizing Wiimote controllers on the Raspberry Pi can be painful to deal with due to its handling of the Bluetooth stack. While they run over standard Bluetooth, their actual functionality is not picked up as a conventional joystick device like Xbox controllers and PlayStation controllers.

Instead to support the full functionality of Wiimote controllers in a way that most applications can support we must utilize two pieces of software.

These being cwiid and wminput. One acts as a library that interprets the Wiimotes actions into joystick actions, the other being somewhat of a driver that creates an input device that applications can utilize easily.

Setting up a Wiimote on the Raspberry Pi

Sadly like most wireless controllers, Wiimotes do not work correctly with the Raspberry Pi straight out of the box. While Wiimote controllers use Bluetooth most of their functionality is not picked up correctly under standard interfaces.

To circumvent this, we can utilize two different pieces of software. We use these as they are widely accepted as the straightforward solution and is used by popular projects such as Retropie.

1. Now before we start on our first method of utilizing a Wiimote with the Raspberry Pi, we must first ensure that everything is entirely up to date. Do this by running the following commands on the Raspberry Pi.

2. Now that we have updated our Raspberry Pi lets go ahead and install some of the packages that we will need for this tutorial.

Python-Cwiid is the python interface to libcwiid. Libcwiid is a library that is designed to act as a bridge between the Wiimote controller and the Linux operating system and interprets the controller’s actions into something that can be understood programmatically.

Wminput is an essential part of this tutorial, it acts as an event/joystick/mouse driver for the Wiimote, allowing the operating system to understand it as one of its controller devices. Wminput helps enable it to be compatible with any program that has support for joystick devices.

Please note that if you are using a Raspberry Pi 2 or earlier, you may be required to install the Bluetooth packages, to do this we need to run the following command on our Raspberry Pi.

3. With the packages we require now installed to our Raspberry Pi we need to now set up the udev rules to allow non-root users access to our uinput devices. Uinput is the interface that wminput creates our Wiimote on.

We can create these Wiimote rules by running the following command.

4. Type in the following line into the file. This line will be read when the system boots up.

5. Now let’s save the file by pressing Ctrl + X, then Press Y and finally press Enter.

6. Let’s now restart the udev service, we do this so that it will load in the new rules file we wrote in the previous step. To do this, we just need to run the following command on our Raspberry Pi.

7. Now before we proceed too far, we should make sure that the Bluetooth service is up and running, to do this we can utilize the following command to check its status.

If you see “Active: active (running)” appear in the command line then you can proceed onto step 8 of this tutorial.

If you see anything else appear you can attempt to restart the bluetooth service by running the following command, in some cases this will start it back up but in others there may be actual errors stopping it from working correctly.

8. Now that we know that the bluetooth service is up and running let’s proceed to finish up our setup of wminput by creating a config file. This config file will associate all the wiimote buttons with a particular action/event in Linux.

Let’s create the config file we need by running the following command, for our guid we will by naming this “mywminput“.

9. Within this file enter the following lines, this gives a basic setup for the classic controller, Wiimote and nunchuck controller.

10. This next step is completely optional, if you don’t want any of the Wiimote LED’s to turn on you can skip to Step 11 of this Raspberry Pi Wiimote controllers tutorial.

Also if you want to learn up how to better configure your Wiimote configuration file you can check out these two helpful documents, the first is a list of wiimote buttons and axis that you can utilize in the configuration file, you can find this list of Wiimote button and axis on their Github.

The other is a list of possible buttons and axis’s that you can assign to your Wiimote button and axis as listed in the link above, you can find this list of buttons and axis on cwiids github page.

If you want to set which LED’s will be enabled when the connection to your Wiimote has been created then you can set them by using one of these following lines. Simply add whatever one you want to the bottom of the mywminput configuration file,

11. Let’s now save our wminput config file by pressing Ctrl + X then Y then finally pressing Enter.

12. Now we can proceed to grabbing all our Wiimote’s MAC addresses, we will need these to initiate the connection with our Raspberry Pi’s bluetooth device.

We can utilize the following command to search for any Bluetooth devices around.

Once you have started the tool press the 1 + 2 buttons at the same time on your Wiimote controller to start the discovery process. If you are unsure what buttons we mean then check out the diagram shown below.

13. You should see something like below appear in your command line, this will be the MAC address of your Wiimote controller followed by an identifier. Write down the MAC address of each of the controllers you intend on using.

14. Now let’s proceed with setting up a script to simplify connecting the controllers to the Raspberry Pi. We will write a script so we can get it to initialize at startup.

We will explain what the bash script does section by section to give you an idea of how everything works.

To begin let’s create a file in our home directory to store this bash script by running the following command on the Raspberry Pi.

15. Now add the following lines to our file. We will try to explain everything as we go along. Begin by entering these first three lines of code.

The top line acts as an identifier, so the operating system knows to use /bin/bash to run the file.

The second line tells the script to sleep for one second, we do this to make sure that the Bluetooth services are fully initiated before we try to use them.

The third line utilizes the hcitool to see whether there are any Bluetooth adapters available to utilize, this will stop the running of the wminput tool if there are no Bluetooth adapters to utilize.

The first line just checks to see whether our previous hcitool line has given us a result that we need to know that the Bluetooth adapter is available.

Our next line is one of the most critical parts of the file. This line initializes wminput to begin searching for your Wiimote controller.

You can have multiple of these lines just make sure you replace YOUR_MAC_ADDRESS in each occurrence with the MAC Address of a Wiimote controller that you want to connect.

The third line is a simple else statement that follows our final three lines of code.

These final three lines of code are incredibly simple. The first line just prints “Bluetooth adapter not present!” to the command line. This message occurs when the hcitool doesn’t see the Bluetooth adapter.

The second line just stops the script from executing any further.

Finally, the last line of our code just marks the end of our if, else statements.

16. Once you have finished writing in all the lines of code, you should end up with something like what we have shown below. Of course with your MAC address in place of YOUR_MAC_ADDRESS.

We also have included an example of what the file would look like if you are trying to connect multiple different controllers. You can see that in the code block over on the next page.

17. Once you are sure, the code is correct, save the file by pressing Ctrl + X then Y and then finally Enter.

18. Now with our script created let’s go ahead and make it executable by using chmod to give it 775permissions. This line will allow the owner and group to have full read/write/execute permissions, but only grants everyone else the ability to run the script.

Let’s do this by running the following line on our Raspberry Pi’s command line.

19. Now, let’s move onto the testing stage of our script. First, to do this, we must first enable the uinput module. We can do this by utilizing modprobe by running the following command on our Raspberry Pi.

20. With the uinput module now enabled we can finally run our script to test if it’s actually working. To do this just run the following command.

While the script is running press the 1 + 2 buttons on your Wiimote controllers, this will initialize the connection. They should flash briefly before either turning off entirely or changing to the settings you set in step 9 of this tutorial.

21. Now that we know the script is working let’s proceed onto the next steps, and that is making the script and the uinput module startup at boot so your controllers can connect at any time.

We will begin by editing the /etc/modules file to load up uinput at boot up. Run the following command to start editing the file.

22. To this file add the following line to the bottom of the file. This line will make sure uinput is loaded at boot up time.

23. You can now save the file by pressing Ctrl + X then Y and then finally pressing Enter.

24. Finally, let’s edit the rc.local file, we will utilize this to execute our script at startup. Let’s begin by editing the file by utilizing the following command.

25. Now in this file add the following line.


Add Above

26. You can now save the file by pressing Ctrl + X then Y and then finally pressing Enter.

27. We must now restart the Raspberry Pi to test to see whether our script is being executed correctly. To do this run the following command on your Raspberry Pi.

28. Now if you press the 1 + 2 buttons on your Wiimote controller, it should make a successful connection automatically. If it does, you have completed the process os syncing your Wiimote to the Raspberry Pi.

29. Now we should test the controller to make sure it is, in fact, working correctly we make use of the jstest tool. To begin using this lets first install the joystick package, while this is usually included with the full installation of Raspbian, it’s not included in the lite edition.

Install it on your Raspberry Pi by running the following command on it.

30. Now lets test our new joystick device by running the following command, when in the jstest tool you should see values change upon doing certain actions on your Wiimote.

There are plenty of reasons why you might want to have set these controllers up on the Raspberry Pi. One of the biggest reasons is so that you can do some gaming on the Raspberry Pi, but there are plenty of regular projects you can use them as a controller.

Hopefully, by the end of this Raspberry Pi Wiimote guide, you have now got your Raspberry Pi up and running with your Wiimotes successfully connecting to it. If you have any feedback or need assistance with this tutorial feel free to drop a comment on our forums.

Previous Post
VLC media player

Raspberry Pi: Compile VLC Player with Hardware Acceleration

Thanks to a post on the Raspberry Pi Foundation’s Message Board, where people have detailed the instructions on how to easily compile VLC Media Player with Hardware Acceleration. For this project, ... Read more

Next Post

Using PlayStation3 Controllers with the Raspberry Pi

If you have ever tried to utilize a PlayStation controller with the Raspberry Pi, you may have found it’s not very simple to do. The Raspberry Pi PlayStation controller guide ... Read more

Short URL:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!