We're going to use CircuitPython, Mu, and a potentiometer with Circuit Playground Express to plot voltage levels. We'll run this code on our Circuit Playground Express and use Mu to plot the voltage data that CircuitPython prints out.

First, let's get the potentiometer attached to your Circuit Playground Express!

To connect the potentiometer to the Circuit Playground Express:

  • Connect the left pin on the potentiometer to GND on the Circuit Playground Express.
  • Connect the middle pin on the potentiometer to A1 on the Circuit Playground Express.
  • Connect the right pin on the potentiometer to 3.3V on the Circuit Playground Express.

Save the following as code.py on your Circuit Playground Express board, using the Mu editor:

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

import analogio
import board

potentiometer = analogio.AnalogIn(board.A1)

def get_voltage(pin):
    return (pin.value * 3.3) / 65536

while True:

Let's take a look at the code!

First we import time, analogio and board. Next we create the potentiometer object and assign it to pin A1.

Then we have the get_voltage() helper function. By default, analog readings will range from 0 (minimum) to 65535 (maximum). This helper will convert the 0-65535 reading from pin.value and convert it a 0-3.3V voltage reading.

Our main loop is super simple. Inside our print statement, we call the get_voltage() helper and provide it with the potentiometer object. Then we have a time.sleep(0.1) to slow down the printed results - otherwise they'd be too fast to read!

Note that the Mu plotter looks for tuple values to print. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14) Since we have only one value, we need to have it print out like (1.0,) note the parentheses around the number, and the comma after the number. Thus the extra parentheses and comma in print((get_voltage(potentiometer),)).

Once you have everything setup and running, try rotating the potentiometer knob attached the Circuit Playground Express, and watch the plotter immediately react! Rotate to the left to watch the plotter go down. Rotate to the right to watch it go up!

This is a great way to see the voltage changes resulting from using a potentiometer, and plot the changes as you move the knob!

This guide was first published on Apr 09, 2018. It was last updated on Dec 08, 2023.

This page (Potentiometer) was last updated on Dec 06, 2023.

Text editor powered by tinymce.