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

Learn how to set the Raspberry Pi Pico GPIOs as digital inputs and digital outputs using Arduino IDE. You’ll learn how to read digital inputs like a button switch and how to control a digital output like an LED.

Raspberry Pi Pico Control Digital Outputs and Read Digital Inputs Arduino IDE

New to the Raspberry Pi Pico? Read the following guide: Getting Started with Raspberry Pi Pico (and Pico W).

If you’re used to programming the Arduino, ESP32 and/or the ESP8266 using Arduino IDE, you’ll find that it’s pretty similar to programming the Raspberry Pi Pico.

Prerequisites

You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:

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 set a GPIO as a digital input or output, you use the pinMode() function that accepts as arguments the GPIO and its mode.

pinMode(GPIO, MODE);

The Raspberry Pi Pico supports three input modes:

  • INPUT: regular input;
  • INPUT_PULLUP: input with an internal pull-up resistance;
  • INPUT_PULLDOWN: input with an internal pull-down resistance.

For example, to create an input for a button on GPIO 21 (for buttons, it is very practical to use the internal pull-down resistance because you don’t need to add it to your circuit):

pinMode(21, INPUT_PULLDOWN);

digitalRead()

To read a digital input, like a button, you use the digitalRead() function. This function accepts as an argument the GPIO you are referring to. It’s as simple as that.

digitalRead(GPIO);

This returns HIGH (or True, or 1) or LOW (or False or 0).

Digital Outputs

To set a GPIO as a digital output, use the pinMode() function as follows:

pinMode(GPIO, OUTPUT);

digitalWrite()

To control a digital output you need to use the digitalWrite() function. This function accepts as arguments, the GPIO you are referring to, and the state, either HIGH or LOW.

digitalWrite(GPIO, STATE);

Schematic

For this example, we’ll wire an LED to GPIO 20 and a pushbutton to GPIO 21. You can use any other GPIO pins as long as you modify the code accordingly.

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.

  • LED » GPIO 20
  • Pushbutton » GPIO 21
Raspberry Pi Pico Pushbutton LED Circuit Schematic Diagram

The easiest way to assemble the circuit is to use a breadboard. The Raspberry Pi Pico fits perfectly on a regular breadboard.

Raspberry Pi Pico LED Pushbutton Circuit

Raspberry Pi Pico – Digital Inputs and Outputs Example Code

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-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

// set pin numbers
const int buttonPin = 21;     // the number of the pushbutton pin
const int ledPin =  20;       // the number of the LED pin

// variable for storing the pushbutton status 
int buttonState = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT_PULLDOWN);
  // initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off
    digitalWrite(ledPin, LOW);
  }
}

View raw code

How the Code Works

Let’s take a quick look at how the code works. Continue reading or skip to the Demonstration section.

In the following two lines, you create variables to assign pins:

const int buttonPin = 21; 
const int ledPin = 20;

The button is connected to GPIO 21, and the LED is connected to GPIO 20.

Next, you create a variable to hold the button state.

int buttonState = 0;

In the setup(), you initialize the button as an INPUT_PULLDOWN, and the LED as an OUTPUT. For that, you use the pinMode() function that accepts the pin you are referring to, and the mode, as we’ve seen previously.

pinMode(buttonPin, INPUT_PULLDOWN);
pinMode(ledPin, OUTPUT);

In the loop() is where you put the lines of code that will run indefinitely. That’s where we’ll read the button state and set the LED accordingly.

In the next line, you read the button state and save it in the buttonState variable. As we’ve seen previously, you use the digitalRead() function.

buttonState = digitalRead(buttonPin);

The following if statement checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite() function that accepts as arguments the ledPin, and the state, HIGH.

if (buttonState == HIGH) {
  digitalWrite(ledPin, HIGH);
}

If the button state is not HIGH, you set the LED off, by writing LOW as second argument in the digitalWrite() function.

else {
  digitalWrite(ledPin, LOW);
}

Uploading the Code to the Raspberry Pi Pico

For you to be able to upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.

If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.

Raspberry Pi Pico Bootloader mode

For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.

Now, select your COM port in Tools > Port. It may be the case that the COM port is grayed out. If that’s the case, don’t worry it will automatically find the port once you hit the upload button.

Raspberry Pi Pico - COM port not found Arduino IDE

Upload the code.

Arduino 2.0 Upload Button

You should get a success message.

Demonstration

After uploading the code, test your circuit. Your LED should light up when you press the pushbutton. And turn it off when you release it.

Wrapping Up

In summary, to set a GPIO as a digital input or as a digital output, you need to use the pinMode() function. After that, you can use the digitalRead() function to read the state of an input or the digitalWrite() function to control the state of an output.

As an example you’ve learned how to read a pushbutton and how to control an LED. What you learned can be applied to any digital input or output.

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 guides:

Check out all our Raspberry Pi Pico Guides »

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 (Arduino IDE)”

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.