How About A Wireless Setup??

Race against your friends! In this game, RVR's LEDs will randomly change colors. Your job is to press the button that matches the color of the robot's LEDs. The better you do, the faster RVR+/RVR will go! If you hit the wrong button or you wait too long to press a button, RVR+/RVR will slow down, so try to keep up!

This straighforward example should take you about 30 min to complete.

If you haven't already set up your micro:bit using your favorite editor, it's probably best to take care of that before jumping in here. Feel free to check out our Getting Started docs on:

Hardware Setup

Parts List

If you want to build your own Rainbow Racer, you will need the following parts (most of them are included in this nifty "inventor's" kit from SparkFun):

Wiring Diagram

First you'll need to put together your circuit! Follow the wiring diagram below:

Software Setup

After you've got your circuit set up, you're ready to flash the code to your micro:bit. Let's explore the code a bit and then we'll walk through how to flash the code.

The Code

Let's get started with our imports. We'll need random for the RVR+/RVR to be able to choose a random color for the user to have to match. From the microbit, we'll pull in what we need to transfer information (via the needed pins) and give feedback to the user (using display and Image).

import random

from microbit import display, Image, sleep, pin8, pin12, pin13, pin16

from sphero import RVRDrive, RVRLed, RVRPower

Our check_input function will read the GPIO pins to figure out which button (if any) was pressed:

def check_input():

    input_btn = 0

    if pin8.read_digital():

        input_btn = 1  # yellow

    elif pin12.read_digital():

        input_btn = 2  # green

    elif pin13.read_digital():

        input_btn = 3  # red

    elif pin16.read_digital():

        input_btn = 4  # blue


    return input_btn

Our set_leds function will set RVR's LEDs depending on the randomly generated current_color:

def set_leds():

    if current_color == 1:

        RVRLed.set_all_leds(255, 255, 0# yellow

    elif current_color == 2:

        RVRLed.set_all_leds(0, 255, 0# green

    elif current_color == 3:

        RVRLed.set_all_leds(255, 0, 0# red

    elif current_color == 4:

        RVRLed.set_all_leds(0, 0, 255# blue


Our show_correct_feedback function will show a check mark if the input_btn matched the current_color:

def show_correct_feedback():



Our show_incorrect_feedback function will show an X if the input_btn did not match the current_color:

def show_incorrect_feedback():



Now that we've gotten through our function definitions, we can wake up our RVR+/RVR, in preparation for using these functions:



Next, we'll initialize our variables (and use our random import 😉):

current_color = random.randint(1, 4)

current_speed = 0

penalty_counter = 0

input_btn = 0

Now we get to the good stuff! First, we'll initialize our LED value and then we'll set that we should check the user's input when we use input_btn and initialize a counter for how many times the user hits an incorrect button.

In our first bit of logic, we'll check if the user takes too long to press a button.

In the second section, we have two pieces of logic that keep current_speed within the range [0, 60]; we'll use the current_speed we land on in a drive command to make the RVR+/RVR move forward at the current_speed.

Once we have all that information, we check user input and give proper feedback!

while True:


    input_btn = check_input()

    penalty_counter += 1


    if penalty_counter > 250:


        current_speed -= 6

        penalty_counter = 0

        current_color = random.randint(1, 4)

        input_btn = 0

    if current_speed > 60:

        current_speed = 60

    if current_speed < 0:

        current_speed = 0, 0)

    if input_btn == 0:


    elif input_btn == current_color:


        current_speed += 6

        penalty_counter = 0

        current_color = random.randint(1, 4)

        input_btn = 0

    elif input_btn != current_color:


        current_speed -= 6

        input_btn = 0

Now, you're ready to play!

Flashing the Code

Follow the below steps:

The Race

To start the race all you'll need to do is plug the micro:bit (which should be mounted in the breakout board) into the RVR+/RVR using the USB cable, and then turn on the RVR+/RVR. You'll need to walk with the RVR+/RVR as it moves since it's plugged into your micro:bit - that's why we suggested finding a long cable! If you have a friend that built their own Rainbow Racer, you can race them! Consider putting tape lanes and a finish line on the ground to have an official race track (keep in mind that the RVR+/RVR will only drive in a straight line). If you're flying solo, run multiple races and keep track of your times. See if you can beat your own high score!

Now What?

Try to improve your Rainbow Racer! Here are some ideas to get you started:

If you're adding a lot to your code, you may run into some Memory Errors. That's okay - micro:bit is best for smaller projects. If you feel like you've outgrown your micro:bit, try upgrading to an Arduino or a Raspberry Pi!

Try our next activity!