CircuitPython

Your board should have CircuitPython pre-installed on the Feather M4. You can test this by plugging a USB A to micro B cable into your computer and the Feather M4. It should show up as a flash drive named CIRCUITPY in the file explorer/finder. The file boot_out.txt shows which version it is running.

If you do not have a CIRCUITPY drive, it could be that the board was previously used in an Arduino project or similar. You can flash CircuitPython using the instructions at the link below.

No external libraries are required as the needed functionality for this project is built into CircuitPython itself.

Code

Download all the files for this project by clicking the Download: Project Zip link below. Copy code.py to the CIRCUITPY root directory via your computer.

# SPDX-FileCopyrightText: 2020 Eva Herrada for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
import digitalio

worm_ratio = 40/1
belt_ratio = 100/60
gear_ratio = worm_ratio * belt_ratio

steps = 200 # Steps per revolution
microsteps = 64 # Microstepping resolution
total_steps = steps * microsteps # Total microsteps per revolution

wait = 1/ ((gear_ratio * total_steps) / 86164.1)

step = digitalio.DigitalInOut(board.D6)
direct = digitalio.DigitalInOut(board.D5)

step.direction = digitalio.Direction.OUTPUT
direct.direction = digitalio.Direction.OUTPUT

direct.value = True

while True:
    step.value = True
    time.sleep(0.001)
    step.value = False
    time.sleep(wait - 0.001)

Walkthrough

In this section, I'll run through the code and what it does.

First we import the necessary libraries.

import time
import board
import digitalio

Then, we find how long we should be waiting in-between steps. This all depends on the gear ratio and how many steps per revolution the motor is making. In my case, my worm gear has a 40:1 gear ratio and my belt drive has a 100:60 gear ratio; the total gear ratio is these two values multiplied by each other, so 40*1.66, which works out to 66.67. My stepper does 200 steps per revolution, but I'm microstepping at a resolution of 64 microsteps per step, so the total steps per revolution is actually 200*64, which is 12800. I then take these values, multiply them by each other to get the number of steps I need to take in a day, and then divide that by the number of seconds in a sidereal day, 86164.1, to get the number of steps I need to take per second. I take that number and divide one by it to get how often I need to take those steps.

A sidereal day is the amount of time it takes the earth to make one rotation relative to distant stars. For more information: https://en.wikipedia.org/wiki/Sidereal_time
worm_ratio = 40/1
belt_ratio = 100/60
gear_ratio = worm_ratio * belt_ratio

steps = 200 # Steps per revolution
microsteps = 64 # Microstepping resolution
total_steps = steps * microsteps # Total microsteps per revolution

wait = 1 / ((gear_ratio * total_steps) / 86400)

Then, we assign the step and direction pins and set them to output. We set the direction pin to True so the motor moves counter-clockwise.

step = digitalio.DigitalInOut(board.D6)
direct = digitalio.DigitalInOut(board.D5)

step.direction = digitalio.Direction.OUTPUT
direct.direction = digitalio.Direction.OUTPUT

direct.value = True

Finally, we run the main loop. This loop just sends the stepper driver a pulse telling it to do a step and then waits the desired amount of time before sending another one.

while True:
    step.value = True
    time.sleep(0.001)
    step.value = False
    time.sleep(wait - 0.001)

This guide was first published on Aug 06, 2020. It was last updated on Jul 28, 2020.

This page (Code) was last updated on Jun 07, 2023.

Text editor powered by tinymce.