Libraries for controlling DC motors, servomotors, and stepper motors with MicroPython on various microcontroller platforms.
Most libraries out there only support basic movements, but here you will find more advance functionalities such as controlling the speed of the servo or moving the motors with the ability to perform other stuff (async-movement).
The DCMotor library provides a simple interface to control a DC motor using PWM signals, with support for both synchronous and asynchronous movement.
The Servo library offers an easy way to control servomotors, with support for both synchronous and asynchronous movement.
The Stepper library allows precise control of stepper motors, with support for both synchronous and asynchronous movement.
- Control motor speed and direction using two PWM pins.
- Fast and slow decay modes.
- Control servomotor angles with optional speed and asynchronous movement.
- Control stepper motors using digital or PWM pins.
- Support for various stepping methods:
SINGLE
,DOUBLE
,INTERLEAVE
,MICROSTEP
. - Ability to perform continuous movements.
Ensure you have MicroPython installed on your microcontroller. Upload the relevant Python files (dc_motor.py
, servo.py
, Stepper.py
) to your microcontroller.
First, import the required libraries:
from DCMotor import DCMotor
from Servo import Servo
from Stepper import Stepper
Create an instance of the DCMotor
class by specifying the pins for speed and direction control, the PWM frequency, and the decay mode.
# Initialize the motor with fast decay mode
motor = DCMotor(pina=0, pinb=1, freq=50, fast=True)
Set the motor speed and direction:
motor.speed(-50) # Set speed (range: -100 to 100)
Stop the motor:
motor.stop()
Create a Servo object by specifying the pin to which the servomotor is connected. Optional parameters include the start angle, minimum and maximum angles, PWM frequency, and minimum and maximum pulse widths.
servo = Servo(pin=2, start=0)
Use the move
method to move the servomotor to a specific angle. Optionally, you can specify the movement speed and asynchronous mode.
servo.move(90)
servo.move(90, speed=30, async_mode=True)
Use the goal_reached
method to check if the servomotor has reached the target angle.
if servo.goal_reached():
print("The servo has reached the target.")
Use the stop
method to stop the servomotor's movement.
servo.stop()
Use the detach
method to detach the servomotor.
servo.release()
Create a Stepper
object by specifying the pins for the coils and, optionally, the number of microsteps.
motor = Stepper(ain1=12, ain2=13, bin1=14, bin2=15)
ain1
,ain2
,bin1
,bin2
: Pin numbers for coil connectionsmicrosteps
: Number of microsteps (optional, default is None for digital outputs)
motor.onestep(direction=FORWARD, style=SINGLE)
direction
: EitherFORWARD
orBACKWARD
style
: One of the stepping methods (SINGLE
,DOUBLE
,INTERLEAVE
,MICROSTEP
)
motor.step(200, direction=FORWARD, style=SINGLE, rpm=60)
steps
: Number of steps to performdirection
: EitherFORWARD
orBACKWARD
style
: One of the stepping methods (SINGLE
,DOUBLE
,INTERLEAVE
,MICROSTEP
)rpm
: Revolutions per minute
motor.angle(90, direction=FORWARD, style=SINGLE, rpm=60)
angle
: Angle in degrees
motor.continuous(direction=FORWARD, style=SINGLE, rpm=60)
direction
: EitherFORWARD
orBACKWARD
style
: One of the stepping methods (SINGLE
,DOUBLE
,INTERLEAVE
,MICROSTEP
)rpm
: Revolutions per minute
motor.stop()
motor.release()
Here is a complete example of using the DCMotor library:
from DCMotor import DCMotor
import time
motor = DCMotor(pina=0, pinb=1, freq=50, fast=True)
motor.speed(100) # Set speed
time.sleep(5) # Run for 5 seconds
motor.stop() # Stop the motor
Here is a complete example of using the Servo library:
from servo import Servo
import time
servo = Servo(pin=2, start=0)
servo.move(90, speed=30, async_mode=True) # 30 degrees per second
time.sleep(5) # Wait for 5 seconds to let the servo move
servo.stop()
Here is a complete example of using the Stepper library:
from Stepper import Stepper
import time
motor = Stepper(ain1=12, ain2=13, bin1=14, bin2=15, microsteps=16)
motor.step(steps=200, direction=FORWARD, style=SINGLE, rpm=60) # Move 200 steps
time.sleep(5) # Wait for 5 seconds
motor.stop() # Stop the motor
These libraries support platforms that can use machine.Pin
, machine.PWM
, and _thread
modules in MicroPython.
These projects are licensed under the MIT License - see the LICENSE file for details.