Skip to content

Commit 3516f2b

Browse files
authored
Merge pull request #2266 from arduino/karlsoderby/review-micropython-basics
[PXCT-198] MicroPython basics review
2 parents 755c248 + 8786618 commit 3516f2b

File tree

3 files changed

+344
-162
lines changed

3 files changed

+344
-162
lines changed
Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
---
2-
3-
featured: micropython-101
4-
title: '2. Micropython Basics - Digital I/O'
5-
description: 'Learn the basics for loops on MicroPython.'
2+
title: 'Digital I/O'
3+
description: 'A guide to digital inputs and outputs using MicroPython.'
64
author: 'Pedro Lima'
7-
hero_image: "./hero-banner.png"
8-
5+
tags: [MicroPython, Digital I/O]
96
---
107

11-
Digital pins are fundamental for interacting with the physical world using your Arduino board. In this chapter, we'll explore how to use digital pins in MicroPython to:
8+
Digital pins are fundamental for interacting with the physical world using your Arduino board. With them, you can:
129

1310
- Control outputs, such as turning an LED on and off.
1411
- Read inputs, like detecting the state of a button.
@@ -20,29 +17,77 @@ Digital signals have two distinct values:
2017

2118
Although they can only represent two states, digital signals are highly useful. Being binary in nature, they directly interface with microcontrollers and processors, making them ideal for tasks requiring fast, on/off communication, such as reading sensors or controlling simple outputs. Their simplicity also gives them a natural resilience to electrical noise, as noise only disrupts digital signals when it is strong enough to cross the threshold between HIGH and LOW states. This makes them reliable for clear, consistent communication in various environments.
2219

