CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards. You can learn more about CircuitPython in this guide.
CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started. Mu is available for PC, mac, and Linux and it has some very handy features we'll use.
Analog Reads
The CircuitPython analogio
module provides the analog pin functions comparable to MakeCode analog read block
and Arduino analogRead
function.
Save the following program to your Circuit Playground Express CIRCUITPY drive as code.py.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from analogio import AnalogIn potentiometer = AnalogIn(board.A1) # potentiometer connected to A1, power & ground while True: print((potentiometer.value,)) # Display value time.sleep(0.25) # Wait a bit before checking all again
AnalogIn
creates an object, here called potentiometer
, on pad A1 on Circuit Playground Express. The while
loop indefinitely prints out the value of the potentiometer object to the Serial connection then waits a bit.
Opening the Serial window in Mu (button circled in picture below), you will see that the range of values returned from AnalogIn
is much higher in CircuitPython: 0 to 65535 (was 65520 before CircuitPython 8.0.0). Take that into account when scaling or mapping values to a different range for display, etc.
In Mu, you can press the Plotter button to see a plot of the values. As you turn the knob of the potentiometer, the values change.
Display on the Circuit Playground Express NeoPixels
Any string of NeoPixels makes a great indicator for the value of a potentiometer. In the "old days" they would put markings on the case to tell you the amount you had turned the knob. Now you can do this electronically. A stick of NeoPixels or a ring of NeoPixels works great.
If you are using the Circuit Playground Express, the built-in NeoPixels are perfect as an indicator. The code is shown below.
Save the following program to your Circuit Playground Express CIRCUITPY drive as code.py.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from analogio import AnalogIn import neopixel NUMPIXELS = 10 # Circuit Playground Express has 10 pixels pixels = neopixel.NeoPixel(board.NEOPIXEL, NUMPIXELS, auto_write=False) # CPX NeoPixels potentiometer = AnalogIn(board.A1) # potentiometer connected to A1, power & ground def show_value(val): # Show value 0-9 on CPX NeoPixels for i in range(val): pixels[i] = (50, 0, 0) # Red for i in range(val, NUMPIXELS): pixels[i] = (0, 0, 0) pixels.show() while True: show_value(int(potentiometer.value / 65520 * NUMPIXELS)) # Show on NeoPixels print((potentiometer.value,)) # Print value time.sleep(0.25) # Wait a bit before checking all again
You can change the neopixel.NeoPixel call to reference an external strip or ring of pixels for your board and the number of pixels being addressed. NUMPIXELS
should be changed to the number you have.
Hopefully this has given you an idea how to read a potentiometer in CircuitPython and use the value in your project.
Text editor powered by tinymce.