CircuitPython Code

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 “” 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
Last updated on 2018-01-13 at 12.03.08 AM Published on 2016-10-14 at 04.50.11 PM