Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PXCT-198] MicroPython New Documentation #2260

Open
wants to merge 96 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
d0182e8
First steps
pedromsousalima Oct 9, 2024
5ebc29f
Review MicroPython - Introduction
karlsoderby Oct 15, 2024
05eee48
Review MicroPython - First Script
karlsoderby Oct 15, 2024
0904d57
Merge pull request #2242 from arduino/karlsoderby/rev-micropython-fir…
karlsoderby Oct 17, 2024
c0983d3
mp loop guide
pedromsousalima Oct 17, 2024
5964719
Added articles
pedromsousalima Oct 21, 2024
4129e6c
Fix folder structure
karlsoderby Oct 28, 2024
d8bbc22
Update structure
karlsoderby Oct 28, 2024
b445a26
basics update
karlsoderby Oct 28, 2024
3571dcc
Added installation
pedromsousalima Oct 28, 2024
004c803
Analog IO added
pedromsousalima Oct 28, 2024
a2fc7d0
Linter
pedromsousalima Oct 29, 2024
eda5000
Added info on offline IDE
pedromsousalima Oct 29, 2024
6238558
Update loops
pedromsousalima Oct 29, 2024
3c9d0ab
Linter fix
pedromsousalima Oct 29, 2024
53676e5
REPL added
pedromsousalima Oct 29, 2024
755c248
Fix linter
pedromsousalima Oct 30, 2024
13cde66
review micropython installation
karlsoderby Oct 31, 2024
4c6016d
review first steps complete
karlsoderby Oct 31, 2024
88f6a8e
Update digital-io.md
karlsoderby Oct 31, 2024
bf0a8cb
Update digital-io.md
karlsoderby Oct 31, 2024
66a79a3
Update digital-io.md
karlsoderby Oct 31, 2024
a96bff6
Update installing-micropython.md
karlsoderby Oct 31, 2024
b040193
Update digital-io.md
karlsoderby Oct 31, 2024
d35a093
Update analog-io.md
karlsoderby Oct 31, 2024
fe46696
review loops pt1
karlsoderby Oct 31, 2024
5f9be60
add modulino micropython docs
Hannes7eicher Nov 4, 2024
8786618
loops pt2
karlsoderby Nov 4, 2024
b921ab2
REPL review
karlsoderby Nov 4, 2024
a7a75b6
Update repl.md
karlsoderby Nov 4, 2024
5e53628
Update content/micropython/03.micropython/02.enviroment/01.repl/repl.md
karlsoderby Nov 5, 2024
3516f2b
Merge pull request #2266 from arduino/karlsoderby/review-micropython-…
karlsoderby Nov 5, 2024
2a97e65
Update installation.md
karlsoderby Nov 5, 2024
c2c5957
Merge pull request #2262 from arduino/karlsoderby/micropython-review-2
karlsoderby Nov 5, 2024
129c995
Merge pull request #2267 from arduino/karlsoderby/repl-review
karlsoderby Nov 5, 2024
f025bbc
update installation tutorial
karlsoderby Nov 5, 2024
236ea55
Update installation.md
karlsoderby Nov 5, 2024
806a1ac
Distance+pixels
karlsoderby Nov 5, 2024
132992a
buzzer
karlsoderby Nov 5, 2024
1008daf
buzzer, buttons, knob, movement, thermo
karlsoderby Nov 5, 2024
3eacc47
update frontmatters
karlsoderby Nov 5, 2024
c238e04
Merge pull request #2268 from arduino/karlsoderby/modulino-review
Hannes7eicher Nov 5, 2024
917e07f
Merge pull request #2265 from arduino/hannes7eicher/Modulino-MicroPython
karlsoderby Nov 5, 2024
ec287b0
Added Code Editor info
pedromsousalima Nov 5, 2024
c1b22fb
Merge branch 'content/micropython/micropython-revamp' of https://gith…
pedromsousalima Nov 5, 2024
cdbd551
Added "File System"
pedromsousalima Nov 5, 2024
47f140b
Added Module information
pedromsousalima Nov 5, 2024
4484c48
Create micropython.md
karlsoderby Nov 6, 2024
e4cf23e
remove link
karlsoderby Nov 6, 2024
a920267
remove links
karlsoderby Nov 6, 2024
41cd4ac
Merge branch 'content/micropython/micropython-revamp' of https://gith…
pedromsousalima Nov 6, 2024
98c3d71
add to sidebar
karlsoderby Nov 6, 2024
3ffa4f3
rename folder
karlsoderby Nov 6, 2024
962fba4
review file system
karlsoderby Nov 6, 2024
36f27ae
review modules
karlsoderby Nov 6, 2024
0d307ec
review code editor
karlsoderby Nov 6, 2024
cf3a453
add boards
karlsoderby Nov 11, 2024
76c49f0
Update nano-rp2040-connect.md
karlsoderby Nov 11, 2024
f545c55
Merge pull request #2279 from arduino/karlsoderby/board-specific-guides
pedromsousalima Nov 11, 2024
195400b
Merge pull request #2271 from arduino/karlsoderby/review-ecosystem
karlsoderby Nov 13, 2024
0ff5ea4
Add online editor guide
karlsoderby Nov 13, 2024
94ef4f2
Add datalogger tutorial
karlsoderby Nov 13, 2024
e7c72f5
Create reference.md
karlsoderby Nov 13, 2024
3038b06
Runtime added
pedromsousalima Nov 14, 2024
e579f79
Small improvments
pedromsousalima Nov 14, 2024
d03df6f
Update content/micropython/03.micropython/02.enviroment/00.online-edi…
karlsoderby Nov 18, 2024
05c6333
Update content/micropython/03.micropython/02.enviroment/00.online-edi…
karlsoderby Nov 18, 2024
5d9f72e
Added nano esp 32
pedromsousalima Nov 18, 2024
e27e94b
Eddited Code Editor
pedromsousalima Nov 19, 2024
1073ce0
Merge pull request #2285 from arduino/karlsoderby/move-reference
karlsoderby Nov 20, 2024
7e6c888
Merge pull request #2283 from arduino/karlsoderby/online-editor
karlsoderby Nov 20, 2024
6e14a2f
Merge pull request #2284 from arduino/karlsoderby/csv-storage
karlsoderby Nov 20, 2024
bc3bfbd
installation + intro
karlsoderby Nov 20, 2024
b35e41e
first sketch
karlsoderby Nov 20, 2024
f2ab2e4
foldername changes
karlsoderby Nov 20, 2024
40e03e3
basics
karlsoderby Nov 20, 2024
e09cd2c
Merge pull request #2293 from arduino/karlsoderby/graphics-update
karlsoderby Nov 20, 2024
f711f1f
Update content/micropython/03.micropython/02.enviroment/00.online-edi…
pedromsousalima Nov 21, 2024
ca182f3
Update content/micropython/03.micropython/02.enviroment/00.online-edi…
pedromsousalima Nov 21, 2024
010af8b
Update giga-r1-wifi.md
pedromsousalima Nov 21, 2024
7b1d750
Essentials created
pedromsousalima Nov 21, 2024
b24c796
add how it works section
Hannes7eicher Nov 25, 2024
4f549f8
Merge branch 'content/micropython/micropython-revamp' of https://gith…
pedromsousalima Nov 26, 2024
7f8c06e
move online editor folder
karlsoderby Nov 26, 2024
fa82087
fix folder names
karlsoderby Nov 26, 2024
b0e340b
smaller updates
karlsoderby Nov 26, 2024
b619a25
Update essentials.md
karlsoderby Nov 26, 2024
85e5a9d
add pinout and fix article
karlsoderby Nov 26, 2024
8e78dac
board examples update
karlsoderby Nov 26, 2024
5f4e3b7
add img rp2040
karlsoderby Nov 26, 2024
f9079e3
Merge pull request #2300 from arduino/karlsoderby/ble-sense
karlsoderby Nov 26, 2024
239d572
Improved snippet
pedromsousalima Nov 28, 2024
e08df26
Merge pull request #2299 from arduino/karlsoderby/review-essentials
pedromsousalima Dec 2, 2024
95f3074
Graphics added
pedromsousalima Dec 3, 2024
28b2109
Added Installer graphics
pedromsousalima Dec 4, 2024
ad6dd60
Added imaged file system
pedromsousalima Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ Now that all the previous steps have been set, let's see how to make Alvik movin
``` python
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys

alvik = ArduinoAlvik()
alvik.begin()
sleep_ms(5000) # waiting for the robot to setup
distance = 12
distance = 15
degrees = 45.00
speed = 10.00

while (True):

distance_l, distance_cl, distance_c, distance_r, distance_cr = alvik.get_distance()
sleep_ms(50)
print(distance_c)

if distance_c < distance:
Expand All @@ -84,6 +81,7 @@ while (True):
else:
alvik.drive(speed, 0.0, linear_unit='cm/s')

sleep_ms(100)
```

