Circuit Playground Express Timer

The Circuit Playground Express (CPX) hand wash timer will be created with MakeCode. If you're new to MakeCode, check out this guide on getting started.

We'll use MakeCode's graphical, block-based interface to create the code that runs on the CPX.

Click the link above to open the MakeCode file -- be sure to use an approved browser, such as Chrome or Edge.

Once you have the code open, click on the Edit button.

The key features of the code are the on start block, the on loud sound block, and the three functions:

  • blink
  • prep_countdown
  • countdown

On Start

When the CPX is first powered on or reset, the on start block will run. In it we set the NeoPixel brightness to 10, call the blink function with arguments of 1 for color and 2 for times (more on blink below).

Then the pixels are turned of and the on-board red LED is turned on by setting the pin high.

On Loud Sound

When a lound sound is detected, the code in the on loud sound block runs. This calls the prep_countdown function and the countdown function. More on those below.


The blink function is called, along with two arguments, color and time.

The repeat loop will iterate however many times it is called, for example the startup loop blink calls it twice.

Inside the repeat, the pixels are first turned off, then after a short pause the color argument is checked -- if it is a 0 then the pixels will be set to red, if not, green.

This could be expanded to include more colors.

Then, there is a short pause and the process repeats, thus creating the blinking pattern.

Prep Countdown

The prep_countdown function is used to put a certain number of seconds "on the clock" by lighting up the NeoPixels one at a time in a counterclockwise direction. It is called along with an argument of a number of seconds.

The for index loop iterates whatever code is contained within it the number of times specified by the seconds value. Additionally, the index variable's value will start at 0 and increase by one for each iteration through the loop.

On the first ten iterations (0-9) the corresponding NeoPixel will be lit up cyan, thanks to the if index < 10 check.

Once index is greater than 9, the pixels will loop around the circle a second time, this time being lit up green.

Since the value of index will be 10-20 and there are no NeoPixels on the CPX with those values, we need to do a bit of modulo math to loop back around to 0-9 again. That is how the remainder of index / 10 block is used.


The countdown function works similarly to the prep_countdown function, running back from seconds to 0, and with a one second pause between each NeoPixel call.

To run backwards, a variable called counter is created, and is set to equal the seconds variable minus the index + 1. So, for example, seconds is 20, index is 0 on the first loop through, so counter = 20 - (0 + 1) or 19.

For the first ten seconds, counter > 10, so after a one second pause the appropriate pixel is set to magenta for each iteration. Again, a modulo function is used to light the proper NeoPixel.

On the second ten seconds through, the counter is less than or equal to ten, so the pixels are set to blue when its their turn.

After the 20 seconds elapse, the whole ring is set to red and blinks red three times to tell you its OK to stop scrubbing!

This guide was first published on Apr 01, 2020. It was last updated on Apr 01, 2020.
This page (Circuit Playground Express Timer) was last updated on Jun 01, 2020.