Raspberry Pi Pico: Read Analog Inputs (Arduino IDE)

This guide shows how to read analog values with the Raspberry Pi Pico using Arduino IDE. As an example, we’ll read the values from a potentiometer, but what you’ll learn can be applied to any analog sensor/peripheral.

Raspberry Pi Pico Analog Readings with Arduino IDE

We have a similar guide using MicroPython firmware: Raspberry Pi Pico: Read Analog Inputs (MicroPython).

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:

Analog Readings – Raspberry Pi Pico

In this tutorial, you’ll learn how to read an analog input with the Raspberry Pi Pico. This is useful to read values from variable resistors like potentiometers or analog sensors.

Raspberry Pi Pico Potentiometer

The Raspberry Pi Pico has a 12-bit ADC with four channels on a fixed set of pins (plus an extra internal temperature sensor). This means it can transform an analog signal into a digital signal as a number ranging from 0 to 4095.

It has five ADC channels, but only four are accessible on the GPIOs: GPIO26, GPIO27, GPIO28, and GPIO29.

The first three GPIOs (26, 27, and 28) can be used to read voltage from peripherals, while GPIO29 can be utilized to measure the voltage level of the VSYS supply on the Raspberry Pi Pico board (VSYS is the input voltage that powers the board).

The fifth ADC channel is connected to a built-in temperature sensor.

In summary, here are the key features of the analog pins of the Raspberry Pi Pico board:

  • 12-bit resolution – transforms an analog signal into a value between 0 and 4095;
  • 4 ADC channels on external GPIOs;
  • GPIOs 26, 27, and 28 can be used to read output voltage from peripherals;
  • GPIO29 can measure the input voltage that powers the board (VSYS);
  • There’s a fifth ADC channel that is connected to an internal temperature sensor.

Identify the ADC pins on your Raspberry Pi Pico board. They are highlighted in dark green color.

Raspberry Pi Pico W Pinout
Raspberry Pi Pico Pinout

To learn more about the Pico Pinout, read the following guide: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained.

analogRead()

Reading an analog input in the Raspberry Pi Pico using the Arduino IDE is as simple as using the analogRead() function, which accepts as an argument the GPIO you want to read, as follows:

analogRead(GPIO);

By default, your code will set a 10-bit resolution. To set 12-bit resolution, you can use the analogReadResolution() function as follows:

analogReadResolution(12);

Schematic

Before proceeding, wire a potentiometer to your Raspberry Pi Pico board. You can connect the data pin to any ADC pin. We’ll use ADC0 on GPIO 26.

New to potentiometers? Learn how potentiometers work.

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 Diagram – Raspberry Pi Pico

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

raspberry pi pico potentiometer schematic diagram fritzing

In this example, we’re using GPIO26 to read analog values from the potentiometer, but you can choose any other GPIO that supports ADC.

Code – Raspberry Pi Pico Read Analog Inputs

The following code for the Raspberry Pi Pico reads analog values from GPIO 26 and prints the results on the Serial Monitor.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-analog-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.
*********/

// Potentiometer is connected to GPIO 26 (Analog ADC0)
const int potPin = 26;

// variable for storing the potentiometer value
int potValue = 0;

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);
  delay(1000);
}

void loop() {
  // Reading potentiometer value
  potValue = analogRead(potPin);
  Serial.println(potValue);
  delay(500);
}

View raw code

How the code works

This code simply reads the values from the potentiometer and prints those values in the Serial Monitor.

In the code, you start by defining the GPIO the potentiometer is connected to. In this example, it’s GPIO 26.

const int potPin = 26;

In the setup(), we initialize serial communication at a baud rate of 115200.

Serial.begin(115200);

Set 12-bit resolution using the analogReadResolution() function.

analogReadResolution(12);

If you prefer to use the default 10-bit resolution, you just need to remove this previous line.

In the loop(), you use the analogRead() function to read the analog input from the potPin.

potValue = analogRead(potPin);

Finally, you print the values read from the potentiometer in the serial monitor.

Serial.println(potValue);

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 UDE Upload Button

You should get a success message.

Raspberry Pi Pico Arduino IDE Done Uploading

Demonstration

After uploading the code to the board, open the Serial Monitor at a baud rate of 115200.

Open serial monitor

Rotate the potentiometer and see the values changing.

Raspberry Pi Pico Analog Read Adjust Potentiometer

The maximum value you can get is 4095, and the minimum value is 0.

Read Analog Values Arduino IDE Raspberry Pi Pico

Wrapping Up

In this tutorial, you learned about analog reading with the Raspberry Pi Pico. It has five 12-bit ADC channels, four of which are accessible on its GPIOs, and the fifth channel is connected to an internal temperature sensor.

Reading the voltage on a GPIO is as simple as using the analogRead() function and passing the corresponding GPIO as an argument. This will give you a value between 0 and 4095, in which 0 corresponds to 0V and 4095 to 3.3V

We hope you’ve found this tutorial useful. If you’re just getting started with the Raspberry Pi Pico, you may like to read the following tutorials:

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 »

Enjoyed this project? Stay updated by subscribing our newsletter!

4 thoughts on “Raspberry Pi Pico: Read Analog Inputs (Arduino IDE)”

  1. Thanks for interesting read. I wonder about the GPIO29, is it just ment for reading VSYS, or can it be used like the other ADC inputs.
    Is the only difference that it measures from 0 to 5V instead of 3.3?

    Reply
  2. Hi ,
    I’m searching for code on howto read the internal temperature sensor on the rp2040 , in the arduino IDE.
    Do you know how to do this ? The sensor seems not to be connected to a gpio pin.
    I can find examples in micropython , but I don’t like micrropython 🙁
    Kind regards,
    Ronny

    Reply

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.