Some while ago I started using a Raspberry Pi to control my Lego. It all started when we wanted to figure out if we could run standard Java on the Raspberry Pi. Instead of writing “Hello World” – we wanted to do something cooler. We decided to use Java and later Scala with Akka on the Raspberry Pi to control Lego trains. In the end we could control the speed of the trains, play train sounds, get a live video feed from the train and retrieve the location of the train based on a RFID reader and some RFID cards. Later we expanded it even further by automating track switches, ferris wheels, Lego technic cars and planes.


Picture 1: Lego trains and Ferris wheel controlled by Raspberry Pi’s and Spring Boot

I noticed there was quite some interest in the setup so I started giving presentations about it at conferences. By now I don’t even know how many times I presented the session. You can watch the Devoxx UK video if you want to learn more.

One of the questions I often get from the audience was whether or not the code is available as open source. Unfortunately quite some parts of the code were written under time pressure and I didn’t dare to release it to the public. Another challenge was the fact that you need to execute quite a few commands in Raspbian to enable infrared and other functionalities.

In the end I decided to make the software available and easy to install for everyone. Recently I made a start and published parts of my work to two GitHub repositories. One repository has a Spring Boot application that offers REST endpoints. The other repository contains the scripts to enable the infrared and camera. In the rest of this post I will explain how you can convert your Raspberry Pi to a device that can control your Lego. With “Lego” I mean any Lego that has a Lego infrared receiver; so you can control trains, cars, planes etcetera.


Before you start you need to get some ingredients. To make it easy for you I made a shopping list. It will cost you around 40 euro if you haven’t got any of the ingredients.

Minimum ingredients:

  • Raspberry Pi Zero or Raspberry Pi 3
  • Keyes 38KHz IR Infrared Transmitter Module for Arduino
  • Three Female-Female Header Jumper cables
  • Mini power bank with USB connection for instance from the Anker brand
  • Micro USB male to USB male cable (for the power bank)
  • Micro SD card

If you have the Raspberry Pi Zero, you need the following extra ingredients:

  • EDUP Ultra-Mini Nano USB 2.0 802.11n 150Mbps Wifi/WLAN Wireless Network Adapter
  • Micro USB male to USB female cable (for the USB WIFI adapter)

Optionally you can also buy an official Raspberry Pi Camera Module and connect it to your Raspberry Pi. Bear in mind that if you have a Raspberry Pi Zero you need a special adapter.


Replacing the standard Lego controller

Lego offers two standard Lego infrared remotes, you can see one of them in picture 2. These remotes have some disadvantages. The reach is quite poor, I could no longer control my train when I drove it from my living room to the kitchen. Next to that there are only 8 channels available which basically means that the number of devices you can control is limited. The solution is quite simple, we replace the standard Lego infrared remotes.

Lego infrared remote

Picture 2: Standard Lego infrared remote control

The replacement for the standard infrared remote is a Keyes infrared transmitter as shown in picture 3. The pins from left to right are GPIO17, 5V and GND. Connect those pins to the respective Raspberry Pi GPIO pins with jumper cables as shown in picture 4.


Picture 3: Keyes infrared transmitter


Picture 4: Infrared transmitter connected to Raspberry Pi Zero

The best solution to enable our Lego devices to move freely is to use an USB WIFI dongle and a small powerbank. Connect them as shown in picture 5. With our power bank we can power a Raspberry Pi for quite some hours.


Picture 5: Raspberry Pi Zero with power bank and WIFI adapter

Raspberry Pi camera

Optionally you can connect an official Raspberry Pi camera as shown in picture 6. That camera enables you to stream full HD video for instance from the top of the train. Actually the RPi Cam Web Interface application used in my scripts does not stream video, it refreshes pictures. However you probably won’t notice it as it goes really quick. When you want to stream video from a Raspberry Pi you have two options: a delay of a few seconds, or a solution that requires specific software. As I didn’t wanted to have around 5 seconds delay or a solution that only worked with VLC, I ended up with this solution. I can highly recommend the RPi Cam Web Interface application if you want to do something with a Raspberry Pi camera.


Picture 6: Raspberry Pi Zero with camera


Configuring the Lego components

After assembling the Raspberry Pi and the other hardware it’s time to setup our electronic Lego components. The Lego infrared receiver is shown in picture 7. The orange bar indicates which channel the infrared receiver is listening on. If you have only one receiver it’s best to select the channel at the top, which is channel 0. This enables you to use a simpler way of calling our application. In picture 8 you can see how you can select another channel, in this case channel 1.



Picture 7: Channel 0 selected on the Lego infrared receiver


Picture 8: Channel 1 selected on the Lego infrared receiver

The back of the infrared receiver in picture 9 has two connectors, these are called outputs. The connector to the left with the engine connected to it is output 1. The connector to the right with the LEDs is output 0. If you only have 1 channel and 1 output, use output 1 as it enables you to use a simpler variant of the application.


