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.
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.
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.
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 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.
To learn more about the Pico Pinout, read the following guide: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained.
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.
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.
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)
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.
At the end, add a delay of 100 ms.
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.
With the code copied to the file, click on the Save icon. Then, select Raspberry Pi Pico.
Save the file with the following name: main.py. Overwrite any existing files with the same name.
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.
Rotate the potentiometer and see the values changing on the MicroPython shell.
The maximum value you can get is 65535, and the minimum value is 0.
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.
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:
- Getting Started with Raspberry Pi Pico (and Pico W)
- Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)
- Raspberry Pi Pico: PWM Fading an LED (MicroPython)
Thanks for reading.