In this project, we will show you how to control the LEGO® Technic 4X4 X-treme Off-Roader (42099) with the Xbox Controller.

This is a fun way to drive the truck around without having to look at your phone screen all time time. You can use all of the analog inputs to drive, which allows for smooth and precise control.

You can also use this setup to create your own remote-controlled cars with Pybricks.

Driving the LEGO® Technic 4X4 X-treme Off-Roader (42099) with the Xbox Controller.
Driving the LEGO® Technic 4X4 X-treme Off-Roader (42099) 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 Technic truck has one steering motor on Port C. It has drive motors on Ports A and B. Both have to run counterclockwise to move the truck forward. The program starts by setting up these motors accordingly.

This program lets you drive the LEGO® Technic 4X4 X-treme Off-Roader (42099)
  with the Xbox Controller. First, you set up the car, and then you can
  control the steering and power level using the analog inputs.
This program lets you drive the LEGO® Technic 4X4 X-treme Off-Roader (42099) with the Xbox Controller. First, you set up the car, and then you can control the steering and power level using the analog inputs.

The car setup block takes care of centering the steering motor and makes it easy to drive later on. Finally, we add the setup block for the Xbox Controller.

The main program is quite simple. There is an infinite loop that sets the steering and drive power based on the analog inputs.

Steering (-100% to 100%) is controlled with the left horizontal stick. The drive power is controlled with the right trigger minus the left trigger. This ranges the power level from -100% (reverse) if the left trigger is fully pressed, all the way to 100% (forward) if the right trigger is pressed.

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.

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 truck in action

When you’re ready, you can drive the truck as shown below!

Ideas for further exploration

You can use any of the other analog inputs drive your car. Can you change it to use the vertical movement of the right joystick to control the speed?

You can read which buttons are pressed. Can you add light functions to match the colors of the A/B/X/Y buttons?

You can also use the rumble block. Can you create force feedback so you can feel the road or when you hit an obstacle?

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.iodevices import XboxController
from pybricks.parameters import Direction, Port
from pybricks.pupdevices import Motor
from pybricks.robotics import Car
from pybricks.tools import wait

# Set up all devices.
steering = Motor(Port.C, Direction.CLOCKWISE)
front = Motor(Port.A, Direction.COUNTERCLOCKWISE)
rear = Motor(Port.B, Direction.COUNTERCLOCKWISE)
car = Car(steering, [front, rear])
xbox = XboxController()


# The main program starts here.
while True:
    # Control steering using the left joystick.
    car.steer(xbox.joystick_left()[0])
    # Control drive power using the trigger buttons.
    car.drive_power(xbox.triggers()[1] - xbox.triggers()[0])
    wait(50)
Python representation of the block program.