**4. **Connect Alvik to your PC using the cable included in the box, under the tray.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: 'Introduction to MicroPython'
description: 'Learn about the fundamentals of Micropython on Arduino boards.'
author: 'Pedro Lima'
tags: [MicroPython, Introduction]
micropython_type: test
---

MicroPython is a lightweight implementation of Python 3 designed to run on microcontrollers and embedded systems. Think of it as a mini-version of Python, tailored for hardware with limited resources like memory and processing power. Despite its smaller size, MicroPython retains the simplicity and flexibility of Python, making it an excellent option for programming hardware.

## MicroPython on Arduino Boards

![MicroPython with Arduino](assets/micropython-arduino.png)

When using MicroPython on Arduino boards, the software is first installed on your Arduino. This allows the board to interpret and run Python code. Once MicroPython is installed on your board (don't worry, we'll cover this [here]()), you can start writing and executing Python scripts instantly.

Unlike traditional development approaches, where you compile code and then flash it to the board, with MicroPython you write Python scripts and run them instantly on your Arduino. This makes the development process much faster and more interactive.

## Running Programs in MicroPython

Once MicroPython is installed, you can start programming by writing scripts and uploading them to the board. These scripts are interpreted in real-time, meaning you can make quick changes and see immediate results, streamlining the development process.

