Using Legos to turn a Raspberry Pi into a mobile device

Xbox 360 Controller

The key that you press on an Xbox 360 controller determines which of the methods found on the server application you will invoke. At this point, you will need to connect the Xbox controller with the client PC and install the correct driver. Then, you should add the Jinput library to the client application [9].

The next step is to use the library to test whether a controller is connected to the client at all. If so, you should run a loop that tests whether the state of the control keys has changed. If this is the case, start a function call. You can decide for yourself what happens when each key is pressed.

For example, the code in Listing 6 defines the effect of pressing the A button of the controller; the state of the appropriate GPIO pins on the Rasp Pi is set to HIGH/LOW or 1/0, as is required by the method motor1Forward(). When the button is released, the routine will set the appropriate GPIO pins to LOW or 0 as prescribed by the motor1Stop() method.

Listing 6

Pressing the A Button

while (isRunning) {
 for (Component c : gamepad.getComponents()) {
  if (c.getIdentifier() == button0) {
   if (!oldValue == c.getPollData())) {
    if (oldValue == 0) {
     oldValue = 1;
    } else {
     oldValue = 0;

Conclusion and Outlook

The costs of implementing the do-it-yourself project presented in this article will run about EUR 200 if you buy all new components. To make the project easier, you could use a BrickPi [10] instead of building the electronics yourself [11]. The BrickPi makes it easy to connect the motor and sensor components from Lego Mindstorms to the Rasp Pi. However, the convenience you gain with this approach raises the price of the project considerably.

You can add as many motor controller chips as you want, provided you have unassigned GPIO pins available. You then define and implement the appropriate methods as part of the interface for the RMI server to control the motors. Additionally, you can add a test for the desired button press, including method invocation. This technique allows you to add additional functions to the car. For example, you could add a motorized version of the grappler from Lego set 42006.


  1. Lego Mindstorms:
  2. Lego Power Functions:
  3. Power Functions internals:
  4. "Tricks zum Programmieren der GPIO-Schnittstelle" by Richard Ryniker, Raspberry Pi GEEK, Issue 5: (in German)
  5. 5V USB Battery:
  6. "First Steps" by Paul C. Brown, Joseph Guarino, and Joe Casad, Raspberry Pi Geek, Special Edition 17, pg.  8:
  7. WiFi Adapter:
  8. Pi4J Library:
  9. WiringPi Library:
  10. Jinput Library:
  11. BrickPi:
  12. "BrickPi" by Marko Dragicevic, Raspberry Pi Geek, issue 3:

The Author

Martin Filusch is currently working on a Master's degree in Computer Science and Distributed and Mobile Applications at the Osnabruck University of Applied Sciences. He has a Bachelor's degree in Computer Science and Media Technology, also from HS Osnabruck.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content