We'll be using the servo motor as the locking/unlocking latch for our safe. The servo motor is different from a regular DC motor, in that it can be precisely controlled to rotate to a specific angle. This is why there are three wires coming from the servo, instead of the usual two on a DC motor for ground and voltage.

The third wire on the servo controls the desired angle by receiving a particular command from the Circuit Playground Express. This control is send in the form of a pulse-width modulated (PWM) signal. We'll take a closer look at this in the coding section on the next page.


Using three small alligator clip leads, connect the Circuit Playground Express to the servo's wires as shown in the diagram above, plugging the male ends of the small leads into the servo connector plug.

  • Red goes from VOUT to servo red voltage wire
  • Black goes from GND to servo brown ground wire
  • Yellow goes from A3 to servo orange signal wire

Before we write the safe code, let's go ahead and try out the servo!

Copy the code below into your text editor and then save it to the Circuit Playground Express as main.py

import time
import board
import pulseio
from adafruit_motor import servo

# create a PWMOut object on Pin A3.
pwm = pulseio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.Servo(pwm)

while True:
    for angle in range(0, 180, 5):  # 0 - 180 degrees, 5 degrees at a time.
        my_servo.angle = angle
    for angle in range(180, 0, -5): # 180 - 0 degrees, 5 degrees at a time.
        my_servo.angle = angle

The servo will sweep back and forth. Very satisfying.

Next, we'll code the Combo Dial Safe with CircuitPython!

This guide was first published on Dec 11, 2017. It was last updated on Jun 20, 2024.

This page (Build the Circuit) was last updated on Mar 08, 2024.

Text editor powered by tinymce.