![Running a script.](assets/run-script.gif)

MicroPython also includes a simple file system where your scripts are stored. For example, when you write a script, it is saved directly on the board and can be executed immediately without compiling. You can also save other scripts that can be activated from the main script!

### How it Works

The MicroPython installation includes several key components:

1. **File System**: MicroPython has a small file system built into the microcontroller. You can store Python scripts and configuration files on the board itself. Common files include:
- `main.py`: This script runs automatically when the board boots up. It's where you can put the main logic of your program.
- `boot.py`: This script runs before `main.py` and is often used for setting up configurations like WiFi connections or hardware initialization.

These files are fully editable, allowing you to control how your board starts and operates.

2. **Base Modules**: MicroPython comes with built-in modules for working with hardware like pins, sensors, and communication protocols (I2C, SPI, etc.). This includes essential modules like `machine`, `network`, and `time`.

## How to Program for MicroPython

Programming in MicroPython involves writing Python scripts in a text editor and then running them on your board. For this, we can use the [Arduino Lab for MicroPython]().

When writing MicroPython code, it's essential to think in terms of **modularity**. A good practice is to break down your code into smaller, reusable modules rather than writing everything in one large file. This approach makes it easier to manage and maintain code, especially for larger projects.

### Structuring Your Code

1. **Main Logic**: This goes into the `main.py` file. You can think of it as your "sketch" in Arduino terms.
2. **Helper Modules**: Break down your code into smaller modules for specific tasks, such as controlling a sensor or managing a display. These modules can be imported into `main.py` as needed.
3. **Interrupts and Background Tasks**: If you're dealing with hardware, you may also need to work with interrupts or periodic tasks, which can be handled in dedicated modules.

## MicroPython vs. C++ for Electronics Projects

MicroPython offers a different approach to programming compared to the traditional C++ used in Arduino development. Here are a few key comparisons:

- **Ease of Use**: Python’s syntax is generally more accessible for beginners. It is less verbose and easier to read, which can speed up the learning process.
- **Real-Time Interactivity**: With MicroPython, you can write and test code interactively, without needing to compile. This makes it faster to experiment and troubleshoot hardware setups.
- **Resource Efficiency**: C++ is more efficient in terms of memory and speed, making it a better option for projects that need to squeeze every bit of performance out of the hardware. MicroPython, on the other hand, prioritizes ease of development over raw performance, but it is still capable of handling many common hardware tasks.

## Summary

In summary, MicroPython provides a powerful and flexible way to develop electronic projects, especially for those familiar with Python. Its ability to run on microcontrollers like Arduino boards makes it an attractive option for both beginners and experienced developers who want a fast and efficient workflow.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
featured: micropython-101
title: 'Installing MicroPython'
description: 'Learn how to install MicroPython on your Arduino board.'
author: 'Pedro Lima'
tags: [MicroPython, Installation]
micropython_type: test
---

