Use Python with HaloCode

HaloCode is a MicroPython-based single board computer. Python code need to be uploaded to HaloCode to run.

Start Using Python

Switch the programming mode from "Blocks" to "Python" to start using Python.

Note: please make sure that "HaloCode" is currently selected.

Here's an example code:

import halo

import event


def on_start():

halo.led.show_all(122, 239, 10)



After programming, click "Upload" to upload the program to HaloCode.

Convert Blocks to Python Code

Use HaloCode's LEDs

LED Ring

The ID and position of each of the 12 LEDs are as follows:

led.show_all(r, g, b)

Set the color of all the LEDs, mixed by red, green, and blue, each color with a value range 0-255.

halo.led.show_all(255, 0, 0) # Set all the LEDs to color red


Turn off all the LEDs.

halo.led.off_all() # Turn off all the LEDs


Set the color of all 12 LEDs at the same time. There are ten colors: red, green, blue, yellow, cyan, purple, white, orange, black, and gray.

halo.led.show_ring('red orange yellow green cyan blue purple white white white white white') # Set the 12 LEDs to color red, orange, yellow, green, cyan, blue, purple, white, white, white, white, and white respectively

led.show_single(led_id, r, g, b)

Set the color of one specified LED.

halo.led.show_single(1, 255, 0, 0) # Set the color of the first LED to red


Turn off one specified LED.

halo.led.off_single(4) # Turn off the fourth LED


Show the default LED animation. There are four options: spoondrift, meteor, rainbow, and firefly.

halo.led.show_animation('rainbow') # Show the "rainbow" LED animation


Use the status of the LED ring to display percentage.

halo.led.ring_graph(60) # Use the LED ring to display 60%

Use the Sensors of HaloCode


If the button is pressed, return True; otherwise, return False.

print(halo.button.is_pressed()) # Print True if the button is pressed


Get the loudness of the microphone. The range is 0-100.

print(halo.microphone.get_loudness()) # Output the loudness


Tell whether HaloCode is being shaken. The result is True or False.

print(halo.motion_sensor.is_shaked()) # If HaloCode is being shaken, return True


Tell whether HaloCode is tilted to the right or left. The result is True or False.

print(halo.motion_sensor.is_tilted_left()) # If HaloCode is left-tilted, return True


Tell whether HaloCode is placed arrow-up or arrow-down. The result is True or False.

print(halo.motion_sensor.is_arrow_up()) # If HaloCode is placed arrow-up, return True


Get the roll, pitch or yaw value of HaloCode's gyroscope

  • motion_sensor.get_roll()

  • motion_sensor.get_pitch()

  • motion_sensor.get_yaw()

Get the gyroscope's rotation value (in degrees) around a certain axis.

  • motion_sensor.get_rotation(axis): x, y, or z axis

  • motion_sensor.reset_rotation(axis="all"): reset the rotation angles of the gyro

You can use motion_sensor.get_shake_strength() to get the intensity of the shaking.

print("The yaw and pitch is:", halo.motion_sensor.get_yaw(), halo.motion_sensor.get_pitch()) # output the yaw and pitch value of the gyro

print("The rotation value is:", halo.motion_sensor.get_rotation(x), halo.motion_sensor.get_rotation(y), halo.motion_sensor.get_rotation(z))

halo.motion_sensor.reset_roation() # reset the rotation value.

print("Shake strength:", halo.motion_sensor.get_shake_strength())


Get the timer value in seconds (since startup or last reset).

print(halo.get_timer()) # print the timer value since startup or last reset


Reset the timer.


print(halo.get_timer()) # prints 0

HaloCode's Event and Flow Control

HaloCode supports events (like when the button is pressed), and it also supports multi-threading.

If you wish to use event, declare a function and register it to the event. A program can only register no more than 6 event functions.


def on_button_pressed(): # define a function

print("The button is pressed!")

halo.on_button_pressed() # register it to "when button is pressed" event


When the button is pressed, run the function.

def on_button_pressed():

print("The button is pressed!")



When HaloCode is being shaken, call the function.

def on_shaked():

print("I'm shaken!")



Call the function when HaloCode is tilted to the left.

def on_tilted_left():

print("I'm left-tilted!")


on_greater_than(volume, 'microphone')

When the loudness is over a certain value, call the function.

def on_greater_than():

print("The loudness is over 50! Too loud!")

halo.on_greater_than(50, 'microphone')


When the specified message is received, call the function.

def on_received():

print("Game start!")


Last updated