Picture 9: Lego infrered receiver with an engine and lights

Don’t forget to connect a battery box as shown in picture 10. Lego has a few different types of battery boxes, it doesn’t really matter which one you use.


Picture 9: Lego infrared receiver connected to the battery box

WIFI setup

At some point you have to configure your wireless settings in Raspbian. I’ve opted to install all the software via a wired connection and configure wireless after everything is ready. But you can also opt to configure the wireless now and don’t use a wired connection. To configure wireless open the wpa_supplicant file on the Raspberry Pi (sudo nano /etc/wpa_supplicant/wpa_supplicant.conf) and add the following section. Don’t forget to change it to match your routers configuration.





Software installation

Install Raspian on a SD Card and use SSH to connect to the Raspberry Pi. The default username is ‘pi’, the default password is ‘raspberry’. If you don’t know the IP address of the Raspberry Pi then you can use something like ‘Advanced IP Scanner’ to figure it out.

After logging in to the Raspberry Pi you can install all the software with the command below. After installing the software it’s best to reboot the system. After a reboot, the Spring Boot application will automatically start.

sudo apt-get update && sudo apt-get install -y git && git clone && sh LCCInstallScript/

Controlling your Lego

If you only have one infrared receiver (configured at channel 0) and one output (configured at output 1) than you can use the Spring Boot application in a simplified form. From any computer that can access the Raspberry Pi you execute the call http://[IP]:8080/intensity/[x],for instance in the browser, where X can be any number from -7 up to and including 7. Don’t forget to replace ‘[IP]’ with your IP address. http://[IP]:8080/intensity/3. will make an engine go forward or make a led shine. http://[IP]:8080/intensity/0 will stop the engine or the light.

If you have multiple infrared receivers and or multiple outputs than you need to specify the channel and the output in the call. The call then has the following form http://[IP]:8080/intensity/[x]?channel=[y]&output=[z] where x is the intensity again, y is the channel and z is the output. So for instance http://[IP]:8080/intensity/-2?channel=3&output=0 will make an engine go backward with speed 2 or make a led shine if the device is connected to channel 3 and output 0.

It’s possible to watch the camera feed within a browser by entering the following address: http://[IP]. Or at the default location http://[IP]/html in case the scripts are modified.

Example use cases

Picture 11 shows an example use case of our Raspberry Pi Lego controller with a Lego train. Picture 12 shows the same train, but now with the optional camera.


Picture 11: Lego train


Picture 12: Lego train with the optional camera

You can automate more or less any Lego, especially Technic Lego. This is easy to automate as it often already contains electronic parts like engines, lights, infrared receiver and a battery box. I automated a Ferris wheel, a racecar, an airplane and a wheel loader. The wheel loader in picture 13 already contained two infrared receivers, four engines and a battery box. I just added a Raspberry Pi Zero with the infrared transmitter inside the wheel loader and I could make it move around and make it pickup items.


Picture 13: Lego wheel loader. Four engines controlled by the Raspberry Pi Zero.


With these instructions together with the script and the Spring Boot application it’s relatively easy to control your own Lego. You can use the setup to code/play together with kids, or just by yourself ;). It’s still work in progress, so if you have any questions or additions please contact me for instance on Twitter: @johanjanssen42. One of the features I’m still working on is the ability to play sound through a speaker. Another feature is the integration with Scratch which will make it easier for kids to play with it. Stay tuned for an article about those topics.

Control your Lego with Java and a Raspberry Pi

| Mind the Geek| 7,829 views | 4 Comments
About The Author
- Johan is a trainer/architect at Info Support. Johan is frequently speaking at conferences such as Devoxx, Voxxed, JavaOne, J-Fall, JFokus, JavaLand, ScalaDays.


  • Michael Rasmussen

    I watched your video. I’m a lego nut, java programmer, and recent raspberry pi convert. I’ll have to check out the code and see how it all works. I realize this is entirely a departure from your IR controls, but there is a BLE motor controller that a company in the czech republic makes. I purchased one last year and it is quite cool. I also have some plans to get a ble module for a pi and do much of what you did, but I might have to rethink using IR if you’ve already done all the lifting. As an aside there are a few designs out there for switches that are driven by PF motors, mainly this one

    I’ll let you know if/when I get some of this working on my own.

    • Johan Janssen

      Sorry for the late reply. I actually received the new Sbrick model recently. It looks really interesting and it has a nice application which you can run on your smartphone. However I still need to try the Sbrick with my Lego, so I don’t know if it works as well as advertised :).

      Interesting link about the switches. However most solutions require that you use equipment to remove parts of the switch so that it runs more smoothly. I didn’t want to modify the Lego switches, so I opted for another solution with a Raspberry Pi and servos you can see some more information here:

      Let me know if you’ve build some cool stuff :).

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>