# Installing MicroPython

In this article, we will go through the necessary tools needed to install and run MicroPython on an Arduino board. By the end of this guide, we will be ready to write and run our first MicroPython script. Let's get started!

## Requirements

Before we start, let's check the requirements:

### MicroPython Compatible Arduino Boards

MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:

- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
- [Opta](https://store.arduino.cc/products/opta)
- [Opta Wifi](https://store.arduino.cc/products/opta-wifi)
- [Opta RS485](https://store.arduino.cc/products/opta-rs485)
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-rev2)
- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense)
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)


### Software Requirements

- [MicroPython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) - the firmware installer is needed to install MicroPython on our Arduino board.
- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board.

***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***

## Install MicroPython

1. First, download the [Micropython Firmware Installer](https://labs.arduino.cc/en/labs/micropython-installer) and launch it.
2. Connect your board to your computer, it should be recognized by the installer.
![Arduino Nano ESP32 detected!](./assets/select-board.png)
3. Press **INSTALL MICROPYTHON**. A loading animation will appear.

Once the firmware is installed a "Installation successful" message will appear. At this point you can safely close the installer as your board is now ready for tinkering!
![Firmware Successfully Uploaded!](./assets/installation-success.png)

## Editor Setup

After installing MicroPython on your board, we will need an editor to write code. We now have the option to choose the **offline** or **online** option.

### Offline Setup

First, download the latest version of [Arduino Lab for MicroPython](https://labs.arduino.cc/en/labs/micropython) for your operative system. See instructions for MacOS / Windows below:

#### MacOS

Unzip the downloaded file, and run the application.

#### Windows

Unzip the downloaded file, and run the executable file (`.exe`).

### Online Setup

Alternatively, we can use the IDE without the need of installing anything. Simply visit the link below:
- [Arduino Lab for MicroPython (online)](https://lab-micropython.arduino.cc/).

## Connecting Board and IDE

At this point in the tutorial, we have
- Installed MicroPython on our board
- Downloaded and installed the editor (or opted for the online version).

We will now try out running a script on the board, to make sure things are working properly.

1. Plug the Arduino board into the computer using a USB cable.
![Connect board to computer.](assets/usb-comp.png)
2. Press the connection button on the top left corner of the window. The connected Arduino board should appear (by its port name), and we can click it:
![Connect to the board in the editor.](assets/select-board-ide.png)

We have now set up all necessary steps for running a script!

## Running a Test Script

With the installation and setup complete, let's try out running a very simple script: **Hello World!**

1. In the text area field of the editor, write `print("Hello World!")`, and then click on the play symbol (RUN).
![Write hello world.](assets/write-code-run.png)
2. After running it, you should see `Hello World!` in the black box. This is the board sending the **"Hello World!"** back to you, because the script is run on the board, not on the computer. This means everything is successful, and you are ready to start writing MicroPython scripts!
![Hello world from the board.](assets/repl-print.png)

## Troubleshooting

If you run into any issues during installation, here are some common problems and solutions:

- **Board Not Detected** - if you cannot locate your board, ensure that your board is properly connected and the correct USB drivers are installed. Try using a different USB cable, or double tap the reset button on the board. If a light starts fading, it means it is in default mode, and is ready to be installed!

- **Unable to Install Firmware** - double-check that the MicroPython Firmware Installer was able to install the firmware and that your board is compatible (it will not show up on the installer if it is not). Also, verify that no other software is using the same serial port.

## Summary

By following these steps, you should be able to successfully install MicroPython on your Arduino board and run your first script.

### Next Steps

This tutorial is **Part Two** of the **"First Steps"** series. We recommend you following the next tutorial that will allow you to control the Arduino board's LED.
- [First Steps - Part Three]()


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---
title: 'My First Script'
description: 'Learn how to write a basic MicroPython script to blink an LED.'
author: 'Pedro Lima'
tags: [MicroPython, Blink]
micropython_type: test
---

In this tutorial, we will create our very first MicroPython script that will run on an Arduino board. Starting of simple, we will make an LED blink, a classic beginner project that will get us familiar with the MicroPython programming environment.

## Requirements

Before we start, let's check the requirements:

### MicroPython Compatible Arduino Boards

MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:

- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
- [Opta](https://store.arduino.cc/products/opta)
- [Opta Wifi](https://store.arduino.cc/products/opta-wifi)
- [Opta RS485](https://store.arduino.cc/products/opta-rs485)
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-rev2)
- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense)
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)


### Software Requirements

- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board.

***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***

## Board and Editor Setup

1. Open the [Arduino Lab for MicroPython]() application.
2. Plug the Arduino board into the computer using a USB cable.
![Connect board to computer.](assets/usb-comp.png)
3. Press the connection button on the top left corner of the window. The connected Arduino board should appear (by its port name), and we can click it:
![Connect to the board in the editor.](assets/select-board-ide.png)

***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***

## First Script (LED Blink)

Once your board is connected, we can start writing code! Below you will find a basic example, that will flash the built in LED on your board every second.

1. First, open the `main.py` file on your board. We write in this file, because once saved, the code will run even if you reset the board.
![Open main.py file.](assets/open-files.png)

2. Copy and paste the following code into your editor:
```python
import machine
import time

# The pin used for built-in LED varies
# GIGA: 0 (green), Nano ESP32: 0 (green), Nano RP2040 Connect: 25
# Nano BLE Sense: 13
led = machine.Pin(25, machine.Pin.OUT)

while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
```

***Note: The built-in LED pin varies from board to board. For example, on the Arduino Nano RP2040 Connect, the built-in LED is on pin `25`.***

3. Click the **Run** button in your editor to transfer the script to your board.
![Run the script.](assets/run-script.png)

Once the script is running, the LED on the board should start blinking at one-second intervals. This means our MicroPython script has loaded successfully.

![LED blinking on your board.](assets/blink.gif)

### Code Breakdown

Let's take a look at the code, line by line, to understand what is happening:

- **Import Modules**:

```python
import machine
import time
```

We import the `machine` and `time` modules to access hardware functions and time delays.

- **Initialize the LED Pin**:

```python
led = machine.Pin(25, machine.Pin.OUT)
```

We create a `Pin` object named `led`, set to pin number `25`, and configure it as an output.

- **Infinite Loop**:

```python
while True:
led.value(1)
time.sleep(1)
led.value(0)
time.sleep(1)
```

Inside the loop, we:

- Turn the LED on by setting its value to `1`.
- Wait for 1 second.
- Turn the LED off by setting its value to `0`.
- Wait for another second.
- Repeat the cycle.

## Understanding Programming Concepts

Let's break down the key programming concepts used in this script:

### `machine` Module

The machine module is a built-in MicroPython library that provides direct access to your board's hardware components. It allows you to control and interact with the microcontroller's features, such as:

- **Pins:** Configure and control digital and analog pins.
- **Timers:** Set up timers for scheduling tasks.
- **Communication Interfaces:** Use protocols like I2C, SPI, and UART.
- **Hardware-Specific Functions:** Access features unique to your microcontroller.

In our script, we use the `machine.Pin` class to interact with a specific pin on the board. By creating a `Pin` object, we can control the voltage level of that pin, which in turn controls the LED.

### `time` Module

The time module provides functions for managing time-related tasks. It allows you to add delays, measure time intervals, and schedule events. Key functions include:

- `time.sleep(seconds)` Pauses the execution of your script for the specified number of seconds. It accepts floating-point numbers for sub-second delays.

### `while True` Loop

A `while True` loop creates an infinite loop, allowing the code inside it to run repeatedly. This is essential for tasks that need to run continuously, like blinking an LED.

## Modification: Make the LED Blink Faster

Let's modify the script to make the LED blink faster. We'll change the delay from 1 second to 0.2 seconds.

### Modified Code

```python
import machine
import time

led = machine.Pin(25, machine.Pin.OUT)

while True:
led.value(1)
time.sleep(0.5) # 0.5 seconds
led.value(0)
time.sleep(0.5) # 0.5 seconds
```

### Steps

1. Change the `time.sleep(1)` lines to `time.sleep(0.5)`.
2. Upload the modified script to your board.
3. Observe that the LED now blinks faster, turning on and off every 0.5 seconds.

## Summary

Great work! We have now written and modified our first MicroPython script on an Arduino board. This exercise introduced:

- Importing modules (`machine`, `time`)
- Initializing hardware components (LED)
- Using loops (`while`)
- Controlling time delays (`time.sleep()`)

These concepts are key for a vast majoraty of operations we can perform while writing MicroPython programs.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: First Steps
---
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading