Skip to content

uPesy only delivers currently in France.

Contents Menu Expand Light mode Dark mode Auto light/dark mode

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 Pin object of the machine 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 Pin type. It will suffice to execute functions of the mon_pin.une_fonction() 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

Configuration of pins in input or output with MicroPython

The configuration of an input/output pin is done when creating a Pin object.

To configure an output pin (for example, for the Pi Pico built-in LED, a Pin object is created by setting the pin number and its output mode (OUT) :

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, value is 0.

To configure an input pin, you must change the input mode to (IN) 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 pull 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)

Different ways in MicroPython to read or set a voltage

MicroPython offers several functions to set an output voltage:
  • Pin.on() and Pin.off()

  • Pin.high() and Pin.low()

  • Pin.value(value)

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)

Note

Choose the functions couple that speak to you the most. For example, .on ()|.off() to turn on LEDs and high ()|.low() for a more general case. The Pin.value() function is useful when the value is stored in a Pin.value(my_variable) 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

The Pin.toggle() 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 Pin.value() 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

Project: Turn on the LED when a push-button is pressed

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.

Note

We generally use pullup resistors rather than pulldown for the digital inputs.

With the internal pulldown resistance of the digital input

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()

With the internal pullup resistor of the digital input

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()

Note

You will notice that we can condense pin_button.value () == 1 by pin_button.value() in the if condition.

We can condense the code because an if 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 if 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())

Note

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.

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy