Adafruit Trinket M0
CircuitPython or Arduino IDE on this tiny little microcontroller board
- Overview
- Guided Tour
- Pinouts
- Windows Driver Installation
- What is CircuitPython?
- CircuitPython
- CircuitPython Essentials
- CircuitPython Pins and Modules
- CircuitPython Built-Ins
- CircuitPython Digital In & Out
- CircuitPython Analog In
- CircuitPython Analog Out
- CircuitPython PWM
- CircuitPython Servo
- CircuitPython Cap Touch
- CircuitPython Internal RGB LED
- CircuitPython NeoPixel
- CircuitPython DotStar
- CircuitPython UART Serial
- CircuitPython I2C
- CircuitPython HID Keyboard and Mouse
- CircuitPython CPU Temp
- CircuitPython Storage
- CircuitPython Expectations
- MakeCode
- Arduino IDE Setup
- UF2 Bootloader Details
- Downloads
CircuitPython Analog In
This example shows you how you can read the analog voltage on the A1 pin on your board.
In the example below, click theDownload Project Bundle button below to download the necessary libraries and thecode.py file in a zip file. Extract the contents of the zip file, open the directoryCircuitPython_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 yourCIRCUITPY drive.
YourCIRCUITPY 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 timeimport boardfrom analogio import AnalogInanalog_in = AnalogIn(board.A1)def get_voltage(pin): return (pin.value * 3.3) / 65536while True: print((get_voltage(analog_in),)) time.sleep(0.1)
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries## SPDX-License-Identifier: MIT"""CircuitPython Essentials Analog In example"""import timeimport boardfrom analogio import AnalogInanalog_in = AnalogIn(board.A1)def get_voltage(pin): return (pin.value * 3.3) / 65536while 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 frompin.value and convert it a 0-3.3V voltage reading.
Main Loop
The main loop is simple. Itprints out the voltage as floating point values by callingget_voltage on our analog object. Connect to the serial console to see the results.
Changing It Up
By default the pins arefloating so the voltages will vary. While connected to the serial console, try touching a wire fromA1 to theGND pin or3Vo pin to see the voltage change.
You can also add a potentiometer to control the voltage changes. From the potentiometer to the board, connect theleftpin toground, themiddlepin toA1, and therightpin to3V. 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 thepinout 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 thepinouts 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 thepinout 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 thepinouts 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 thepinouts 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 thepinouts 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).
YourMetro M0 Express board has 6 analog pins you can use. For the full list, see thepinouts page on the main guide.
YourMetro M4 Express board has 6 analog pins you can use. For the full list, see thepinouts page on the main guide.
Reading Analog Pin Values
Theget_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 usingpin.value. For example, to simply read the raw analog pin value from the potentiometer, you would run the following code:
import timeimport boardfrom analogio import AnalogInanalog_in = AnalogIn(board.A1)while True: print(analog_in.value) time.sleep(0.1)
import timeimport boardfrom analogio import AnalogInanalog_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.
Page last edited January 22, 2025
Text editor powered bytinymce.



















