This code sample toggles the switch on or off whenever a loud sound is heard. It also lights up the Circuit Playground's onboard lights for just a moment, to give more feedback when the switch is triggered.

This is a great example of using MakeCode to create a toggle on/off switch with just a few blocks and an if/then statement. We've created a variable called switchOn which allows the Circuit Playground to "remember" whether the light is on or off, and toggle the power outlets accordingly.

Once you've customized your code, click the Download button at the bottom of the screen. Plug your Circuit Playground into your computer via the USB port. The lights on the face will turn green (if they don't, click the tiny "reset" button in the center of the board) and a drive will appear on your computer called CPLAYBOOT

To program your Circuit Playground, drag the file you downloaded onto this drive and the code will start running.

If you're having trouble, head over to the MakeCode Beginner Guide for more tips and troubleshooting.

This guide was first published on Apr 21, 2020. It was last updated on Mar 08, 2024.

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

Text editor powered by tinymce.