CircuitPython Analog In

This example shows you how you can read the analog voltage on the A1 pin on your board.

Copy and paste the code into code.py using your favorite editor, and save the file to run the demo.

# CircuitPython AnalogIn Demo
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

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 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.)

When you turn the knob of the potentiometer, the wiper rotates left and right, increasing or decreasing the resistance. This, in turn, changes the analog voltage level that will be read by your board on A1.

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.

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.

This guide was first published on Aug 23, 2017. It was last updated on Oct 11, 2018. This page (CircuitPython Analog In) was last updated on Jul 04, 2018.