Claws-down, Halloween is my favorite holiday. Add some electronics to the mix and it’s an unstoppable force of nature.

In this simple no-soldering-required project we’ll make the Adafruit Circuit Playground Classic or Circuit Playground Express board act like a flickering candle. Pop this inside a jack-o’-lantern (or any other item that might normally use flame) to create a festive spooky atmosphere.

Sure, one can just buy a fake battery-operated candle, but they’re usually pretty dim. Also, we can take this as an opportunity to learn a bit about code…and once Halloween’s over (how sad!) the Circuit Playground board can be used again in endless other projects.

Items needed:

If this is your first time using Circuit Playground, start with our “Introducing Circuit Playground” or “Circuit Playground Express” guide. There’s some software to set up on your computer and some procedures to learn. Try out a couple of the examples, make sure you know how to upload “sketches” (code) to the board.

Circuit Playground Express can be programmed three different ways. You only need to pick ONE of these — whichever you’re most comfortable with or looks interesting.

Arduino Code for Simulated Candle

This is the code you’ll use for Circuit Playground Classic. If you have a Circuit Playground Express, you can use this code as well, but have some additional options shown later.

Copy and paste the code below into a new Arduino sketch:

#include "Adafruit_CircuitPlayground.h"

void setup() {
  CircuitPlayground.setBrightness(255); // LEDs full blast!

uint8_t prev = 128;              // Start brightness in middle

void loop() {
  uint8_t lvl = random(64, 192); // End brightness at 128±64
  split(prev, lvl, 32);          // Start subdividing, ±32 at midpoint
  prev = lvl;                    // Assign end brightness to next start

void split(uint8_t y1, uint8_t y2, uint8_t offset) {
  if(offset) { // Split further into sub-segments w/midpoint at ±offset
    uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
    split(y1 , mid, offset / 2); // First segment (offset is halved)
    split(mid, y2 , offset / 2); // Second segment (ditto)
  } else { // No further subdivision - y1 determines LED brightness
    uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
                 * 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
    for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);

 From the “Tools” menu, select “Board→Adafruit Circuit Playground” (or Circuit Playground Express). Also make sure the correct serial port is selected, then upload the sketch to the board.

If all goes well, the NeoPixels on the Circuit Playground board should start flickering (mostly shades of yellow). There’s our electronic “candle”!

CircuitPython Code for Simulated Candle

Circuit Python Express boards can run CircuitPython — a different approach to programming compared to Arduino sketches. If you want to learn the basics of setting up and using CircuitPython, this is explained in the Circuit Python Express guide.

This is for Express boards only; Classic boards can’t run CircuitPython.

Below is CircuitPython code that works similarly to the Arduino sketch shown on a prior page. To use this, plug Circuit Playground Express into USB…it should show up on your computer as a small flash drive…then edit the file “main.py” with your text editor of choice. Select and copy the code below and paste it into that file, entirely replacing its contents (don’t mix it in with lingering bits of old code). When you save the file, the code should start running almost immediately (if not, see notes at the bottom of this page).

If Circuit Playground Express doesn’t show up as a drive, follow the guide link above to prepare the board for CircuitPython.

import board
import neopixel
import math
import time
        import urandom as random  # for v1.0 API support
except ImportError:
        import random

numpix = 10        # Number of NeoPixels
pixpin = board.D8  # Pin where NeoPixels are connected
strip  = neopixel.NeoPixel(pixpin, numpix, brightness=1.0)
prev   = 128

def split(y1, y2, offset):
        if offset != 0:
                mid = ((y1 + y2 + 1) / 2 + random.randint(-offset, offset))
                offset = int(offset / 2)
                split(y1, mid, offset)
                split(mid, y2, offset)
                c = math.pow(y1 / 255.0, 2.7) * 255.0 + 0.5
                strip.fill((int(c), int(c / 8), int(c / 48)))
                # time.sleep(0.01)

while True:  # Loop forever...
        lvl = random.randint(64, 191)
        split(prev, lvl, 32)
        prev = lvl

MakeCode for Simulated Candle

Another option for Circuit Playground Express boards (not Classic) is MakeCode, a “block based” visual programming environment.

The basics of using MakeCode are explained in the Circuit Playground Express guide.

Powering the Project

For stand-alone powering the project, our 3xAAA battery holder has a power switch and a JST connector that fits the Circuit Playground board. A fresh set of batteries should run it for about ten hours, maybe even longer. Alternately, a USB phone charger (and micro-B cable) can be used.

Last updated on 2017-10-30 at 08.42.11 PM Published on 2016-10-14 at 04.50.11 PM