This example shows you how you can read the analog voltage on the A1 pin on your board.
In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Essentials/CircuitPython_Analogin/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """CircuitPython Essentials Analog In example""" import time import board from analogio import AnalogIn analog_in = AnalogIn(board.A1) def get_voltage(pin): return (pin.value * 3.3) / 65536 while True: print((get_voltage(analog_in),)) time.sleep(0.1)
Creating the analog input
analog1in = AnalogIn(board.A1)
Creates an object and connects the object to A1 as an analog input.
get_voltage
Helper
get_voltage(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 prints
out the voltage as floating point values by calling get_voltage
on our analog object. Connect to the serial console to see the results.
Changing It Up
By default the pins are floating so the voltages will vary. While connected to the serial console, try touching a wire from A1 to the GND pin or 3Vo pin to see the voltage change.
You can also add a potentiometer to control the voltage changes. From the potentiometer to the board, connect the left pin to ground, the middle pin to A1, and the right pin to 3V. If you're using Mu editor, you can see the changes as you rotate the potentiometer on the plotter like in the image above! (Click the Plotter icon at the top of the window to open the plotter.)
Wire it up
The list below shows wiring diagrams to help find the correct pins and wire up the potentiometer, and provides more information about analog pins on your board!
Circuit Playground Express
A1 is located on the right side of the board. There are multiple ground and 3V pads (pins).
Your board has 7 analog pins that can be used for this purpose. For the full list, see the pinout page on the main guide.
Trinket M0
A1 is labeled as 2! It's located between "1~" and "3V" on the same side of the board as the little red LED. Ground is located on the opposite side of the board. 3V is located next to 2, on the same end of the board as the reset button.
You have 5 analog pins you can use. For the full list, see the pinouts page on the main guide.
Gemma M0
A1 is located near the top of the board of the board to the left side of the USB Micro port. Ground is on the other side of the USB port from A1. 3V is located to the left side of the battery connector on the bottom of the board.
Your board has 3 analog pins. For the full list, see the pinout page on the main guide.
QT Py M0
A1, shown connected to the blue wire, is near the USB port between A0 and A2. Ground is on the opposite side of the QT Py, near the USB port, between 3V and 5V. 3V is the next pin, between GND and MO.
Your board has 10 analog pins. For the full list, see the pinouts page in the main guide.
Feather M0 Express and Feather M4 Express
A1 is located along the edge opposite the battery connector. There are multiple ground pins. 3V is located along the same edge as A1, and is next to the reset button.
Your board has 6 analog pins you can use. For the full list, see the pinouts page on the main guide.
ItsyBitsy M0 Express and ItsyBitsy M4 Express
A1 is located in the middle of the board, near the "A" in "Adafruit". Ground is labled "G" and is located next to "BAT", near the USB Micro port. 3V is found on the opposite side of the USB port from Ground, next to RST.
You have 6 analog pins you can use. For a full list, see the pinouts page on the main guide.
Metro M0 Express and Metro M4 Express
A1 is located on the same side of the board as the barrel jack. There are multiple ground pins available. 3V is labeled "3.3" and is located in the center of the board on the same side as the barrel jack (and as A1).
Your Metro M0 Express board has 6 analog pins you can use. For the full list, see the pinouts page on the main guide.
Your Metro M4 Express board has 6 analog pins you can use. For the full list, see the pinouts page on the main guide.
Reading Analog Pin Values
The get_voltage()
helper used in the potentiometer example above reads the raw analog pin value and converts it to a voltage level. You can, however, directly read an analog pin value in your code by using pin.value
. For example, to simply read the raw analog pin value from the potentiometer, you would run the following code:
import time import board from analogio import AnalogIn analog_in = AnalogIn(board.A1) while True: print(analog_in.value) time.sleep(0.1)
This works with any analog pin or input. Use the <pin_name>.value
to read the raw value and utilise it in your code.
Text editor powered by tinymce.