Skip to content

Commit bf0a8cb

Browse files
committed
Update digital-io.md
1 parent 88f6a8e commit bf0a8cb

File tree

1 file changed

+87
-56
lines changed
  • content/micropython/03.micropython/01.basics/00. digital-io

1 file changed

+87
-56
lines changed
Lines changed: 87 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
---
2-
title: 'Digital I/O'
3-
description: 'Learn about digital I/O with MicroPython.'
2+
3+
featured: micropython-101
4+
title: '2. Micropython Basics - Digital I/O'
5+
description: 'A guide to digital I/Oor loops on MicroPython.'
46
author: 'Pedro Lima'
5-
tags: [MicroPython, Digital I/O]
7+
hero_image: "./hero-banner.png"
8+
69
---
710

8-
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:
11+
Digital pins are fundamental for interacting with the physical world using your Arduino board. With them, you can:
912

1013
- Control outputs, such as turning an LED on and off.
1114
- Read inputs, like detecting the state of a button.
@@ -15,35 +18,79 @@ Digital signals have two distinct values:
1518
- **HIGH (1)**: Represents a voltage level close to the board's operating voltage (e.g., 3.3V or 5V).
1619
- **LOW (0)**: Represents a voltage level close to 0V (ground).
1720

18-
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, compared to analog signals.
21+
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.
22+
23+
In this chapter, we'll explore how to use digital pins in MicroPython.
1924

2025
## Requirements
2126

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

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

2476
## Digital Outputs
2577

26-
To control digital outputs in MicroPython, we use the `Pin` class from the `machine` module. Setting a pin as an output allows us to control devices like LEDs, relays, or other actuators.
78+
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.
2779

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

30-
For this exercise, we will use the "Blink" example, where we turn an LED on and off at regular intervals.
82+
### Circuit Diagram
3183

32-
**Components Needed:**
84+
Connect an LED to the Arduino board, following the circuit diagram below:
3385

34-
- Arduino board compatible with MicroPython
35-
- LED (optional if using the onboard LED)
36-
- Current-limiting resistor (e.g., 220Ω) if using an external LED
37-
- Jumper wires
86+
- Connect the anode (+) of the LED to a digital output pin.
87+
- Connect the cathode (-) of the LED through a resistor to `GND`.
3888

39-
**Circuit Diagram:**
89+
![LED circuit.]()
4090

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

46-
**MicroPython Code:**
93+
After completing the circuit diagram, copy the following code into your editor, and run the script.
4794

4895
```python
4996
from machine import Pin
@@ -62,7 +109,7 @@ while True:
62109
time.sleep(1) # Wait for 1 second
63110
```
64111

65-
**Explanation:**
112+
Let's take a look at what's included in this code example:
66113

67114
- **Import Modules**: We import `Pin` from `machine` and `time` for delays.
68115
- **Initialize LED Pin**: Create a `Pin` object, setting the pin number and direction (`Pin.OUT`).
@@ -73,10 +120,13 @@ while True:
73120
- The loop repeats indefinitely, causing the LED to blink.
74121

75122

123+
76124
## Digital Inputs
77125

78126
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.
79127

128+
In this section, we will explain the different pull modes, and then try them out, by connecting a **pushbutton** to the Arduino.
129+
80130
### Understanding Pull Modes
81131

82132
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 +138,16 @@ These internal resistors are built into the microcontroller and can be enabled i
88138

89139
![We can create a image here to explain that]()
90140

91-
92-
93-
### Pull-Up Mode
141+
### Example: Pull-Up Mode
94142

95143
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`).
96144

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

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-
148+
![Pull-up mode circuit.]()
115149

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:**
150+
After completing the circuit diagram, copy the following code into your editor, and run the script.
125151

126152
```python
127153
from machine import Pin
@@ -139,7 +165,7 @@ while True:
139165
time.sleep(0.1)
140166
```
141167

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

144170
- **Initialize Button Pin**:
145171
- We set up the pin as an input with a pull-up mode (`Pin.PULL_UP`), enabling the internal pull-up resistor.
@@ -151,17 +177,16 @@ while True:
151177
- Reads the button state and prints a message accordingly.
152178
- A short delay helps debounce the button.
153179

180+
### Example: Pull-Down Mode
154181

182+
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`).
155183

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

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

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

166191
```python
167192
from machine import Pin
@@ -179,7 +204,7 @@ while True:
179204
time.sleep(0.1)
180205
```
181206

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

184209
- **Initialize Button Pin**:
185210
- We set up the pin as an input with a pull-down mode (`Pin.PULL_DOWN`), enabling the internal pull-down resistor.
@@ -191,3 +216,9 @@ while True:
191216
- Reads the button state and prints a message accordingly.
192217
- A short delay helps debounce the button.
193218

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

0 commit comments

Comments
 (0)