The Circuit Playground Bluefruit has a sound sensor located on the right side of the board, above the ear printed on the board, and below button B. This sensor can be used to detect sound levels.
Add the following code to your code.py. Remember, if you need help with this, check here.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This example prints out sound levels using the sound sensor on a Circuit Playground Bluefruit. Try making sounds towards the board to see the values change. NOTE: This example does NOT support Circuit Playground Express. """ import time from adafruit_circuitplayground import cp while True: print("Sound level:", cp.sound_level) time.sleep(0.1)
Open the serial console to see the sound level printed out. Try making noise at your Circuit Playground to see the values change!
Let's look at the code. First we import time
and cp
.
Inside our loop, we print to the serial console, Sound level:
followed by the sound level value, cp.sound_level
. Then we have a time.sleep(0.1)
to slow down the speed at which it prints to the serial console. If it's too fast, it's difficult to read!
Plotting Sound Level
Let's take a look at these values on the Mu plotter! Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This example prints out sound levels using the sound sensor on a Circuit Playground Bluefruit. If you are using Mu, open the plotter to see the sound level plotted. Try making sounds towards the board to see the values change. NOTE: This example does NOT support Circuit Playground Express. """ import time from adafruit_circuitplayground import cp while True: print("Sound level:", cp.sound_level) print((cp.sound_level,)) time.sleep(0.1)
The code is almost identical, but we've added one line, print((cp.sound_level,))
.
Note that the Mu plotter looks for tuple values to plot. Tuples in Python come in parentheses ()
with comma separators. If you have two values, a tuple would look like (1.0, 3.14)
. Since we have only one value, we need to have it print out like (1.0,)
- note the parentheses around the number, and the comma after the number. Thus the extra parentheses and comma in print((cp.sound_level,))
.
Click on the Plotter button on the top of Mu to see the plotter. Try making sounds towards your board to see the plotter line go up. Try being quiet to see the plotter line go down. Have fun with it!
Loud Sound
What if you wanted to use a sound as an input? With the loud_sound()
feature, you can! It allows you to use a clap, snap or any other suitably loud sound as an input.
The following example lights up the NeoPixel LEDs when a loud enough sound occurs. Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This example lights up the NeoPixels on a Circuit Playground Bluefruit in response to a loud sound. Try snapping or clapping near the board to trigger the LEDs. NOTE: This example does NOT support Circuit Playground Express. """ import time from adafruit_circuitplayground import cp while True: if cp.loud_sound(): cp.pixels.fill((50, 0, 50)) time.sleep(0.2) else: cp.pixels.fill((0, 0, 0))
Try clapping, snapping, or yelling at your board. Purple NeoPixels!
Let's take a look at the code. First we import time
and cp
.
Inside our loop, we begin by saying if a loud sound occurs, if cp.loud_sound()
, turn on the NeoPixels a slightly dim purple, cp.pixels.fill((50, 0, 50))
. Then we add a time.sleep(0.2)
so the LEDs stay on long enough to see them. Without it, they only flash on for a moment.
Then we say, otherwise, turn the pixels off by setting them to (0, 0, 0)
. Without this, the pixels would turn on and stay on.
Loud Sound Threshold
If you find it's too easy or too difficult to trigger the loud sound, you can decrease or increase the threshold. loud_sound()
defaults to sound_threshold=200
. To make it harder to trigger, you can increase the threshold. Add the following code to your code.py:
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This example lights up the NeoPixels on a Circuit Playground Bluefruit in response to a loud sound. Try snapping or clapping near the board to trigger the LEDs. NOTE: This example does NOT support Circuit Playground Express. """ import time from adafruit_circuitplayground import cp while True: if cp.loud_sound(sound_threshold=250): cp.pixels.fill((50, 0, 50)) time.sleep(0.2) else: cp.pixels.fill((0, 0, 0))
The code is the same except we've increased the threshold by setting sound_threshold=250
, making it require a louder sound to trigger.
If you find it's too difficult to trigger, you can lower the threshold, making it require a quieter sound to trigger. Try setting sound_threshold=150
to see the difference.
Now you can use sound as an input on the Circuit Playground Bluefruit. Try combining it with the other concepts learned in this guide to see what else you can do!
Page last edited January 22, 2025
Text editor powered by tinymce.