As you read in the Overview, MicroPython is an implementation of Python. In this page, you will find some basic and intermediate MicroPython scripts that can be used by any Arduino board. This includes some very common concepts such as variables, loops, delays, how to print and more.
There are some differences between MicroPython and Python, which mostly concern standard library and types, but also some language-level features.
After you download Arduino Lab for MicroPython, click the file for your system, extract and run the application. The interface is similar to Arduino IDE.
First of all, we need to connect our board to the computer via USB.
After connecting the board, click on the connect button, and select the port.
Now that you have connected your board, let’s create a file that will contain the script that will run on your board. Click on New button to create your file. The editor automatically creates the main file in the board.
If you are not working with the editor, the file should be named main.py and should be saved to your board manually. The board will recognize this as the main program.
Our first example is a basic script that will print
every second in the terminal. Paste the below script in the editor, and press the "Play" button.
There are three basic programming constructs which are Sequential, Looping and Branching.
In this section we’ll see some of program constructs.
In this second example we introduce the module time using import in order to pause the execution for a specific time. The module help us control the board with MicroPython.
1import time2content = "Hello world!"34while True:5 print(content)6 time.sleep(1)
By definition a variable is a string of characters and numbers associated with a piece of information. We define the variable content
Then with the
loop we execute the statements as long as the condition is true. In the code we use the
function to pause the execution of the script for a second before it continues to print. This is imported from the
A function is a block of code, a sequence of instructions composed by several statements, which runs only when it is called. You can pass the information as parameters into a function. A function can have input arguments, and can also have output parameters.
We can define our own functions, the most common way can be specified using the def keyword, inside the parentheses you can find the arguments if there are. Take a look to the example below:
1def my_function():2 print("Hello world!")
Then you can call your function using the function name followed by parentheses:
The function need two components: the header, starting with keyword def, followed by parentheses with inside the arguments and ending by colon (:) and the indented body is composed by descriptive string, function statements, return statements.
This script prints "Hello world!" every second. In addition, the function counter_function() increases the number every second and will be printed next to.
1import time23content = "Hello world!"4count = 056def counter_function():7 global count8 count = count + 1910while True:11 counter_function()12 print(content, count)13 time.sleep(1)
MicroPython supports logical conditions from mathematics, that can be used in several ways, the most common is an "if conditional" and “for loop”. The if statements is written by if keyword and it needs the indentation, otherwise you will get an error.
A if/else statement is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by the program.
You can try the code below:
1a = 422b = 233if a > b :4 print("a is greater than b")5else :6 print("a is not greater than b")
The result in this case is always going to be:
a is greater than b
is larger than
Simple use of a for loop and functions. This script counts to 10, and then back to 0.
1import time23count = 045def function_increase():6 global count7 count = count +18 print(count)910def function_decrease():11 global count12 count = count -113 print(count)1415while True:16 for x in range(10):17 function_increase()18 time.sleep(1)1920 for x in range(10):21 function_decrease()22 time.sleep(1)
An array is one of the most known and used construct in programming. In MicroPython an array by definition is a collection of elements (values or variables), selected by one or more indices computed at run-time, you refer to an array element by referring to the index number.
1myFruit = ['orange', 'persimon', 'apple', 'kiwi', 'lemon']23def printFruitNames():4 for fruit in myFruit:5 print(fruit)67printFruitNames()