![]() The Pin object we created has functions to change its state. This pin is defined as an electrical output. This is why we create a pin_led, corresponding to the pin that controls the built-in LED. The module Pin is, in fact, a class that defines the operation of an input/output pin (GPIO). The machine module contains most of the objects specific to MicroPython. In the example script, we import from the machine module the sub-module Pin. MicroPython objects are easily used even without being an expert in object-oriented programming. Since Python programming will be used to control the electronics, we simply need to set up RPi as a Linux machine that can code, test, and run Python. For example, the input/output pins of the boards will be seen and manipulated as objects. As a reminder, Python is an object-oriented language: any element in Python is considered an object. The difference with MicroPython is that even basic objects must be imported. This is equivalent to the #include Arduino IDE code for external libraries #include , #include . The structure of a MicroPython script is as follows:Įach MicroPython script is preceded by a phase of importing the modules necessary for its proper functioning. A detailed explanation of a MicroPython script This is not very annoying during the development phase, but it is important to know when using a MicroPython script on a board. To remedy this, it is necessary to register the script on the board with the following name main.py. Note that the Python script will not automatically launch when the Pico board is switched on. To save the script on the board afterward, go to File → Save as. Thonny IDE will send the code directly to the MicroPython interpreter (via the REPL) when executing the script. On Thonny IDE, it is better to save the code on the computer and not directly on the board. If necessary, you can refer to the following tutorial, the procedure to follow for configuring the Raspberry Pi Pico in Thonny IDE. Running the MicroPython script on your Pico.įirst, ensure you have configured your IDE software for your Pico board. You should get the same result, as illustrated in figure 4.įigure 4 – Python 3.x “Hello World” program.It’s best to know the basics of Python syntax before jumping into MicroPython. To print the “Hello World” message, simply send the same Python command we have used before. You can confirm that by the version highlighted in the figure.įigure 3 – Starting the Python 3.x interpreter via command line. So, to open the Python 3.x interpreter, our command needs to reflect that.Īfter opening a new command line, simply type the following command and hit enter (notice the 3 at the end of the command):Īs shown in figure 3, now it will open the Python 3 interpreter. When you finish, simply close the whole command line.Īs we have seen in the previous section, if we just write the python command on the command line, it will open the Python 2.x interpreter. You can keep exploring other Python commands in the prompt. The message should get printed as illustrated in figure 2.įigure 2 – Python 2.x “Hello World” program. Now to print a “Hello World” message, simply type the following command and hit enter (you can change the string inside the double quotes to print whatever message you want): Note that, as highlighted, the interpreter lists the whole Python version, including the minor and the micro version.įigure 1 – Starting the Python 2.x interpreter via command line. To do it, simply open a command line and type the following command:Īfter sending the command, it should start the interpreter, a shown in figure 1. ![]() This tutorial was tested on a Raspberry Pi 3 model B+.Īs mentioned, we will need to open the Python interpreter to get started. In both cases, the first thing we will need to do is opening the corresponding Python interpreter and then sending the command to print the message. So, we are going to check how to run the “Hello World” printing program from the command line in the two versions of Python. You can check more about Python versioning here.įor this tutorial, I’m running version 4.9 of Raspbian, installed using NOOBS, which includes both versions of Python. If you want to learn how to print a “Hello World” message using IDLE, the Python IDE, please check this previous article.Īs mentioned in the previous tutorial, some versions of the Raspbian operating system have two versions of Python installed (Python 2.x and Python 3.x, where x is the minor version). In this tutorial we will check how to print a very simple “Hello World” message with Python running on the command line, on the Raspberry Pi. In this tutorial we will check how to print a very simple “Hello World” message with Python running on the command line, on the Raspberry Pi. This tutorial was tested on a Raspberry Pi 3 model B+.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |