The following code reads the sound level, maps it from a range of 0 - 255 down to 0 - 9 so we can use the NeoPixel LEDs to see the readings. It waits a tenth of a second (100 milliseconds), clears the LEDs, and takes another reading.

This is nearly identical to the light sensor program. 

And again in this example the value 255 is not in there, it's been lowered to 150. I could not whistle high enough to register a 255 so it was lowered to get a bigger range of values. You can change this to your own desired value.

Using Sound as a Trigger

There are additional blocks in MakeCode to run code based on whether the sound is above a set threshold. Use the on loud sound block from the INPUT group at the start of your code to indicate whether it has detected a loud sound.

The example sets NeoPixel 9 green on a loud sound. Pushing button A clears the light to start again. When the code is run, the green LED may light, press button A to begin, MakeCode may have a bug when starting the sound detector.

This guide was first published on Jul 26, 2018. It was last updated on Jun 21, 2024.

This page (MakeCode) was last updated on Mar 08, 2024.

Text editor powered by tinymce.