|
| 1 | +--- |
| 2 | +title: 'Arduino Style MicroPython' |
| 3 | +description: 'Learn how to use the runtime package, which allows you to write MicroPython code, Arduino style.' |
| 4 | +author: 'Karl Söderby' |
| 5 | +tags: [MicroPython, Runtime] |
| 6 | +micropython_type: test |
| 7 | +--- |
| 8 | + |
| 9 | +The [Arduino Runtime Package]() is a MicroPython package that allows you to write and program your board using the classic `setup()` and `loop()` constructs. |
| 10 | + |
| 11 | +The package was designed to make it easier to create programs, particularly for those familiar with the Arduino C++ environment. |
| 12 | + |
| 13 | +In this tutorial, you will learn how the package works, along with a set of examples that will get you started. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +To follow this tutorial, you will need to have the following requirements ticked: |
| 18 | + |
| 19 | +### Hardware Requirements |
| 20 | + |
| 21 | +- [A MicroPython compatible board](/micropython/first-steps/install-guide/#micropython-compatible-arduino-boards) (in this tutorial, we will be using an [Arduino Nano ESP32](https://store.arduino.cc/products/nano-esp32)) |
| 22 | +- MicroPython installed on your board (see [installation instructions for MicroPython](/micropython/first-steps/install-guide/)). |
| 23 | + |
| 24 | +### Software Requirements |
| 25 | + |
| 26 | +- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - an editor where we can create and run MicroPython scripts on an Arduino board. |
| 27 | +- [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer) - for installing **MicroPython packages** on an Arduino board. |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +To use the runtime library, we will need to install it first. |
| 32 | + |
| 33 | +1. Download and install the [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer). |
| 34 | +2. Connect your board to your computer. |
| 35 | +3. Run the tool. In the tool, you should now see your board connected. |
| 36 | + |
| 37 | + ![Board connected.]() |
| 38 | + |
| 39 | +4. After verifying that your board is connected, click on the search field, and search for **runtime**. Install the package. |
| 40 | + |
| 41 | + ![Install the package.]() |
| 42 | + |
| 43 | +5. When the installation is complete, we are ready to use the library. |
| 44 | + |
| 45 | +## Basic Example |
| 46 | + |
| 47 | +We will begin by one of the most known example: blinking an LED. Let's take a look at the code example below: |
| 48 | + |
| 49 | +```python |
| 50 | +from arduino import * |
| 51 | + |
| 52 | +led = 'LED_BUILTIN' |
| 53 | +def setup(): |
| 54 | + print('starting my program') |
| 55 | + |
| 56 | +def loop(): |
| 57 | + print('loopy loop') |
| 58 | + digital_write(led, HIGH) |
| 59 | + delay(500) |
| 60 | + digital_write(led, LOW) |
| 61 | + delay(500) |
| 62 | + |
| 63 | +start(setup, loop) |
| 64 | +``` |
| 65 | + |
| 66 | +This program has two main functions: `setup()` and `loop()`. If you are unfamiliar with this concept, here's how it works: |
| 67 | +- `setup()` - this function will run just once, at the start of a program. Like in this example, we use `print('starting my program')`. |
| 68 | +- `loop()` - this function will continue to run, until you disrupt the program by disconnecting the board or stopping the script. |
| 69 | + |
| 70 | +Inside of the functions, you can see that we are using `digital_write(led, HIGH)`. This is a function that will enable a pin on the board, and write it high (or low). Since we configured it at the top as `'LED_BUILTIN'`, we will control that LED on the board. |
| 71 | + |
| 72 | +At the bottom of the program, we have something called `start()`. This function will launch the program and concurrently run the `loop()` function. |
0 commit comments