In this tutorial, you will learn how to move a servo using M4 Express.


Parts you need in this tutorial:

  • Adafruit Feather M4 Express
  • Male-Male jumper wire
  • USB – Micro USB connector cable
  • Breadboard
  • LiPo battery (Optional)


Step 1: Setup Circuit Python on M4 Express and Import the Library

Step 2: Building the Circuit

Step 3: Code

(Optional) Step 4: Power the servo by battery

Step 1: Setup Circuit Python on M4 Express and Import the Library

Setup CircuitPython on M4 Express.

Check out the tutorial.

In your CIRCUITPY drive, you should already have a lib folder. If not, you can simply create one yourself.

This folder is for you to add any separate libraries that are not built in.

Where can you get the libraries?

Luckily, CircuitPython already prepares you a Library Bundle.

Click here to visit their website for there latest library bundle

Download the bundle that matches with your CircuitPython version. You may check the version in the boot_out.txt file in your drive.

After downloading the library bundle and unzipping it, your library bundle may look like this.

Open the lib folder, you may see a lot of .mpy files and folders. These are the libraries that you may install on your board.

If you want to learn more about libraries, please check out here.

The library we need to use in this tutorial is adafruit_motor. Find it in the library bundle.

Drag and copy adafruit_motor into the lib folder in the CIRCUITPY drive.

You may see the library folder appear in the lib folder. Now you successfully install the library to the drive.

Step 2: Building the Circuit

Connect three jumper wires with the servo.

Connect the ground wire to Gnd pin, the power wire to USB pin, and the signal wire to A1.

Now you can upload code and power from a USB cable to your computer.

Step 3: Code

Open your file in Mu Editor.

Here is the sample code.

# NYU Abu Dhabi Interactie Media
# Desert Media Art
# Tutorial for Motor on M4 Express
# Modified by Lydia Yan

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials Servo standard servo example"""
import time
import board
import pwmio
from adafruit_motor import servo

# create a PWMOut object on Pin A1.
pwm = pwmio.PWMOut(board.A1, 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

Github: code

Now the servo is moving.

(Optional) Step 4: Power the servo by battery

Make sure you have saved your code onto the board.

Unplug the board from your computer.

Remove the power wire from the USB pin.

Plug the power wire to the battery pin (Bat). Now your board will receive power from external batteries.

Plug the LiPo battery into the battery hub.

Now your servo should be moving normally.

Categories: Tutorial


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *