This quick-start example shows how you can read the analog voltage of a potentiometer connected to the Circuit Playground Express.
First, connect your potentiometer to the Circuit Playground Express using three alligator clip leads, as shown. The connections are:
- Left pot connection to 3.3V
- Center pot (wiper) to A1
- Right pot connection to GND
In the example below, click the Download Project Bundle button below to download the necessary files in a zip file. Extract the contents of the zip file, open the directory Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_AnalogIn/ and then click on the directory that matches the version of CircuitPython you're using and copy code.py to your CIRCUITPY drive.
Your CIRCUITPY
# SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries # # SPDX-License-Identifier: MIT # Circuit Playground AnalogIn # Reads the analog voltage level from a 10k potentiometer connected to GND, 3.3V, and pin A1 # and prints the results to the serial console. import time import board import analogio analogin = analogio.AnalogIn(board.A1) def getVoltage(pin): # helper return (pin.value * 3.3) / 65536 while True: print("Analog Voltage: %f" % getVoltage(analogin)) time.sleep(0.1)
Creating an Analog Input
analogin = analogio.AnalogIn(board.A1)
creates an object named analogin
which is connected to the A1 pad on the Circuit Playground Express.
GetVoltage Helper
getVoltage(pin)
is our little helper program. 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.
Main Loop
The main loop is simple, it will just print out the voltage as a floating point value (the %f
indicates to print as floating point) by calling getVoltage
on each of our analog object, in this case the potentiometer.
If you connect to the serial console, you'll see the voltage printed out. Try turning the knob of the potentiometer to see the voltage change!
Text editor powered by tinymce.