You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: 'Learn how to use the runtime package, which allows you to write MicroPython code, Arduino style.'
4
-
author: 'Karl Söderby'
2
+
title: "Arduino MicroPython Runtime"
3
+
description: "Learn how to use the Arduino MicroPython runtime package, which allows you to write MicroPython code in a familiar Arduino style."
4
+
author: "Karl Söderby"
5
5
tags: [MicroPython, Runtime]
6
6
micropython_type: test
7
7
---
8
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.
9
+
The [Arduino Runtime Package](https://github.com/arduino/arduino-runtime-mpy/tree/main) is a MicroPython package that allows you to write and program your board using the classic `setup()` and `loop()` constructs.
10
10
11
11
The package was designed to make it easier to create programs, particularly for those familiar with the Arduino C++ environment.
12
12
@@ -28,19 +28,19 @@ To follow this tutorial, you will need to have the following requirements ticked
28
28
29
29
## Installation
30
30
31
-
To use the runtime library, we will need to install it first.
31
+
To use the runtime package, we will need to install it first.
32
32
33
-
1. Download and install the [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer).
33
+
1. Download and install the [Arduino MicroPython Package Installer](https://labs.arduino.cc/en/labs/micropython-package-installer).
34
34
2. Connect your board to your computer.
35
35
3. Run the tool. In the tool, you should now see your board connected.
36
36
37
-
![Board connected.]()
37
+

38
38
39
39
4. After verifying that your board is connected, click on the search field, and search for **runtime**. Install the package.
40
40
41
-
![Install the package.]()
41
+

42
42
43
-
5. When the installation is complete, we are ready to use the library.
43
+
5. When the installation is complete, we are ready to use the package.
44
44
45
45
## Basic Example
46
46
@@ -64,6 +64,7 @@ start(setup, loop)
64
64
```
65
65
66
66
This program has two main functions: `setup()` and `loop()`. If you are unfamiliar with this concept, here's how it works:
67
+
67
68
-`setup()` - this function will run just once, at the start of a program. Like in this example, we use `print('starting my program')`.
68
69
-`loop()` - this function will continue to run, until you disrupt the program by disconnecting the board or stopping the script.
69
70
@@ -75,16 +76,23 @@ At the bottom of the program, we have something called `start()`. This function
75
76
76
77
Arduino Runtime was created to simplify the code creation when programming in MicroPython, providing a more user-friendly syntax that allows you to understand the programs you create a bit better.
77
78
78
-
Now that we have everything installed, and our basic example tested out, let's take a look at some of the more common examples.
79
+
Now that we have everything installed, and our basic example tested out, let's take a look at some of the more common examples.
79
80
80
-
***A list of commands are listed [later in this article](). You can also view the [source code on Github]() for further understanding.***
81
+
***The API is listed at [the end of this article](#runtime-api). You can also view the [source code on GitHub](https://github.com/arduino/arduino-runtime-mpy/tree/main) for further understanding.***
81
82
82
83
### Pin Mode
83
84
84
85
-`pin_mode(pin, mode)`
85
86
86
87
Configures a pin as an input or an output.
87
88
89
+
```python
90
+
pin ="D6"
91
+
92
+
defsetup():
93
+
pin_mode(pin, OUTPUT)
94
+
```
95
+
88
96
### Analog Read
89
97
90
98
-`analog_read(pin)`
@@ -113,7 +121,7 @@ To write an analog signal (using PWM), we can use the `analog_write()` method. T
113
121
The example below sets the pin to "half capacity", and if you connect an LED to this pin, it will shine at half brightness.
114
122
115
123
```python
116
-
pin ="D6"
124
+
pin ="LED_BUILTIN"
117
125
brightness =127#half brightness
118
126
119
127
defsetup():
@@ -132,11 +140,13 @@ start(setup, loop)
132
140
Reads a digital pin and returns a HIGH (1) or LOW (0) value.
133
141
134
142
```python
143
+
pin ="D2"
144
+
135
145
defsetup():
136
146
print("Digital Read Example")
137
147
138
148
defloop():
139
-
value = digital_read("D2")
149
+
value = digital_read(pin)
140
150
print(value)
141
151
142
152
start(setup, loop)
@@ -146,15 +156,16 @@ start(setup, loop)
146
156
147
157
-`digital_write(pin)`
148
158
149
-
Writes a HIGH (1) or LOW (0) value to a digital pin.
159
+
Writes a **HIGH (1)** or **LOW (0)** value to a digital pin.
150
160
151
161
```python
162
+
pin ="LED_BUILTIN"
163
+
152
164
defsetup():
153
165
print("Digital Write Example")
154
166
155
167
defloop():
156
-
digital_write("D2")
157
-
168
+
digital_write(pin, HIGH)
158
169
159
170
start(setup, loop)
160
171
```
@@ -163,12 +174,12 @@ start(setup, loop)
163
174
164
175
-`delay(time)`
165
176
166
-
Freezes the program for the duration specified in *microseconds*.
177
+
Freezes the program for the duration specified in _microseconds_.
167
178
168
179
Below is a demonstration of the classic blink example:
169
180
170
181
```python
171
-
led ="D13"
182
+
led ="LED_BUILTIN"
172
183
173
184
defsetup():
174
185
print("Delay Example")
@@ -179,21 +190,178 @@ def loop():
179
190
delay(1000)
180
191
digital_write(led, LOW)
181
192
delay(1000)
182
-
193
+
183
194
start(setup, loop)
184
195
```
185
196
186
-
## Runtime Specific Examples
197
+
## Runtime API
198
+
199
+
The API for the runtime package can be found below. See the table for a quick overview:
200
+
201
+
| Function | Description | Parameters | Returns | Alias |
0 commit comments