Getting to know CircuitPython

CircuitPython is a programming language based on Python, one of the fastest growing programming languages in the world. It is specifically designed to simplify experimenting and learning to code on low-cost microcontroller boards.

CircuitPython is easiest to use within the Mu Editor. If you haven't previously used Mu, this guide will get you started.

Why Use CircuitPython?

CircuitPython allows for a certain level of flexibility that cannot be achieved in MakeCode, so for more experienced users it can be a natural next step after MakeCode.

The example code here allows you to include custom audio files, one of which will play when the piñata is hit, and another that plays when the piñata dispenses candy.

Detailed instructions on how to create your own audio files can be found here.

Preparing your Board

To get your Circuit Playground Express set up to run this code, follow these steps:

1) Install the latest CircuitPython for CPX from For this guide, please use version 4.0.0 or greater. Version 3.1.2 is not as efficient on memory and may give an error.

2) Get the latest 4.0 library pack, unzip it, and drag the library folder adafruit_motor over into the /lib folder on CIRCUITPY. If there is no lib directory, create one to put the file into. (More info on installing libraries, read information here.)

Find the adafruit_motor library in the library pack 

Drag to the lib folder in the CIRCUITPY drive


Make sure you've connected the Circuit Playground Express to your computer (mac/PC/Linux) via a known good USB A to micro-B cable. Your board should show up as a flash disk drive named CIRCUITPY (If you see a disk name CPLAYBOOT, try pressing the reset button again. If the only drive name you get is named CPLAYBOOT, CircuitPython may not be loaded on the board. You can load CircuitPython as per this guide).

Once your board is connected, copy from the window below and paste it into Mu. Press the Save button and your code should automatically be saved to the CIRCUITPY disk drive (which appears when the Circuit Playground Express is plugged into your computer) as

# SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
# SPDX-License-Identifier: MIT

# Circuit Playground Express Piñata by Dano Wall for Adafruit Industries
# CircuitPython code by Anne Barela for Adafruit Industries, MIT License
import time
import random
import board
import pwmio
from adafruit_motor import servo
from import cpx

# create a PWMOut object on CPX Pin A1
pwm = pwmio.PWMOut(board.A1, frequency=50)
# Create a servo object cpx_servo
cpx_servo = servo.Servo(pwm)

hits = 0
max_hits = random.randint(3, 10)
cpx.detect_taps = 1  # Detect single taps
cpx_servo.angle = 0
cpx.pixels.fill((0, 0, 0))  # All NeoPixels off

while hits < max_hits:
    if cpx.tapped:
        hits += 1
        cpx.pixels.fill((255, 255, 255))  # All White
        time.sleep(1.0)  # Wait time in seconds
        cpx.pixels.fill((0, 0, 0))  # All off

# Hits Reached, Payout!
cpx.pixels.fill((0, 255, 0))  # All green
cpx_servo.angle = 180
print("Press Reset or power cycle to reset device")
while True:
    pass     # Infinite loop, press Reset button to reset
Make sure the file saved to CIRCUITPY is named "", this will allow it to run automatically when your CPX is powered on.

Piñata Sounds

Click on the green buttons below to upload the two sound files used in this project:

The sound clips used in this project have been trimmed and edited from the following free audio files:

If you'd like to change the sounds your piñata makes, more audio clips can be downloaded from sites like or

You will need to convert the files to the appropriate format for microcontroller use. See this guide on how to do that.

When you have a file converted, you can name it either hit.wav or candy.wav and drag it onto your CIRCUITPY drive to have it play when the piñata is hit or to announce the dispensing of candy.

Directory View

When you have all the files on your CIRCUITPY drive, you should have the files shown below. The lib folder is in the main directory, the adafruit_motor folder in the lib directory and the py/mpy files in the adafruit_motor directory.


Problem: I don't hear any audio!

Solution: If you're creating your own audio files, make sure they are all exported as 16-bit PCM WAV at 22,050 Hz and they are should be Mono.


Problem: My Circuit Playground Express isn't recognized by Mu!

Solution: Make sure your board is set up with CircuitPython, which has the Circuit Playground Express show up as a flash drive named CIRCUITPY when you connect the CPX to your computer. If it is showing up as CPLAYBOOT on your computer, you can follow the steps in this guide to ensure CircuitPython is loaded and you see the CIRCUITPY drive.

More CircuitPython Projects!

If you enjoy programming in CircuitPython and want to continue learning you can check out more CircuitPython projects on the Adafruit Learn System.

This guide was first published on Apr 11, 2019. It was last updated on Jun 17, 2024.

This page (CircuitPython Code) was last updated on Jun 17, 2024.

Text editor powered by tinymce.