In this project, we will show you how to control the LEGO® Technic Liebherr Crawler Crane (42146) with the Xbox Controller.

This gives you precise control over each of the crane’s functions without having to use a phone or tablet. You can drive it around with the left joystick and control the functions with the buttons and triggers.

This crane has two hubs, so we’ll also show you how to connect them via Bluetooth.

Driving the LEGO® Technic Liebherr Crawler Crane (42146) with the Xbox Controller.
Driving the LEGO® Technic Liebherr Crawler Crane (42146) with the Xbox Controller.

Requirements

To follow this project, you will need the following:

See this overview for all compatible models.

Understanding the code

This project has two programs, one for each hub. The hub in the base connects to the Xbox Controller and drives the vehicle. It also calculates the required speeds of the crane functions based on your input, and broadcasts these to the other hub in the crane.

The crane hub receives these messages and controls the crane functions accordingly. While the order of installation doesn’t matter, it’s practical to download and run the crane program first. Because it simply starts and stops the crane functions based on the incoming signals, you can customize all the actions in the program for the hub in the base instead.

When sending numbers, communication works best if you send small numbers. So we just send the percentage values from the joysticks, and multiply them by a factor in the crane program to get the desired speed.

The program for the hub in the crane receives three values and sets the crane motor speed functions accordingly.
The program for the hub in the crane receives three values and sets the crane motor speed functions accordingly.
The program for the hub in the base receives the Xbox Controller input, drives and turns the base, and broadcasts the required speeds for the crane functions to the other hub.
The program for the hub in the base receives the Xbox Controller input, drives and turns the base, and broadcasts the required speeds for the crane functions to the other hub.

Running the Pybricks program

This project uses Pybricks on your LEGO hub. Pybricks makes your creations come alive and helps you unlock the full potential of your LEGO Technic, City, MINDSTORMS, BOOST, or Spike sets.

If you haven’t already, install Pybricks on your hub as shown below, or check out our getting started guide for more details. You can go back to the LEGO firmware and apps at any time.

Install the Pybricks firmware.
Tools
Install

Now import the program you downloaded earlier, as shown below. Click to connect your hub and ▶ to start!

Import a Pybricks Code project.
files
import
open
connect
run

You can run imported block programs even if you’re not signed up. This is a great way to try out Pybricks and see how it works.

In this project, you have two hubs. Installation works the same for both. During installation, you can give each hub a unique name such as Base or Crane, which makes it easier to tell them apart when you connect later.

Pairing with the Xbox Controller

The very first time, you’ll need to pair the hub with your controller:

  1. Turn the controller on.
  2. Press and hold the pairing button on the back.
  3. Release after a few seconds. The controller light will start flashing more rapidly. This is pairing mode.
  4. Then start your program. Either using the Pybricks app, or using the green button on the hub if you’ve already loaded the program earlier.

The hub will start looking for your controller when the program starts. When the connection is successful, the controller light will stop flashing and stay on for as long as the program is running.

You can see this in the video below.

Connecting without pairing

The next time you use it, pairing is not required. Just turn the controller on. The hub will connect to it automatically when your program runs.

The Xbox controller remembers only one device. If you use the controller with another LEGO hub or your Xbox console, and then with this hub again, you’ll need to pair them again as above.

Updating the Xbox Controller

If you often use the Xbox Controller with your console, it is probably already up to date. If you have not used it for a while or if you bought one recently, you may need to update it.

To update the controller without a console, you can use the Xbox Accessories app on a Windows computer. Connect the controller via USB to the computer and follow the instructions in the app to click on “Update now”. If it is already at firmware version 5.17 or later, you don’t need to do anything.

Technic Hub limitations

Once you start your program, the Technic Hub will disconnect from your computer to free up the connection for the Xbox Controller. This means you can’t use the Pybricks app to stop the program or change the code while the Xbox Controller is connected.

To change your program, stop the program by pressing the green button on the hub. Then you can connect to the hub with the Pybricks app again, and change your program as needed.

The crane in action

When you’re ready, you can operate the crane as shown below!

Communication between the two hubs works best if you disconnect them from your computer.

Running it as a Python program

You can also run this project as a Python (MicroPython) program. The following code was generated from the block program above. To run it, create a new empty Python program in Pybricks and copy the code into it.

from pybricks.hubs import TechnicHub
from pybricks.parameters import Axis, Direction, Port, Stop
from pybricks.pupdevices import Motor
from pybricks.tools import wait

# Set up all devices.
hub = TechnicHub(top_side=Axis.Z, front_side=Axis.X, broadcast_channel=0, observe_channels=[5])
boom = Motor(Port.A, Direction.CLOCKWISE)
jib = Motor(Port.D, Direction.CLOCKWISE)
hoist = Motor(Port.B, Direction.CLOCKWISE)

# Initialize variables.
boom_speed = 0
jib_speed = 0
hoist_speed = 0


# The main program starts here.
while True:
    # Get and unpack data sent from the hub in the base.
    # This unpack block sets the variables to 0 if there is no data.
    boom_speed, jib_speed, hoist_speed = hub.ble.observe(5) or [0] * 3
    # Operate the boom.
    if boom_speed:
        boom.run(boom_speed * 8)
    else:
        boom.stop()
    # Operate the jib.
    if jib_speed:
        jib.run(jib_speed * 8)
    else:
        jib.stop()
    # Operate the hoist.
    if hoist_speed:
        hoist.run(hoist_speed * 10)
    else:
        hoist.stop()
    wait(50)
Program for the crane hub.
from pybricks.hubs import TechnicHub
from pybricks.iodevices import XboxController
from pybricks.parameters import Axis, Button, Direction, Port, Stop
from pybricks.pupdevices import Motor
from pybricks.robotics import DriveBase
from pybricks.tools import wait

# Set up all devices.
hub = TechnicHub(top_side=Axis.Z, front_side=Axis.X, broadcast_channel=5)
left = Motor(Port.C, Direction.CLOCKWISE)
right = Motor(Port.D, Direction.COUNTERCLOCKWISE)
turntable = Motor(Port.B, Direction.COUNTERCLOCKWISE)
drive_base = DriveBase(left, right, 48, 208)
xbox = XboxController()

# Initialize variables.
boom_speed = 0
jib_speed = 0
hoist_speed = 0


# The main program starts here.
while True:
    # If Y or X have a nonzero value:
    # - Then use Y for speed  and X for steering.
    # - Otherwise stop.
    if xbox.joystick_left()[1] or xbox.joystick_left()[0]:
        drive_base.drive(xbox.joystick_left()[1] * 2, xbox.joystick_left()[0] * 2)
    else:
        drive_base.stop()
    # We use the left minus the right trigger to control the turn table.
    # This lets you rotate it by pressing one or the other.
    # If that speed is zero, then coast the motor.
    if xbox.triggers()[0] - xbox.triggers()[1]:
        turntable.run((xbox.triggers()[0] - xbox.triggers()[1]) * 10)
    else:
        turntable.stop()
    # Operate boom using direction pad up/down.
    boom_speed = 100 if Button.UP in xbox.buttons.pressed() else (-100 if Button.DOWN in xbox.buttons.pressed() else 0)
    # Operate jib using right joystick vertical movement.
    jib_speed = xbox.joystick_right()[1]
    # Operate hoist using bumpers.
    hoist_speed = 100 if Button.LB in xbox.buttons.pressed() else (-100 if Button.RB in xbox.buttons.pressed() else 0)
    # Broadcast the values, so the hub in the crane can observe it.
    hub.ble.broadcast([boom_speed, jib_speed, hoist_speed])
    wait(50)
Program for the base hub.