Programming with MakeCode

The Circuit Playground Express can be programmed a number of ways: it will run Arduino code, CircuitPython, or you can program it with MakeCode.  

Microsoft MakeCode for Adafruit is a web-based code editor for physical computing. It provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users. 

This means you can drag and drop light animations and functionality using the Circuit Playground Express' onboard sensors without ever writing a single line of code.   Just snap the blocks together and watch your lights dance.

For this project, I used the Circuit Playground's onboard light sensor to turn the lights on automatically when the room gets dark.  When the room brightens back up, the lights will turn off.

The lights are running MakeCode's built-in rainbow animation.  Check out this guide for more info on working with neopixel strands and building your own animations.

Go to https://makecode.adafruit.com/ and select "New Project".

Set Up the Light Strand

We soldered our light strand to pin A1.  Tell the code by adding an on start loop (Loops > on start).  This block will run ONCE when the board powers up, so it's the perfect place to define our setup.

Drag the on start block above the forever block.

Next go to "Variables" and drag set (item) to 0 into the on start block.   

Change (item) to (strip).  

Go to the "Neopixel" bin (you may need to click the "Light" bin to make it appear).  Drag create strip on (A1) with (24) pixels into the block you just made, replacing the "0" field.

In the "Neopixel" bin, find strip > show animation () for 500 ms.  Drag this block into the foreverloop. 

Download Code

Let's test to see if it's working. 

  1. Plug your Circuit Playground Express into your computer with a USB cable. 
  2. Click the reset button. 
  3. Green lights will appear on the Circuit Playground's face  and it will appear in your list of devices, called CPLAYBOOT

If you don't see this, try double-clicking the reset button instead of single-clicking.

Click the pink Download button on your MakeCode screen and the code you just made will download to your computer.  Drag it onto the CPLAYBOOT device.  

Plug the light strand into power and you will see a pretty rainbow animation.  Hooray!

Add Ambient Light Sensing

Let's make the lights turn on auto-magically when it gets dark, and off again in bright daylight.  We can do this using the Circuit Playground Express' onboard light sensor and a little bit of experimentation.

From the pink Input bin, drag four blocks:

  • two instances of on light (dark) 
  • two instances of set (dark) light threshold to 0

From the Neopixel bin, drag two blocks:

  • two instances of (strip) set brightness to (0)

We want the lights to come ON when it gets dark in the room, and go OFF when it gets bright. 

Place one instance of (strip) set brightness to (0) inside each on light () loop.   Leave the (light) one at 0, and set the (dark) one at 150 or so, or whatever you'd like your maximum brightness to be (on a scale of 1-255, 255 being the brightest they can go).   

Next, place both the set (dark) light threshold to 0 blocks inside the on start loop.  Change (dark) to (light) on one of them, so one triggers above a certain brightness and the other triggers below a certain darkness.

Calibrate

Every room or environment is different, so you'll need to experiment a bit to get the thresholds right.  I found that set (bright) to 21 and set (dark) to 32 works well in a window-lit bedroom -- when I turn on my overhead lights, the light strand goes off, and when the overhead lights go off, the light strand comes on.  

Download the code and drag it to CPLAYBOOT to test and see where your light threshold needs to be.  Experiment until it behaves the way you want.  A bright flashlight is helpful for experimentation!

Here's the completed project that you can play with directly.

Last updated on Feb 08, 2018 Published on Feb 08, 2018