This code turns the power relay ON when the Circuit Playground is moved or shaken. It then starts a countdown timer, and when the time runs out, it switches the power relay to OFF again.

We used this as a closet light. The Circuit Playground is mounted to the door, and when the door is opened the light in the closet comes on, then turns itself off a few seconds later.

The timer code is based on the Game Clock code, so if you want to delve into how it works, check that guide out here

You can change the duration of the countdown timer by changing the timer variable.  And you can change the countdown light color by choosing your favorite in the set pixel color block.

If you want to change the sensitivity or the type of motion being sensed, there are lots of options available in the input block. Test a few out and see what works best for you.

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 (Motion Activation) was last updated on Mar 08, 2024.

Text editor powered by tinymce.