Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (2024)

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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (1)

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:

  • Programming Raspberry Pi Pico with Arduino IDE

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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (2)

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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (4)

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

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (5)

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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (6)

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 valueint 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 thesetup(), 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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (7)

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: Read Analog Inputs (Arduino) | Random Nerd Tutorials (8)

Upload the code.

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (9)

You should get a success message.

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (10)

Demonstration

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

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (11)

Rotate the potentiometer and see the values changing.

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (12)

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

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (13)

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:

  • Programming Raspberry Pi Pico with Arduino IDE (Pico W compatible)
  • Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
  • Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)
  • Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)

Check out all our Raspberry Pi Pico Guides »

Thanks for reading.

Raspberry Pi Pico: Read Analog Inputs (Arduino) | Random Nerd Tutorials (2024)
Top Articles
The Best Starbucks Egg Bites Recipe {Copycat} - The Girl on Bloor
40 Whole30 Air Fryer Recipes - Wholesomelicious
Vegas X Vip.org
Trivago Manhattan
Victoria Tortilla & Tamales Factory Menu
Basic Setup – OpenXR & Pimax HMDs...
Futuretechgirls Contact
Gay Black Scat
Trailmaster Fahrwerk - nivatechnik.de
Nsu Kpcom Student Handbook
Configuring Fail2ban with Traefik
Telegraph Ukraine podcast presenter David Knowles dies aged 32
Ju Hua (Flos Chrysanthemi): Uses, Benefits, Side Effects, Warnings
2320 Pioneer Rd
The Goddess Collection
Savage Model 110 Serial Number Lookup
Metalico Sharon Pa
The Dillards: From Mayberry's Darlings to Progressive Bluegrass Pioneers
Costco Plaza Alhambra Photos
Dow Futures Pre Market Cnn
Best 2 Player Tycoons To Play With Friends in Roblox
Nope 123Movies Full
Brise Stocktwits
Xxc Renegade 1000 Xxc Price In India Price
Meridamoonbeams
Rockcastle County Schools Calendar
One Piece Chapter 1077 Tcb
Pdinfoweb
Eddie Murphy Cast Of Elemental
Lids Locker Room Vacaville Photos
Back Doctor Near Me That Accept Medicaid
3Kh0 1V1 Lol
Tackytwinzzbkup
Ancestors The Humankind Odyssey Wikia
Wgu Admissions Login
Leccion 4 Lesson Test
Jan Markell Net Worth
Super Restore Vs Prayer Potion
Mula Pelada
Warrior Badge Ability Wars
Shirley Arica Unlock
Boise Craigslist Cars And Trucks - By Owner
Tandon School of Engineering | NYU Bulletins
MyEyeDr. near Lind<b>ergh Center Metro Station
Swissport Timecard
Super Bowl 17 Ray Finkle
Idaho Pets Craigslist
Green Press Gazette Obits
424-385-0597 phone is mostly reported for Text Message!
Craigslist Free Stuff Bellingham
LP Vinyl Samling pop rock thrash metal trance
Lottozahlen für LOTTO 6aus49 | LOTTO Bayern
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5937

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.