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 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.'
4
6
author: 'Pedro Lima'
5
-
tags: [MicroPython, Digital I/O]
7
+
hero_image: "./hero-banner.png"
8
+
6
9
---
7
10
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:
9
12
10
13
- Control outputs, such as turning an LED on and off.
11
14
- Read inputs, like detecting the state of a button.
@@ -15,35 +18,79 @@ Digital signals have two distinct values:
15
18
-**HIGH (1)**: Represents a voltage level close to the board's operating voltage (e.g., 3.3V or 5V).
16
19
-**LOW (0)**: Represents a voltage level close to 0V (ground).
17
20
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.
19
24
20
25
## Requirements
21
26
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:
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.]()
22
73
74
+
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***
23
75
24
76
## Digital Outputs
25
77
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.
27
79
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.
29
81
30
-
For this exercise, we will use the "Blink" example, where we turn an LED on and off at regular intervals.
82
+
### Circuit Diagram
31
83
32
-
**Components Needed:**
84
+
Connect an LED to the Arduino board, following the circuit diagram below:
33
85
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`.
38
88
39
-
**Circuit Diagram:**
89
+
![LED circuit.]()
40
90
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.***
45
92
46
-
**MicroPython Code:**
93
+
After completing the circuit diagram, copy the following code into your editor, and run the script.
47
94
48
95
```python
49
96
from machine import Pin
@@ -62,7 +109,7 @@ while True:
62
109
time.sleep(1) # Wait for 1 second
63
110
```
64
111
65
-
**Explanation:**
112
+
Let's take a look at what's included in this code example:
66
113
67
114
-**Import Modules**: We import `Pin` from `machine` and `time` for delays.
68
115
-**Initialize LED Pin**: Create a `Pin` object, setting the pin number and direction (`Pin.OUT`).
@@ -73,10 +120,13 @@ while True:
73
120
- The loop repeats indefinitely, causing the LED to blink.
74
121
75
122
123
+
76
124
## Digital Inputs
77
125
78
126
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
127
128
+
In this section, we will explain the different pull modes, and then try them out, by connecting a **pushbutton** to the Arduino.
129
+
80
130
### Understanding Pull Modes
81
131
82
132
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
88
138
89
139
![We can create a image here to explain that]()
90
140
91
-
92
-
93
-
### Pull-Up Mode
141
+
### Example: Pull-Up Mode
94
142
95
143
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
144
97
-
**Circuit Diagram for Pull-Up Mode:**
98
-
99
145
- Connect one side of the button to **GND**.
100
146
- Connect the other side to a digital input pin.
101
147
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.]()
115
149
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.
125
151
126
152
```python
127
153
from machine import Pin
@@ -139,7 +165,7 @@ while True:
139
165
time.sleep(0.1)
140
166
```
141
167
142
-
**Explanation:**
168
+
Let's take a look at what's included in this code example:
143
169
144
170
-**Initialize Button Pin**:
145
171
- 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:
151
177
- Reads the button state and prints a message accordingly.
152
178
- A short delay helps debounce the button.
153
179
180
+
### Example: Pull-Down Mode
154
181
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`).
155
183
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.
159
186
160
-
- Arduino board compatible with MicroPython
161
-
- Push-button switch
162
-
- Jumper wires
187
+
![Pull-down mode circuit.]()
163
188
164
-
**MicroPython Code:**
189
+
After completing the circuit diagram, copy the following code into your editor, and run the script.
165
190
166
191
```python
167
192
from machine import Pin
@@ -179,7 +204,7 @@ while True:
179
204
time.sleep(0.1)
180
205
```
181
206
182
-
**Explanation:**
207
+
Let's take a look at what's included in this code example:
183
208
184
209
-**Initialize Button Pin**:
185
210
- 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:
191
216
- Reads the button state and prints a message accordingly.
192
217
- A short delay helps debounce the button.
193
218
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