20+
In this chapter, we'll explore how to use digital pins in MicroPython.
21+
22+
## Requirements
23+
24+
Before we start, let's check the requirements:
25+
26+
### MicroPython Compatible Arduino Boards
27+
28+
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:
29+
30+
- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
31+
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
32+
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
33+
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
34+
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
35+
- [Opta](https://store.arduino.cc/products/opta)
36+
- [Opta Wifi](https://store.arduino.cc/products/opta-wifi)
37+
- [Opta RS485](https://store.arduino.cc/products/opta-rs485)
38+
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
39+
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
40+
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
41+
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-rev2)
42+
- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense)
43+
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
44+
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)
45+
46+
### Hardware Components
47+
48+
In this guide, we will be using some additional electronic components:
49+
- LEDs (optional if using the onboard LED)
50+
- Current-limiting resistor (e.g. 220Ω) if using an external LED
51+
- Jumper wires
52+
- Pushbutton
53+
- Breadboard
54+
55+
### Software Requirements
56+
57+
- [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.
58+
59+
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***
60+
61+
## Board and Editor Setup
62+
63+
1. Open the [Arduino Lab for MicroPython]() application.
64+
2. Plug the Arduino board into the computer using a USB cable.
65+
![Connect board to computer.]()
66+
3. Press the connection button on the top left corner of the window.
67+
![Connect the editor to the board.]()
68+
4. The connected Arduino board should appear, and we can click it:
69+
![Select board.]()
70+
71+
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***
72+
2373
## Digital Outputs
2474

2575
To control digital outputs in MicroPython, we use the `Pin` class from the `machine` module. Setting a pin as an output allows you to control devices like LEDs, relays, or other actuators.
2676

27-
### Code Example: Blinking an LED
28-
2977
Let's create the classic "Blink" example, where we turn an LED on and off at regular intervals.
3078

31-
**Components Needed:**
79+
### Circuit Diagram
3280

33-
- Arduino board compatible with MicroPython
34-
- LED (optional if using the onboard LED)
35-
- Current-limiting resistor (e.g., 220Ω) if using an external LED
36-
- Jumper wires
81+
Connect an LED to the Arduino board, following the circuit diagram below:
82+
83+
- Connect the anode (+) of the LED to a digital output pin.
84+
- Connect the cathode (-) of the LED through a resistor to `GND`.
3785

38-
**Circuit Diagram:**
86+
![LED circuit.]()
3987

40-
- **Onboard LED**: Many Arduino boards have an onboard LED connected to a specific pin.
41-
- **External LED**:
42-
- Connect the anode (+) of the LED to a digital output pin.
43-
- Connect the cathode (-) of the LED through a resistor to `GND`.
88+
***You can also use the built-in LED on your board, if you do not have an external LED.***
4489

45-
**MicroPython Code:**
90+
After completing the circuit diagram, copy the following code into your editor, and run the script.
4691

4792
```python
4893
from machine import Pin
@@ -61,22 +106,24 @@ while True:
61106
time.sleep(1) # Wait for 1 second
62107
```
63108

64-
**Explanation:**
109+
Let's take a look at what's included in this code example:
65110

66111
- **Import Modules**: We import `Pin` from `machine` and `time` for delays.
67-
- **Initialize LED Pin**: Create a `Pin` object, setting the pin number and direction (`Pin.OUT`).
112+
- **Initialize LED Pin**: with the `Pin` object, we set the pin number and direction (`Pin.OUT`).
68113
- **Main Loop**:
69-
- `led.value(1)`: Sets the pin to HIGH, turning the LED on.
70-
- `time.sleep(1)`: Pauses the program for 1 second.
71-
- `led.value(0)`: Sets the pin to LOW, turning the LED off.
72-
- The loop repeats indefinitely, causing the LED to blink.
114+
- `led.value(1)` - Sets the pin to HIGH, turning the LED on.
115+
- `time.sleep(1)` - Pauses the program for 1 second.
116+
- `led.value(0)` - Sets the pin to LOW, turning the LED off.
117+
- `while True:` - The loop repeats indefinitely, causing the LED to blink.
73118

74119

75120

76121
## Digital Inputs
77122

78123
Reading digital inputs allows your program to respond to external events, like button presses or sensor signals. In MicroPython, we use the `Pin` class to set up pins as inputs, and we can specify pull modes to stabilize the input readings.
79124

125+
In this section, we will explain the different pull modes, and then try them out, by connecting a **pushbutton** to the Arduino.
126+
80127
### Understanding Pull Modes
81128

82129
When a digital input pin is not connected to a definite HIGH or LOW voltage, it is said to be "floating," which can result in unreliable readings due to electrical noise. To prevent this, we use internal pull-up or pull-down resistors, activated by specifying the pull mode in the `Pin` constructor.
@@ -88,40 +135,16 @@ These internal resistors are built into the microcontroller and can be enabled i
88135

89136
![We can create a image here to explain that]()
90137

91-
92-
93-
### Pull-Up Mode
138+
### Example: Pull-Up Mode
94139

95140
In pull-up mode, the input pin is internally connected to a HIGH voltage level. When the input device (like a button) is activated and connects the pin to GND, the pin reads LOW (`0`).
96141

97-
**Circuit Diagram for Pull-Up Mode:**
98-
99142
- Connect one side of the button to **GND**.
100143
- Connect the other side to a digital input pin.
101144

102-
![Demo]() TODO: Show Schematic
103-
104-
### Pull-Down Mode
105-
106-
In pull-down mode, the input pin is internally connected to GND. When the input device is activated and connects the pin to a HIGH voltage level (e.g., 3.3V), the pin reads HIGH (`1`).
107-
108-
**Circuit Diagram for Pull-Down Mode:**
109-
110-
- Connect one side of the button to **3.3V** (or **5V**, depending on your board's logic level).
111-
- Connect the other side to a digital input pin.
112-
113-
![Demo]() TODO: Show Schematic
114-
145+
![Pull-up mode circuit.]()
115146

116-
### Code Example: Reading a Button with Pull-Up Mode
117-
118-
**Components Needed:**
119-
120-
- Arduino board compatible with MicroPython
121-
- Push-button switch
122-
- Jumper wires
123-
124-
**MicroPython Code:**
147+
After completing the circuit diagram, copy the following code into your editor, and run the script.
125148

126149
```python
127150
from machine import Pin
@@ -139,7 +162,7 @@ while True:
139162
time.sleep(0.1)
140163
```
141164

142-
**Explanation:**
165+
Let's take a look at what's included in this code example:
143166

144167
- **Initialize Button Pin**:
145168
- We set up the pin as an input with a pull-up mode (`Pin.PULL_UP`), enabling the internal pull-up resistor.
@@ -151,17 +174,16 @@ while True:
151174
- Reads the button state and prints a message accordingly.
152175
- A short delay helps debounce the button.
153176

177+
### Example: Pull-Down Mode
154178

179+
In pull-down mode, the input pin is internally connected to GND. When the input device is activated and connects the pin to a HIGH voltage level (e.g., 3.3V), the pin reads HIGH (`1`).
155180

156-
### Code Example: Reading a Button with Pull-Down Mode
157-
158-
**Components Needed:**
181+
- Connect one side of the button to **3.3V** (or **5V**, depending on your board's logic level).
182+
- Connect the other side to a digital input pin.
159183

160-
- Arduino board compatible with MicroPython
161-
- Push-button switch
162-
- Jumper wires
184+
![Pull-down mode circuit.]()
163185

164-
**MicroPython Code:**
186+
After completing the circuit diagram, copy the following code into your editor, and run the script.
165187

166188
```python
167189
from machine import Pin
@@ -179,7 +201,7 @@ while True:
179201
time.sleep(0.1)
180202
```
181203

182-
**Explanation:**
204+
Let's take a look at what's included in this code example:
183205

184206
- **Initialize Button Pin**:
185207
- We set up the pin as an input with a pull-down mode (`Pin.PULL_DOWN`), enabling the internal pull-down resistor.
@@ -191,3 +213,9 @@ while True:
191213
- Reads the button state and prints a message accordingly.
192214
- A short delay helps debounce the button.
193215

216+
## Summary
217+
218+
In this guide, we have looked at different ways of interacting with digital pins on an Arduino, using MicroPython:
219+
- How to use digital outputs (turning on/off an LED)
220+
- How the different pull modes work (`PULL_DOWN`, `PULL_UP`)
221+
- How to read a button press using either pull modes.

0 commit comments

Comments
 (0)