Once your CPB is set up with CircuitPython, you'll also need to add some libraries. Follow this page for info on how to download and add libraries to your CPB.
From the library bundle you downloaded in that guide page, transfer the following library folders onto the CPB board's /lib directory:
- adafruit_ble
- adafruit_bluefruit_connect
- adafruit_motor
Before continuing, please be sure your board's CIRCUITPY drive has a /lib folder with the three folders listed above. Please do not copy the files out of those folders to put in the /lib directory.
Text Editor
Adafruit recommends using the Mu editor for using your CircuitPython code with the Circuit Playground Bluefruit boards. You can get more info in this guide.
Alternatively, you can use any text editor that saves files.
Code.py
Copy the code below and paste it into Mu. Then, save it to your CPB as code.py. Alternatively click Download: Project Zip or code.py links to download the file to your computer.
# SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
import pwmio
from adafruit_motor import servo
from adafruit_bluefruit_connect.packet import Packet
from adafruit_bluefruit_connect.button_packet import ButtonPacket
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)
pwm = pwmio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm)
while True:
print("WAITING...")
# Advertise when not connected.
ble.start_advertising(advertisement)
while not ble.connected:
pass
# Connected
ble.stop_advertising()
print("CONNECTED")
# Loop and read packets
while ble.connected:
if uart.in_waiting:
packet = Packet.from_stream(uart)
if isinstance(packet, ButtonPacket):
# if buttons in the app are pressed
if packet.pressed:
if packet.button == ButtonPacket.DOWN:
print("pressed down")
for angle in range(90, 170, 90): # 90 - 170 degrees, 90 degrees at a time.
my_servo.angle = angle
time.sleep(0.05)
if packet.button == ButtonPacket.UP:
print("pressed up")
for angle in range(170, 90, -90): # 170 - 90 degrees, 9 degrees at a time.
my_servo.angle = angle
time.sleep(0.05)
# Disconnected
print("DISCONNECTED")
Bluetooth App
This project uses the Adafruit Bluefruit LE connect app (available free for Android and iOS) to trigger the servo. It uses the control pad to open and close the servo claw. If you haven't downloaded the app yet, use the button below to install it on your mobile device.
Connect to Circuit Playground Bluefruit
Turn on the Circuit Playground Bluefruit by either connecting it via USB to your computer or with the 500mAh battery.
Page last edited January 22, 2025
Text editor powered by tinymce.