Skip to content

Instantly share code, notes, and snippets.

@deckerego deckerego/code.py
Created Nov 5, 2019

Embed
What would you like to do?
Audio level meter with Circuit Playground Express
import array
import math
import time
import neopixel
import audiobusio
import board
def avg(values):
return sum(values) / len(values)
def normalized_rms(values):
avg_value = int(avg(values))
sum_samples = sum(float(sample - avg_value) * (sample - avg_value) for sample in values)
return math.sqrt(sum_samples / len(values))
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False)
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, bit_depth=16)
samples = array.array('H', [0] * 192)
mic.record(samples, len(samples))
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
pixels.fill((0, 0, 0))
pixels[0] = pixels[9] = (0, 128, 0)
if magnitude > 50:
pixels[1] = pixels[8] = (64, 128, 0)
if magnitude > 100:
pixels[2] = pixels[7] = (128, 64, 0)
if magnitude > 200:
pixels[3] = pixels[6] = (128, 32, 0)
if magnitude > 400:
pixels[4] = pixels[5] = (128, 0, 0)
pixels.show()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.