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 halocode
import event

def on_start():
    halocode.led.show_all(122, 239, 10)

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

Convert Blocks to Python Code

In the Scripts area, click     to covert blocks to Python. The following is an example:

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.

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


Turn off all the LEDs.

halocode.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.

halocode.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.

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


Turn off one specified LED.

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


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

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


Use the status of the LED ring to display percentage.

halocode.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(halocode.button.is_pressed()) # Print True if the button is pressed


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

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


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

print(halocode.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(halocode.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(halocode.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:", halocode.motion_sensor.get_yaw(), halocode.motion_sensor.get_pitch()) # output the yaw and pitch value of the gyro
print("The rotation value is:", halocode.motion_sensor.get_rotation(x), halocode.motion_sensor.get_rotation(y), halocode.motion_sensor.get_rotation(z))
halocode.motion_sensor.reset_roation() # reset the rotation value.
print("Shake strength:", halocode.motion_sensor.get_shake_strength())


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

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


Reset the timer.

print(halocode.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!")

halocode.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!")

halocode.on_greater_than(50, 'microphone')


When the specified message is received, call the function.

def on_received():
    print("Game start!")


results matching ""

    No results matching ""