Sensing Sounds with Circuit Playground Express and Circuit Python
Previously, we lit up the Neopixels based on movement but in this project we will detect noise!
Sound detection
The Circuit Playground Express has an on-board microphone.
While obviously it is capable of more than the use we are putting it to, it's also decent noise meter ;)
To find the volume, we need to take some samples, then do some math to smooth it out.
Adafruit supply the audiobusio
module and some example normalized_rms
function code, which is good for me because I am pretty math incompetent!
Plotting and blinking
Based on the output of the Print, we can plot the graph you see above, using the Mu editor.
I noticed the room I was in (the kitchen of the vacation condo) was rarely below 100 magnitude and almost always above 50, you will need to consider your own environment for upper and lower values.
Again, I used the neopixel fill
function to light up the 10 pixels on the board.
Code
import array
import math
import time
import neopixel
import audiobusio
import board
def mean(values):
return sum(values) / len(values)
def normalized_rms(values):
minbuf = int(mean(values))
sum_of_samples = sum(float(sample - minbuf) * (sample - minbuf) for sample in values)
return math.sqrt(sum_of_samples / len(values))
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.5)
pixels.fill((0, 0, 40))
mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK,
board.MICROPHONE_DATA,
bit_depth=16)
samples = array.array('H', [0] * 160)
mic.record(samples, len(samples))
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print(((magnitude),))
if magnitude > 40:
pixels.fill((0, 0, 20))
if magnitude > 50:
pixels.fill((0, 0, 40))
if magnitude > 60:
pixels.fill((0, 0, 60))
if magnitude > 70:
pixels.fill((0, 0, 80))
if magnitude > 80:
pixels.fill((0, 0, 100))
if magnitude > 90:
pixels.fill((0, 0, 120))
if magnitude > 100:
pixels.fill((0, 0, 150))
time.sleep(0.1)
Join our Discord Channel to connect with us and nominate your own or somebody else's posts in our review channel.
Help us to reward you for making it ! Join our voting trail or delegate steem power to the community account.
Your post is also presented on the community website www.steemmakers.com where you can find other selected content.
If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.
Hi @makerhacks!
Your post was upvoted by utopian.io in cooperation with steemmakers - supporting knowledge, innovation and technological advancement on the Steem Blockchain.
Contribute to Open Source with utopian.io
Learn how to contribute on our website and join the new open source economy.
Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV