Skip to content

Getting Started: Reading Analog Values from a Potentiometer with Raspberry Pi Pico & MicroPython

Potentiometer

Table of Contents

Abstract

In this article, a comprehensive step-by-step guide to interface Analog data (Potentiometer) with Raspberry Pi Pico board using MicroPython. Raspberry Pi Pico has 4 different variants (Pico, Pico 2, Pico W, Pico 2W) supporting micro-python. This articles lays the foundation for more advanced embedded system and IoT projects.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Thonny IDE.
  • MicroPython firmware in Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.

Hardware Required

  • Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
  • Potentiometer (5k ohm or 10K ohm).
  • BreadBoard.
  • Micro USB Cable.
  • Connecting wires.
  • 3.3V DC power supply (optional)
Components Purchase Link
Raspberry Pi Pico link
Raspberry Pi Pico 2 link
Raspberry Pi Pico W link
Raspberry Pi Pico 2W link
Potentiometer 5k Ohm : 10k ohm
BreadBoard large : small
Connecting Wires link
Micro USB Cable link

Don't own a hardware 😢

No worries,

Still you can learn using simulation. check out simulation part 😃.

Connection Table

Potentiometer GPIO Remarks
T1 +3.3 V +3.3V of Board or External 3.3V DC Supply
T2 26 (A0) Any Analog pin (26, 27, 28) can be used
T3 GND Ground

Note

  • T1 and T2 can be interchanged in physical potentiometer.
  • In Wokwi simulation, T1 is Vcc, T2 is Sig, T3 is GND.
  • While using External 3.3 V supply, make sure ground pin of External Supply and Pico board is connected.

Circuit Diagram

fig-Connection Diagram

📂 Code

from machine import Pin, ADC
import time

time.sleep(1)

# Potentiometer connected to GPIO 26 (ADC 0)
# ADC PIN are GPIO 26, 27, 28.
pot = ADC(Pin(26))

while True:
    value = pot.read_u16()
    print(f"POT {value=}")

    position = (value / 65535) * 100
    print(f"{position=:0.2f} %")
    time.sleep(2)

Code Explanation

👉 Imports

from machine import Pin, ADC
import time
  • ADC class to configure GPIO pins (26, 27, 28) into ADC pins.
  • time module for creating delay.

👉 Initialize DHT 22 sensor.

pot = ADC(Pin(26))
  • GPIO 26 or A0 is configured as Analog Input Pin.

👉 Continuous looping & measurement.

while True:
    value = pot.read_u16()
    print(f"POT {value=}")

    position = (value / 65535) * 100
    print(f"{position=:0.2f} %")
    time.sleep(2)
  • while True used for continuous looping.
  • print statement to display the information on the terminal.
  • time.sleep(2) 2 second delay.

👉 Read ADC value.

value = pot.read_u16()
  • pot.read_u16() used to read the ADC value.
    • The output of read_u16() will be of 16 bit data
    • Value ranging from 0 to 65535 (i.e., 0b0000000000000000 to 0b1111111111111111)
    • As Raspberry Pi Pico based boards microcontroller operates at 3.3V.
    • The Potentiometer terminal (T1 / Vcc) is connected to 3.3V supply
  • 0 voltage corresponds to 0 value.
  • 3.3 voltage corresponds to 65535 value.

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Extras

Components details

Modules / Libraries Used

  • machine
    • machine module contains specific attributes and methods related to hardware on a particular board. Here class ADC is imported to configure the Analog pins.
    • More Details
  • time
    • time module provides functions related to date & time, measuring time intervals and generating delays.
    • More Details