Python Programming on mBlock 5

With the Python editor of mBlock 5, you can write programs in Python as in any other Python code editors.

Open the Python editor

1. Open mBlock 5 and click Python Editor.


2. Choose File > New Project to create a project.


Now you can start to write your code in the editing area.


Write your Python code

Option 1: Write your code without libraries

num1 = 100
num2 = 200
# Add the two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))


After writing the code, click Run.

The terminal displays the execution result of the program.


Option 2: Write your code based on a library

In addition to the libraries developed by Makeblock, the mBlock Python editor provides some popular third-party libraries and you can write your code based on them.

The following uses the termcolor library as an example. 

#Print Constellations
from termcolor import *
for i in range(12):

After writing the code, click Run.

If the termcolor library has not been installed, you are prompted to install it.


Click Yes and wait for the installation to be completed.


Click Run again. 

The terminal displays the execution result of the program.


You can also install the libraries you need before you write your code.

Install a library

The following describes how to install a library:

1. Choose Libraries.


2. Search for the library you want to install by category and click the Install button on the right.


Option 3: Program a device in Python

Currently, the Python editor supports only CyberPi. You can write your code to control CyberPi.

1. Use a USB cable to connect CyberPi to your computer.

2. Click Connect in mBlock-Python Editor, and then click Connect in the dialog box that appears.


3. Set program execution mode to Live.

For device programming, mBlock-Python Editor provides two program execution modes: Live and Upload.


The Live mode supports Python 3 libraries and is used to control devices in real time.

In Live mode, you don't need to upload programs to devices and can click Run to execute a program in real time.



The Upload mode supports only MicroPython, and you can't use third-party Python 3 libraries to program devices.

In Upload mode, you need to click Upload to upload a program to a device. After being uploaded to a device, a program can be executed on the device even when the device is disconnected from mBlock-Python Editor.



4. Choose File > New project, and enter your code in the project file editing area.


5. Install the cyberpi library.

CyberPi is the latest main control board developed independently by Makeblock. To program CyberPi, you need to install the cyberpi library.


6. Enter the following code to implement the marquee effect of the LEDs on CyberPi:

import math 
import random
import cyberpi
from time import sleep"r g b y c")
count = 0
while True:
    cyberpi.led.set_bri(math.sin(count / 4) * 50 + 52) 
    count += 1



3. Click Run.

The onboard LEDs of CyberPi are lit up in marquee mode.

More information

You can learn more about Python coding from Python Example Programs

To program CyberPi, refer to the Python API Documentation for CyberPi.

Was this article helpful?
7 out of 16 found this helpful


1 comment

Please sign in to leave a comment.