CircuitPython Analog In

This quick-start example shows how you can read the analog voltages on all three Gemma M0 pads.

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

# Gemma IO demo - analog inputs

from digitalio import DigitalInOut, Direction
from analogio import AnalogIn
import board
import time

led = DigitalInOut(board.L)
led.direction = Direction.OUTPUT

analog0in = AnalogIn(board.A0)
analog1in = AnalogIn(board.A1)
analog2in = AnalogIn(board.A2)

def getVoltage(pin):
    return (pin.value * 3.3) / 65536

while True:
    print("A0: %f \t\t A1: %f \t\t A2: %f" %


Creating analog inputs

analog0in = AnalogIn(A0)
analog1in = AnalogIn(A1)
analog2in = AnalogIn(A2)

Creates three objects, one for each pad, and connects the objects to A0, A1 and A2 as analog inputs.

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 three voltages as floating point values (the %f indicates to print as floating point) by calling getVoltage on each of our analog objects.

If you connect to the serial port REPL, you'll see the voltages printed out. By default the pins are floating so the voltages will vary. Try touching a wire from A0 to the GND or 3Vo pad to see the voltage change!

Last updated on 2017-12-01 at 04.59.09 PM Published on 2017-07-26 at 06.08.30 PM