Brief Introduction of microPython

Damien George is a computer engineer who works every day in the Python language and is doing some robotic projects. One day, he suddenly had an idea: is it possible to use Python language to control microcontroller and to realize the control of robot?


We know that Python is a relatively easy scripting language and has a strong community support. Some non-computer-specialized people use it as a first language. Unfortunately, it cannot achieve some very basic control. Therefore, it is inconspicuous in the field of hardware.


In order to break the limit, Damien spent six months to build Micro Python. It is based on ANSI C. Syntax is the same as Pyton 3. It has its own parser, compiler, virtual machine and class library, etc. Currently, it supports a lot of 32-bit processors, such as STM32 series, esp8266, esp32, rtl8195a, nrf51822 and so on.


With Micro Python, users can access and control the basics of hardware completely through Python scripting language, such as controlling LED bulb, LCD displayer, reading voltage, controlling motor, entering SD card, network, Bluetooth, and reading sensor etc.



Goal of the Article

After reading the article, you can have a preliminary understanding of micropython, which is mainly related to variable data type and basic syntax. To prevent being boring, we will use examples in the following to explain in-depth syntax content in details. It is recommended to manually enter the code for actual operation to deepen understanding.



Variable Data Type, Character Type

Character type is any text enclosed in single quotes’ or “double quotation marks”, such as ‘abc’, “xyz”. ‘ ‘ or “ “ is just symbol representing character, not a part of string. Therefore, string ‘abc’ only has three characters, a, b, c. If ‘ is also a character, we use “ “ to enclose it. For example “I’m Ok” includes 6 characters, I, ‘, m, space, O, K. For example:

>>>str=“hello DFRobot”
>>>Print(str)
hello DFRobot

str is a defined string variable and assigned to helloDFRobot. Then output the string.

Boolean Type

A Boolean value only has two values, either True or False (note that the first letter should be uppercase).


For example:

>>>True
True
>>>3>2
True
>>>5>7
False

and operation is collation operation. Only when all are True, result of and operation is True
>>>Trueand True
True
>>>Trueand False
False

or operation is either-or operation. As long as one of them is True, result of or operation is True
>>>Trueor True
True
>>>Trueor False
True

not operation is not-operation. It is unary operation, which changes True into False, and False into True
>>>not True
False
>>>not 1>5
True


Boolean value is often used in conditional judgment, eg:
>>>def int(age):
if age>=18:
print(‘adult’)
else:
print(‘teenager’)

Actual result is as follows:


Integer
Creating a new integer variable and assigning variable is the same process. For a=123 or b=-123, sign on the left of equal mark is variable name, and sign on the right is the value to be assigned to. It is very simple. Data of natural assignment should be whole number, which is simply explained as (positive integer and negative integer). Currently, operators in front of variables of integer data type supported by micropython are: plus (+), minus (-), multiplied by (×), divided by (/) and power (**).



Floating Point Type

Floating point number is decimal. It is called floating point number because the decimal point position of a floating point number can be changed when expressed in scientific notation, for example, 1.23×109 and 12.3×108 are completely the same. Floating point number can be written in mathematics like 1.23, 3.14, -9.01, etc. However, for extremely large or small floating point number, it is must be written in scientific notation. Replacing 10 in e, 1.23×109 is 1.23e9, or 12.3e8. 0.000012 can be written as 1.2e-5. If you cannot understand, you just remember---floating...

Read more »