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 the basics for loops on MicroPython.'
2
+
title: 'Digital I/O'
3
+
description: 'A guide to digital inputs and outputs using MicroPython.'
6
4
author: 'Pedro Lima'
7
-
hero_image: "./hero-banner.png"
8
-
5
+
tags: [MicroPython, Digital I/O]
9
6
---
10
7
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:
12
9
13
10
- Control outputs, such as turning an LED on and off.
14
11
- Read inputs, like detecting the state of a button.
@@ -20,29 +17,77 @@ Digital signals have two distinct values:
20
17
21
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.
22
19
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:
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
+
23
73
## Digital Outputs
24
74
25
75
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.
26
76
27
-
### Code Example: Blinking an LED
28
-
29
77
Let's create the classic "Blink" example, where we turn an LED on and off at regular intervals.
30
78
31
-
**Components Needed:**
79
+
### Circuit Diagram
32
80
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`.
37
85
38
-
**Circuit Diagram:**
86
+
![LED circuit.]()
39
87
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.***
44
89
45
-
**MicroPython Code:**
90
+
After completing the circuit diagram, copy the following code into your editor, and run the script.
46
91
47
92
```python
48
93
from machine import Pin
@@ -61,22 +106,24 @@ while True:
61
106
time.sleep(1) # Wait for 1 second
62
107
```
63
108
64
-
**Explanation:**
109
+
Let's take a look at what's included in this code example:
65
110
66
111
-**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`).
68
113
-**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.
73
118
74
119
75
120
76
121
## Digital Inputs
77
122
78
123
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.
79
124
125
+
In this section, we will explain the different pull modes, and then try them out, by connecting a **pushbutton** to the Arduino.
126
+
80
127
### Understanding Pull Modes
81
128
82
129
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
88
135
89
136
![We can create a image here to explain that]()
90
137
91
-
92
-
93
-
### Pull-Up Mode
138
+
### Example: Pull-Up Mode
94
139
95
140
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`).
96
141
97
-
**Circuit Diagram for Pull-Up Mode:**
98
-
99
142
- Connect one side of the button to **GND**.
100
143
- Connect the other side to a digital input pin.
101
144
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.]()
115
146
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.
125
148
126
149
```python
127
150
from machine import Pin
@@ -139,7 +162,7 @@ while True:
139
162
time.sleep(0.1)
140
163
```
141
164
142
-
**Explanation:**
165
+
Let's take a look at what's included in this code example:
143
166
144
167
-**Initialize Button Pin**:
145
168
- 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:
151
174
- Reads the button state and prints a message accordingly.
152
175
- A short delay helps debounce the button.
153
176
177
+
### Example: Pull-Down Mode
154
178
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`).
155
180
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.
159
183
160
-
- Arduino board compatible with MicroPython
161
-
- Push-button switch
162
-
- Jumper wires
184
+
![Pull-down mode circuit.]()
163
185
164
-
**MicroPython Code:**
186
+
After completing the circuit diagram, copy the following code into your editor, and run the script.
165
187
166
188
```python
167
189
from machine import Pin
@@ -179,7 +201,7 @@ while True:
179
201
time.sleep(0.1)
180
202
```
181
203
182
-
**Explanation:**
204
+
Let's take a look at what's included in this code example:
183
205
184
206
-**Initialize Button Pin**:
185
207
- 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:
191
213
- Reads the button state and prints a message accordingly.
192
214
- A short delay helps debounce the button.
193
215
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