KevsRobots Learning Platform

Raspberry Pi Pico with MicroPython - GPIO Mastery

45% complete

Reset my progress

Analog Input (ADC)

Learn about analog input and the ADC on the Raspberry Pi Pico using MicroPython.

By Kevin McAleer,    2 Minutes


Overview

Digital pins only see on or off. Many sensors give you a voltage that can be anywhere in between. In this lesson you’ll learn how the Pico’s Analog-to-Digital Converter (ADC) turns a voltage into a number.


Analog input and ADC

An analog signal can have any value in a range, like the position of a dial or the amount of light in a room. The ADC measures the voltage on a pin and turns it into a number your code can use.

The Pico’s ADC is available on three pins:

ADC channel GP pin Physical pin
ADC0 GP26 31
ADC1 GP27 32
ADC2 GP28 34
ADC4 - Internal temperature sensor

The input must stay between 0V and 3.3V.


Configuring a pin for analog input

from machine import ADC

adc = ADC(26)   # GP26, ADC0

You can also write ADC(Pin(26)). Either way, only GP26, GP27 and GP28 will work.


Reading analog values

value = adc.read_u16()
print(value)

read_u16() returns a number from 0 (0V) to 65535 (3.3V).

To turn it into a voltage:

voltage = value * 3.3 / 65535
print(f"{voltage:.2f} V")

Resolution

The Pico’s ADC is really 12-bit (0 to 4095). MicroPython scales it up to 16 bits so that read_u16() gives the same range on every board.


Try it yourself

Connect GP26 to 3V3, then to GND, and print the reading each time. What do you get when the pin is connected to nothing?

Quick quiz 1 of 2

Which of these will work as an ADC input?

Quick quiz 2 of 2

read_u16() returns 32768. Roughly what voltage is on the pin?


< Previous Next >

You can use the arrows  ← → on your keyboard to navigate between lessons.


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z