Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)

Learn how to set the Raspberry Pi Pico GPIOs as digital inputs and digital outputs using MicroPython firmware. As an example, you’ll learn how to read the value of a pushbutton and light up an LED accordingly. With this simple example, you’ll learn how to read any digital input and control any digital output.

Raspberry Pi Pico Control Digital Outputs and Read Digital Inputs with MicroPython

We have a similar guide using Arduino IDE: Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE).

Prerequisites

To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.

The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash micropython firmware, and upload code to the board.

Project Overview

To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.

Control Digital Outputs and Read Digital Inputs pushbutton pressed led on off

Raspberry Pi Pico GPIOs

The Raspberry Pi Pico comes with 40 pins, 26 of which are programmable GPIOs that you can use to connect peripherals.

You can use the following pinouts as a reference to identify and locate each GPIO on your board. The pinout is slightly different for the Pico and Pico W.

The following picture shows the Raspberry Pi Pico pinout (which functions are supported by each pin).

Raspberry Pi Pico Pinout
Image source: raspberrypi.com
Raspberry Pi Pico W pinout

The pins marked in red are power pins that output 3.3V. The black pins are GND pins. All pins in light green can be used as “regular” GPIOs (input and output).

Digital Inputs

To get the value of a GPIO, first, you need to create a Pin object and set it as an input. For example:

button = Pin(21, Pin.IN)

This creates a Pin object called button on GPIO 21. So, when you use this line of code, you’re telling your program that GPIO 21 should act as an input (Pin.IN).

You also have the option to tell the inputs to use the microcontroller’s internal pull-up or pull-down resistor. For a pushbutton, it’s useful to use the internal pull-down resistor. For that, you can add a third parameter to the Pin() constructor (PULL_DOWN).

button = Pin(21, Pin.IN, Pin.PULL_DOWN)

Depending on the way you’re wiring your pushbutton, you may want to use the internal pull-up resistor, in that case:

button = Pin(21, Pin.IN, Pin.PULL_UP)

After declaring it as an input, to get its value, you need to use the value() method on the Pin object without passing any argument. For example, to get the state of a Pin object called button, use the following expression:

button.value()

We’ll show you in more detail how everything works in the project example.

Digital Outputs

To set a GPIO on or off, first, you need to set it as an output. For example:

led = Pin(20, Pin.OUT)

This creates a Pin object called led on GPIO 20. So, when you use this line of code, you’re telling your program that GPIO 20 should act as an output (Pin.OUT).

To control the GPIO, use the value() method on the Pin object and pass 1 or 0 as an argument. For example, the following command sets a Pin object (led) to HIGH—turns the led on:

led.value(1)

To set the GPIO to LOW, pass 0 as an argument—turns the led off:

led.value(0)

Schematic

Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 20 and the pushbutton to GPIO 21.

Parts Required

Here’s a list of the parts you need to build the circuit:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic – Raspberry Pi Pico

You can use the following diagram as a reference to connect the pushbutton and LED to the Raspberry Pi Pico board.

Raspberry Pi Pico Pushbutton LED schematic diagram

Script

The following code reads the state of the pushbutton and lights up the LED accordingly.

#  Rui Santos
#  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-outputs-inputs-micropython/

from machine import Pin
from time import sleep

led = Pin(20, Pin.OUT)
button = Pin(21, Pin.IN, Pin.PULL_DOWN)

while True:
  led.value(button.value())
  sleep(0.1)
  print(button.value())

View raw code

How the Code Works

You start by importing the Pin class from the machine module, and the sleep class from the time module.

from machine import Pin
from time import sleep

Then, create a Pin object called led on GPIO 20. LEDs are outputs, so pass as second argument Pin.OUT.

led = Pin(20, Pin.OUT)

We also create an object called button on GPIO 21. Buttons are inputs, and we want to use the internal pull-down resistor, so we declare it as follows.

button = Pin(21, Pin.IN, Pin.PULL_DOWN)

Use button.value() to return/read the button state. Then, pass the button.value() expression as an argument to the LED value.

led.value(button.value())

This way, when we press the button, button.value() returns 1. So, this is the same as having led.value(1).

This sets the LED state to 1, lighting up the LED. When the pushbutton is not being pressed, button.value() returns 0. So, we have led.value(0), and the LED stays off.

Demonstration

Save the code to your Raspberry Pi Pico board using Thonny IDE or any other MicroPython IDE of your choice.

Follow the next instructions if you’re using Thonny IDE.

Copy the code provided to the Thonny IDE untitled file.

Thonny IDE Inputs and Outputs MicroPython Raspberry Pi Pico

With the code copied to the file, click on the Save icon. Then, select Raspberry Pi Pico.

Saving file to Raspberry Pi Pico MicroPython IDE

Save the file with the following name: main.py. Overwrite any existing files with the same name.

micropython main.py file

Note: When you name a file main.py, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.

Reset your board (unplug and plug it into your computer).

Then, the LED should light up when you press the button and stay off when you release it.

Wrapping Up

To wrap up, to read the value of a GPIO, we simply need to use the value() method on the corresponding Pin object. To set the value of a GPIO, we just need to pass as argument 1 or 0 to the value() method to set it on or off, respectively.

We hope you’ve found this tutorial useful. If you’re just getting started with the Raspberry Pi Pico, make sure you read our getting started guide:

If you prefer to program the Raspberry Pi Pico using Arduino IDE, you can get started with the following tutorial:

You can check out all our Raspberry Pi Pico tutorials and projects on the following link:

You can also learn more about this amazing board using our dedicated Raspberry Pi Pico eBook:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

4 thoughts on “Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)”

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.