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
Copy file name to clipboardExpand all lines: content/micropython/03.micropython/02.environment/01.code-editor/code-editor.md
+30-11Lines changed: 30 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,20 @@
1
1
---
2
-
featured: micropython-101
3
-
title: '2. Micropython Environment - Editor'
2
+
title: 'Code Editor'
4
3
description: 'Learn the basics for loops on MicroPython.'
5
4
author: 'Pedro Lima'
6
-
hero_image: "./hero-banner.png"
5
+
tags: [MicroPython, Arduino Lab for MicroPython]
7
6
---
8
7
9
-
Arduino Labs for MicroPython includes a user-friendly code editor that helps us write, format, and run MicroPython code with ease. In this article, we’ll explore how the code editor works, key formatting rules for MicroPython, and some useful tips to keep our code clean and error-free.
8
+
Arduino Lab for MicroPython includes a user-friendly code editor that helps us write, format, and run MicroPython code with ease. In this article, we’ll explore how the code editor works, key formatting rules for MicroPython, and some useful tips to keep our code clean and error-free.
10
9
11
-
## The Code Editor in Arduino Labs for MicroPython
10
+
## Requirements
11
+
12
+
-[Arduino Lab for MicroPython]()
13
+
-**MicroPython compatible board** (not required for using the editor, but for running any code).
14
+
15
+
***Note that the Arduino Lab for MicroPython is now also available online, as part of the Arduino Cloud platform. You will find it here: [Arduino Cloud - Arduino Lab for MicroPython]().***
16
+
17
+
## Arduino Labs for MicroPython
12
18
13
19
The code editor in Arduino Labs is designed to streamline our coding experience, providing tools to:
14
20
@@ -18,6 +24,24 @@ The code editor in Arduino Labs is designed to streamline our coding experience,
18
24
19
25
As we get familiar with the editor, remember that MicroPython has a few syntax rules that differ from other languages we might know.
20
26
27
+
### Overview
28
+
29
+
Text / bullet points here summarizing e.g. buttons and icons in the editor.
30
+
31
+
![Overview of the code editor.]()
32
+
33
+
### Files
34
+
35
+
Text / bullet points here for how to manage files (and point to the file system tutorial)
36
+
37
+
![Managing files in Arduino Lab for MicroPython]()
38
+
39
+
### REPL
40
+
41
+
Text / bullet points here for where the REPL is and how it works (and point to the REPL tutorial)
42
+
43
+
![The REPL.]()
44
+
21
45
## Indentation
22
46
23
47
In MicroPython, indentation (using spaces or tabs) is essential for defining the structure of our code. Unlike many other languages, MicroPython relies on indentation instead of symbols like `{ }` to define code blocks.
@@ -104,9 +128,4 @@ Here are a few quick tips to make the most of our time in the code editor:
104
128
-**Use Descriptive Names**: Name variables and functions clearly to make the code readable.
105
129
-**Keep It Modular**: Split complex code into functions for easier maintenance.
106
130
-**Comment as We Go**: Leave helpful comments explaining why we wrote certain sections of code.
107
-
-**Indent Carefully**: Pay attention to indentation for clean, error-free code.
108
-
109
-
## Try It Out
110
-
111
-
-**Experiment with Indentation**: Try writing a few functions with nested loops or conditionals to get comfortable with indentation.
112
-
-**Add Comments**: Document a small code snippet with comments explaining each part.
131
+
-**Indent Carefully**: Pay attention to indentation for clean, error-free code.
Copy file name to clipboardExpand all lines: content/micropython/03.micropython/02.environment/02.file-system/file-system.md
+51-44Lines changed: 51 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,21 @@
2
2
title: 'File System'
3
3
description: 'Learn how to use the File Sytem in MicroPython.'
4
4
author: 'Pedro Lima'
5
-
tags: [MicroPython, REPL]
5
+
tags: [MicroPython, File System]
6
6
---
7
7
8
+
When working with MicroPython, we’re not limited to a single program file like in traditional Arduino sketches (using C++). Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
8
9
9
-
When working with MicroPython, we’re not limited to a single program like in traditional Arduino sketches. Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
10
-
11
-
In this article, we’ll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
10
+
In this article, we'll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
12
11
13
12
## The MicroPython File System: Key Differences
14
13
15
-
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. With MicroPython, however, we work within a file system that can store multiple files. This file system allows us to:
14
+
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. In MicroPython we work within a file system that can store multiple files. This file system allows us to:
16
15
17
16
-**Upload and Download Files**: We can save individual scripts, libraries, and assets directly on the device.
18
17
-**Organize Project Files**: Create folders, save multiple scripts, and organize files for a modular approach.
19
18
-**Edit Files Directly on the Device**: Modify files without needing to overwrite everything, making adjustments faster and more flexible.
19
+
-**Run Different Scripts**: while we have `boot.py` (runs at start) and `main.py` (runs after start), we can also create more scripts that we can run, either from the editor, or from the board itself.
20
20
21
21
## Accessing the MicroPython File System
22
22
@@ -26,9 +26,11 @@ To interact with the MicroPython file system, we’ll use Arduino Labs for Micro
26
26
2.**Upload and Download Files**: Use the file manager to upload files from our computer to the microcontroller or download files back to our computer.
27
27
3.**Organize Files**: We can create folders and store multiple files, making it easy to organize our project.
28
28
29
+
![Accessing the file system.]()
30
+
29
31
## Basic MicroPython File Structure
30
32
31
-
A typical MicroPython project includes a main file, boot script, libraries, and any supporting files our project needs. Here’s a standard layout:
33
+
A typical MicroPython project includes a `main.py` file, `boot.py` file, libraries, and any supporting files our project needs. Here’s a standard layout:
32
34
33
35
```
34
36
/ (Root Directory)
@@ -41,59 +43,64 @@ A typical MicroPython project includes a main file, boot script, libraries, and
41
43
-**`boot.py`**: Runs once at startup, before `main.py`, and is typically used for system configurations that need to be set when the device first powers on.
42
44
-**`main.py`**: This is the primary script, similar to the `setup()` and `loop()` structure in Arduino. It runs automatically after `boot.py` finishes.
43
45
44
-
## Example: Switching Execution from `main.py` to `favorite_things.py`
46
+
## Example: Importing Code from Scripts
45
47
46
-
Let’s say we’re creating a project where `main.py`runs and, at a certain point, hands off control to `favorite_things.py` to perform a specific task. Here’s how we might organize it.
48
+
With the MicroPython file system, we can create our own scripts and import them in the `main.py`script. This can be helpful in avoiding long scripts, as we instead can store the code in other files. This approach also makes it more modular.
47
49
48
-
1.**Write the Main Script** (`main.py`): This script runs some initial code and then switches to executing `favorite_things.py`.
49
-
2.Create the file "favourite_things.py".
50
-
3.**Add Content to `favorite_things.py`**: In this case, we’ll simply print a message from `favorite_things.py`.
50
+
To run code from a separate script in our `main.py` file, we can follow the instructions below:
51
51
52
-
### Sample `main.py` Code
52
+
1. Create a file named `my_new_script.py`, and add the following function:
53
53
54
-
Here’s how `main.py` might look, printing an introductory message and then executing `favorite_things.py`:
54
+
```python
55
+
deftest():
56
+
print("This runs from my_new_script.py")
57
+
```
55
58
56
-
```python
57
-
defmain():
58
-
print("Getting the list of my favourite things...")
59
+
2. In `main.py`, we run some initial code and then switches to executing a function from`my_new_script.py`. Here's an example:
59
60
60
-
# Now execute favorite_things.py
61
-
print("Switch to favorite_things.py...")
62
-
try:
63
-
withopen("favorite_things.py") as f:
64
-
exec(f.read())
65
-
exceptOSError:
66
-
print("Error: Could not open favorite_things.py")
67
-
68
-
if__name__=="__main__":
69
-
main()
70
-
```
61
+
```python
62
+
import my_new_script
63
+
print("This runs from main.py")
71
64
72
-
### Sample `favorite_things.py` Code
65
+
my_new_script.test()
66
+
```
73
67
74
-
In `favorite_things.py`, we’ll keep it simple with a message:
68
+
3. Check the REPL, we should see:
75
69
76
-
```python
77
-
# favorite_things.py
78
-
print("Bears. Beets. Battlestar Galactica.")
79
-
```
70
+
```bash
71
+
This runs from main.py # executed from main.py
72
+
This runs from my_new_script.py #executed from my_new_script.py
73
+
```
80
74
81
-
### Explanation
75
+
Essentially, this is how [modules]() work. You import a module, and use a function from that module.
82
76
83
-
-**Switching Execution**: `main.py` starts by printing an introductory message, and then uses `exec(f.read())` to read and run the content of `favorite_things.py`.
84
-
-**Running Another Script**: The `exec()` function allows `main.py` to execute `favorite_things.py` as if it were part of `main.py`, printing “Bears. Beets. Battlestar Galactica.” directly.
77
+
![Import code from a script.]()
85
78
86
-
### Expected Output
79
+
## Example: Directly Executing a Script
87
80
88
-
When you run `main.py`, the output should look like this:
81
+
We can also directly execute another script stored on the device. For this example, let's create a script and name it `run_directly.py`.
89
82
90
-
```
91
-
Getting the list of my favourite things...
92
-
Switch to favorite_things.py...
93
-
Bears. Beets. Battlestar Galactica.
94
-
```
83
+
1. In the script, store this code:
84
+
85
+
```python
86
+
print("I was run from main.py")
87
+
```
88
+
89
+
2. Then in`main.py`, we will use the `open()`, `exec()`and`read()` functions.
90
+
91
+
```python
92
+
withopen("run_directly.py") as f:
93
+
exec(f.read())
94
+
```
95
+
96
+
-`open()`- opens a file
97
+
-`exec()`- executes a file
98
+
-`read()`- reads a file
99
+
100
+
As a result, we should read `"I was run from main.py"`in the REPL. How this differs from the previous example, is that the `run_directly.py` script was just run from top to bottom, as opposed to importing a specific segment of code.
101
+
102
+
![Executing a script directly.]()
95
103
96
-
This setup demonstrates how to use MicroPython’s file system to organize and execute multiple scripts, allowing for modular and readable code.
Copy file name to clipboardExpand all lines: content/micropython/03.micropython/02.environment/03.modules/modules.md
+12-8Lines changed: 12 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
2
title: 'Modules'
3
-
description: 'Understanding Modules and how to use them.'
3
+
description: 'Understanding modules in MicroPython and how to use them.'
4
4
author: 'Pedro Lima'
5
5
tags: [MicroPython, Modules]
6
6
---
7
7
8
8
9
-
In this article, we’ll cover how modules work in MicroPython, explore a few built-in modules, and demonstrate how to install an external package like Modulino to extend our MicroPython project’s functionality.
9
+
In this guide, we’ll cover how modules work in MicroPython, explore a few built-in modules, and demonstrate how to install an external package like Modulino to extend our MicroPython project’s functionality.
10
10
11
11
## What Are Modules?
12
12
@@ -38,7 +38,7 @@ In this example, `time.sleep()` introduces a delay. Built-in modules like `time`
38
38
39
39
## External Modules
40
40
41
-
Some modules aren’t included with the default MicroPython installation and need to be installed separately. External modules, often provided by the community or specific hardware packages, extend MicroPython’s functionality. For example, the **Modulino** library is an external module that provides tools for working with Arduino Modulinos.
41
+
Some modules aren’t included with the default MicroPython installation and need to be installed separately. External modules, often provided by the community or specific hardware packages, extend MicroPython’s functionality. For example, the [Modulino library]() is an external module that provides tools for working with Arduino Modulinos.
42
42
43
43
To demonstrate how to use external modules, we’ll go through the steps to install the Modulino package on an Arduino board.
44
44
@@ -51,37 +51,41 @@ Before we can install external modules, we need to have MicroPython running on o
51
51
- Press the "Refresh" button if the board does not appear.
52
52
- Click "**Install MicroPython**" and wait for the installation to complete.
53
53
54
+
***For more details, visit the [MicroPython installation guide]()***
55
+
54
56
### Step 2: Install the Modulino Package
55
57
56
58
To install the Modulino package, we’ll use `mpremote`, a tool that allows us to install MicroPython packages directly onto our board from the command line.
57
59
58
-
1.**Install `mpremote`**: Make sure Python is installed on your computer, then open a terminal and type:
60
+
1. Make sure Python is installed on your computer
61
+
2. Open a terminal on your machine.
62
+
3. Run the following command to install `mpremote`.
59
63
60
64
```bash
61
65
pip install mpremote
62
66
```
63
67
64
-
2.**Connect to Your Board**: Find your board’s serial port by running:
68
+
4. With `mpremote` installed, run the following script to find our board's serial port.
3.**Install the Modulino Package**: Use the following command to install the Modulino package, replacing `<PORT>` with your board’s port:
82
+
5.Use the following command to install the Modulino package (or any other package we want to install), replacing `<PORT>` with our board’s port retrieved in the previous step.
4.**Verify Installation**: After installation, check Arduino Labs for MicroPython. You should see a `/lib` folder with the Modulino library inside, indicating a successful installation.
88
+
6.After the installation, open Arduino Labs for MicroPython, and connect your board. In the board's files, we should see a `/lib` folder with the Modulino library inside, indicating a successful installation.
0 commit comments