Use the microphone on your Circuit Playground Express to measure sound levels and display them on a VU-meter-like display!
The program is below. There are many settings that you can change to make the readings more or less sensitive and the display more or less jumpy. Try changing CURVE
to be 4 or 1 or 10 or -2 and see what happens.
The program samples audio for a short time and the computes the energy in the sample (corresponding to volume) using a Root-Mean-Square computation (RMS). You could try varying the sample size if you like.
The log_scale()
function varies the number of NeoPixels lit in an exponential way, because sound levels can vary by many factors of 10 between loud and soft. Try varying how it's computed to see what happens.
The program takes one sample when it first starts to set a "quiet" sound level. If that doesn't work for you, set input_floor
to be a fixed number. If the meter seems too sensitive, try changingĀ input_ceiling = input_floor +
500
to beĀ input_ceiling = input_floor +Ā 2000
or higher. Or go the other way.
You can also change the colors. Try different ways of computing volume_color(i)
for more of a rainbow effect, or make it a constant if you don't like changing colors.
Installing Project Code
To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory Introducing_CircuitPlaygroundExpress_SoundMeter/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:

# SPDX-FileCopyrightText: 2017 Dan Halbert for Adafruit Industries # SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries # SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT # The MIT License (MIT) # # Copyright (c) 2017 Dan Halbert for Adafruit Industries # Copyright (c) 2017 Kattni Rembor, Tony DiCola for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # Circuit Playground Sound Meter import array import math import audiobusio import board import neopixel # Color of the peak pixel. PEAK_COLOR = (100, 0, 255) # Number of total pixels - 10 build into Circuit Playground NUM_PIXELS = 10 # Exponential scaling factor. # Should probably be in range -10 .. 10 to be reasonable. CURVE = 2 SCALE_EXPONENT = math.pow(10, CURVE * -0.1) # Number of samples to read at once. NUM_SAMPLES = 160 # Restrict value to be between floor and ceiling. def constrain(value, floor, ceiling): return max(floor, min(value, ceiling)) # Scale input_value between output_min and output_max, exponentially. def log_scale(input_value, input_min, input_max, output_min, output_max): normalized_input_value = (input_value - input_min) / \ (input_max - input_min) return output_min + \ math.pow(normalized_input_value, SCALE_EXPONENT) \ * (output_max - output_min) # Remove DC bias before computing RMS. def normalized_rms(values): minbuf = int(mean(values)) samples_sum = sum( float(sample - minbuf) * (sample - minbuf) for sample in values ) return math.sqrt(samples_sum / len(values)) def mean(values): return sum(values) / len(values) def volume_color(volume): return 200, volume * (255 // NUM_PIXELS), 0 # Main program # Set up NeoPixels and turn them all off. pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False) pixels.fill(0) pixels.show() mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) # Record an initial sample to calibrate. Assume it's quiet when we start. samples = array.array('H', [0] * NUM_SAMPLES) mic.record(samples, len(samples)) # Set lowest level to expect, plus a little. input_floor = normalized_rms(samples) + 10 # OR: used a fixed floor # input_floor = 50 # You might want to print the input_floor to help adjust other values. # print(input_floor) # Corresponds to sensitivity: lower means more pixels light up with lower sound # Adjust this as you see fit. input_ceiling = input_floor + 500 peak = 0 while True: mic.record(samples, len(samples)) magnitude = normalized_rms(samples) # You might want to print this to see the values. # print(magnitude) # Compute scaled logarithmic reading in the range 0 to NUM_PIXELS c = log_scale(constrain(magnitude, input_floor, input_ceiling), input_floor, input_ceiling, 0, NUM_PIXELS) # Light up pixels that are below the scaled and interpolated magnitude. pixels.fill(0) for i in range(NUM_PIXELS): if i < c: pixels[i] = volume_color(i) # Light up the peak pixel and animate it slowly dropping. if c >= peak: peak = min(c, NUM_PIXELS - 1) elif peak > 0: peak = peak - 1 if peak > 0: pixels[int(peak)] = PEAK_COLOR pixels.show()
Page last edited January 22, 2025
Text editor powered by tinymce.