It's easy to use the A4988 Stepper Motor Driver with CircuitPython and the digitalio core module. This module allows you to easily write Python code to access input and outputs on GPIO pins.
You can use the example code with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
Here is how you'll wire the breakout to a Feather RP2040 and stepper motor:
Check your stepper motor wiring - your motor may have different wire colors or wire order.
- Stepper motor power supply positive to breakout terminal block + (red wire)
- Stepper motor power supply negative to breakout terminal block - (black wire)
- Breakout VDD to Feather 3.3V (red wire)
- Breakout GND to Feather GND (black wire)
- Breakout DIR to Feather pin 5 (blue wire)
- Breakout STEP to Feather pin 6 (orange wire)
- Breakout 1A to stepper motor coil 1 positive (green wire)
- Breakout 1B to stepper motor coil 1 negative (yellow wire)
- Breakout 2A to stepper motor coil 2 positive (red wire)
- Breakout 2B to stepper motor coil 2 negative (black wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the breakout and a stepper motor:
Check your stepper motor wiring - your motor may have different wire colors or wire order.
- Stepper motor power supply positive to breakout terminal block + (red wire)
- Stepper motor power supply negative to breakout terminal block - (black wire)
- Breakout VDD to Pi 3.3V (red wire)
- Breakout GND to Pi GND (black wire)
- Breakout DIR to Pi GPIO 5 (blue wire)
- Breakout STEP to Pi GPIO 6 (orange wire)
- Breakout 1A to stepper motor coil 1 positive (green wire)
- Breakout 1B to stepper motor coil 1 negative (yellow wire)
- Breakout 2A to stepper motor coil 2 positive (red wire)
- Breakout 2B to stepper motor coil 2 negative (black wire)
Python Blinka Setup
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
CircuitPython Usage
To use with CircuitPython, you need to update code.py with the example script.
In the example below, click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip file, and copy the code.py file to your CIRCUITPY drive.
There are no additional libraries needed for this example. It uses core modules that are included when you install CircuitPython on your board.
Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from digitalio import DigitalInOut, Direction # direction and step pins as outputs DIR = DigitalInOut(board.D5) DIR.direction = Direction.OUTPUT STEP = DigitalInOut(board.D6) STEP.direction = Direction.OUTPUT # microstep mode, default is 1/16 so 16 # another ex: 1/4 microstep would be 4 microMode = 16 # full rotation multiplied by the microstep divider steps = 200 * microMode while True: # change direction every loop DIR.value = not DIR.value # toggle STEP pin to move the motor for i in range(steps): STEP.value = True time.sleep(0.001) STEP.value = False time.sleep(0.001) print("rotated! now reverse") # 1 second delay before starting again time.sleep(1)
The code starts by setting up the direction and step pins as outputs. In the loop, the direction pin will toggle once every loop to change directions. The step pin toggles in a for
loop to step the motor. As the code runs, you'll see your attached stepper motor turn clockwise and then reverse and turn counterclockwise.
Text editor powered by tinymce.