Using I/O with MicroPython on the Pi Pico
The control of the inputs/outputs of the pins (GPIO) is done very quickly in MicroPython with the
object of the
module. To interact with a “physical” pin on the board, you will need to associate a “virtual Pin” object with it. Therefore, each physical pin will be described by a variable (more exactly an object) of the
type. It will suffice to execute functions of the
type on the “virtual” pin to modify the state of the “physical” pin.
So remember to import this submodule at the start of your Python script:
from machine import Pin
The configuration of an input/output pin is done when creating a
To configure an output pin (for example, for the Pi Pico built-in LED, a
object is created by setting the pin number and its output mode
pin_led = Pin (25, mode = Pin.OUT)
You can, if you wish, directly set an output voltage when creating the object:
pin_led = Pin(25, mode=Pin.OUT, value=1) # 3.3V en sortie -> LED will be ON pin_led = Pin(25, mode=Pin.OUT, value=0) # 0V en sortie -> LED will be OFF
By default, when creating a Pin object, the output voltage is 0V,
To configure an input pin, you must change the input
when creating the pin.
pin_24 = Pin(24, mode=Pin.IN)
It is possible to define the input type you want to have. You can choose between input with a pullup resistance, with a pulldown resistance or without a pull resistance. If these terms do not speak to you, I suggest learning the function and usefulness of pullup resistors (pullup, pulldown). We use the optional argument
to select the pulling resistance type.
pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_DOWN) # Input with a pull-down resistor pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_UP) # Input with a pullup resistor (the most used)
- MicroPython offers several functions to set an output voltage:
The function names are self-explanatory:
pin_led = Pin(25, mode=Pin.OUT) # Set a 3.3V outpout voltage (high logic state) pin_led.on() pin_led.high() pin_led.value(1) # Set a 0V (GND) outpout voltage (low logic state) pin_led.off() pin_led.low() pin_led.value(0)
Choose the functions couple that speak to you the most. For example,
to turn on LEDs and
for a more general case. The
function is useful when the value is stored in a
variable. For example :
pin_led = Pin(25, mode=Pin.OUT) output_state=1 pin_led.value(output_state) # 3.3V output output_state=0 pin_led.value(output_state) # 0V output
function lets you reverse the output state.
pin_led = Pin(25, mode=Pin.OUT, value=1) # 3.3V output pin_led.toggle() # 0V output
To read the state of a pin, i.e., either 3.3V for a high logic level and 0V for a low logic level, we use the same
function without specifying a value.
pin_24 = Pin(24, mode=Pin.IN, pull=Pin.PULL_UP) pin_24.value() # Returns 1 or 0 depending on the measured voltage
To practice the control of GPIO pins, here is a script that lights the built-in LED when a push-button is pressed. There are 2 variations of the script: one uses a digital input with a pullup resistor and another with a pulldown resistor.
We generally use pullup resistors rather than pulldown for the digital inputs.
Here is the circuit with the Pi Pico to make the MicroPython script work.
from machine import Pin pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_DOWN) pin_led = Pin(25, mode=Pin.OUT) while True: if pin_button.value() == 1: pin_led.on() else: pin_led.off()
Here is the circuit associated with the MicroPython script. You just have to connect the button to the ground instead of 3.3V.
from machine import Pin pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP) pin_led = Pin(25, mode=Pin.OUT) while True: if not pin_button.value(): pin_led.on() else: pin_led.off()
You will notice that we can condense
We can condense the code because an
condition can be written on only one line in Python:
from machine import Pin pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP) pin_led = Pin(25, mode=Pin.OUT) while True: pin_led.on() if not pin_button.value() else pin_led.off()
We could further simplify the code by completely removing the
even if we lose clarity.
from machine import Pin pin_button = Pin(14, mode=Pin.IN, pull=Pin.PULL_UP) pin_led = Pin(25, mode=Pin.OUT) while True: pin_led.value(not pin_button.value())
These minor optimizations can be critical if we want to run the code as quickly as possible because, as a reminder, MicroPython is very slow compared to the C/C ++ language.