CircuitPython/Adafruit Feather M0 Express

From MattWiki

Adafruit Feather M0 Express CircuitPython Examples

Here are a few of my example code for the Adafruit Feather M0 Express.

You may find a more information about the board on the website.

The current CircuitPython firmware for the Feather M0 Express can be found at circuitpython.org.

Read the battery voltage

# Read Battery Voltage - Adafruit Feather M0 Express

import board
import analogio
import time

vbat_voltage = analogio.AnalogIn(board.D9)

# Define the get_voltage function
def get_voltage(pin):
    return (pin.value * 3.3) / 65536 * 2


# Run the Main loop
while True:
    # Get the current voltage
    battery_voltage = get_voltage(vbat_voltage)
    
    # Just for demo purposes, print the current voltage
    print("VBat voltage: {:.2f}".format(battery_voltage))
    time.sleep(5)

Analog Out

This example shows you how you can set the DAC (true analog output) on pin A0.

# CircuitPython IO demo - analog output
import board
from analogio import AnalogOut
 
analog_out = AnalogOut(board.A0)
 
while True:
    # Count up from 0 to 65535, with 64 increment
    # which ends up corresponding to the DAC's 10-bit range
    for i in range(0, 65535, 64):
        analog_out.value = i

PWM with Fixed Frequency

Your board has pulseio support, which means you can PWM LEDs, control servos, beep piezos, and manage "pulse train" type devices like DHT22 and Infrared.

Nearly every pin has PWM support! For example, all ATSAMD21 board have an A0 pin which is 'true' analog out and does not have PWM support.


import time
import board
import pulseio

led = pulseio.PWMOut(board.D13, frequency=5000, duty_cycle=0)

while True:
    for i in range(100):
        # PWM LED up and down
        if i < 50:
            led.duty_cycle = int(i * 2 * 65535 / 100)  # Up
        else:
            led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)  # Down
        time.sleep(0.01)