Raspberry Pi Pico: Read Analog Inputs (MicroPython)

This guide shows how to read analog values with the Raspberry Pi Pico using MicroPython. 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 MicroPython

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

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

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.

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.

However, then, this is handled in MicroPython and transformed to a 16-bit number ranging from 0 to 65535 so that it behaves the same as ADC on other MicroPython microcontrollers.

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 – micropython then transforms this number to a range of 0 to 65535;
  • 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.

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.

Script – 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 Shell.

# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-analog-inputs-micropython/
from machine import Pin, ADC
from time import sleep

pot = ADC(Pin(26))

while True:
  pot_value = pot.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v
  print(pot_value)
  sleep(0.1)

View raw code

How the code works

This code simply reads the values from the potentiometer and prints those values in the MicorPython Shell.

To read analog inputs, import the ADC class in addition to the Pin class from the machine module. We also import the sleep method.

from machine import Pin, ADC
from time import sleep

Then, create an ADC object called pot on GPIO 26— the GPIO the potentiometer is connected to.

pot = ADC(Pin(26))

In the while loop, read the pot value and save it in the pot_value variable. To read the value from the pot, simply use the read_16() method on the pot object. This will give a value between 0-65535 across a 0-3.3V voltage range.

pot_value = pot.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v

Then, print the pot value.

print(pot_value)

At the end, add a delay of 100 ms.

sleep(0.1)

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 Raspberry Pi Pico Read Analog Values

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

Save Files to Raspberry Pi Pico Thonny IDE

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

Micropython saving main.py file Thonny IDE

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). Click the little green button “Run Current Script” or press F5.

Thonny IDE run code

Rotate the potentiometer and see the values changing on the MicroPython shell.

Raspberry Pi Pico Analog Read Adjust Potentiometer

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

Raspberry Pi Pico Read Analog Values Micropython shell

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 using MicroPython firmware is as simple as using the read_u16() function on an ADC object.This will give you a value between 0-65535 across a voltage range between 0.0V – 3.3V.

Learn more about the Raspberry Pi Pico with our eBook:

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 »

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!

2 thoughts on “Raspberry Pi Pico: Read Analog Inputs (MicroPython)”

  1. Nice project for me. I just started to work with the Pico.
    I am just wondering how you can count up to 65535 with a 12-bit ADC.
    Because you can basically only count up to 4095 with 12 bits.
    Do they use a mapping command in Micro-python to stretch the area between 0 and 4095 to 0 and 65535?

    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.