Skip to main content

A library for controlling hobby servos with PWM

Project description

rcservo logo

rcservo

PyPI version License: MIT MicroPython Compatible

A lightweight, zero-dependency Python library for controlling hobby servo motors and RC servos with PWM (Pulse Width Modulation). Works seamlessly on CPython and MicroPython platforms.

Keywords: micropython rc servo, micropython hobby servo, servo control, PWM, robotics

Features

  • ✨ Simple, intuitive API for servo control
  • 🎯 Angle-based and PWM pulse-width control
  • 🔧 Flexible configuration for different servo types and ranges
  • 🐍 Works on both standard Python and MicroPython (Raspberry Pi Pico, ESP32, etc.)
  • 📦 Zero external dependencies
  • ⚡ Lightweight and efficient

Installation

Install from PyPI:

pip install rcservo

For MicroPython

Copy rcservo.py to your MicroPython device:

# Using mpremote (Raspberry Pi Pico, etc.)
mpremote cp rcservo.py :

# Or using ampy (ESP32, ESP8266, etc.)
ampy --port /dev/ttyUSB0 put rcservo.py

Quick Start

from rcservo import Servo

# Create a servo instance on GPIO pin 12
servo = Servo(pin=12)

# Set servo angle (-90 to +90 degrees)
servo.angle(45)

# Set servo angle to neutral (0 degrees)
servo.angle(0)

# Set servo angle to far left (-90 degrees)
servo.angle(-90)

Usage

Basic Initialization

from rcservo import Servo

# Default configuration (1000-2000 µs pulse width, -90 to +90 degrees)
servo = Servo(pin=12)

Custom Configuration

# Custom servo with different pulse width ranges
servo = Servo(
    pin=12,
    min_pulse=800,      # Minimum pulse width in microseconds
    max_pulse=2200,     # Maximum pulse width in microseconds
    min_angle=-120,     # Minimum angle
    max_angle=120       # Maximum angle
)

Control Methods

# Set servo angle
servo.angle(45)  # Move to 45 degrees

# Set servo PWM pulse width directly (in microseconds)
servo.pwm(1500)  # Set to 1500 µs pulse

Hardware Requirements

  • Servo motor: Standard hobby servo or RC servo with PWM control
  • Microcontroller: Any board with PWM GPIO output capability:
    • Raspberry Pi Pico (MicroPython)
    • ESP32 / ESP8266 (MicroPython)
    • Arduino (with Python via Micropython)
    • Raspberry Pi / BeagleBone (standard Python)
    • Any board supported by CPython or MicroPython
  • Power supply: Appropriate for your servo (typically 4.8–6V for hobby servos)

API Reference

Servo Class

__init__(pin, min_pulse=1000, max_pulse=2000, min_angle=-90, max_angle=90)

Initialize a servo on the specified pin.

Parameters:

  • pin (int): GPIO pin number
  • min_pulse (int): Minimum pulse width in microseconds. Default: 1000
  • max_pulse (int): Maximum pulse width in microseconds. Default: 2000
  • min_angle (int): Minimum angle. Default: -90
  • max_angle (int): Maximum angle. Default: 90

angle(angle)

Set the servo to a specific angle.

Parameters:

  • angle (float or int): Target angle. Will be clamped between min_angle and max_angle.

pwm(pwm)

Set the servo pulse width directly.

Parameters:

  • pwm (int or float): Pulse width in microseconds. Will be clamped between min_pulse and max_pulse.

Notes

  • Angles are clamped to the configured min/max range
  • Pulse widths are clamped to the configured min/max range
  • Standard hobby servo pulse: 1000–2000 µs for −90 to +90 degrees
  • 1500 µs corresponds to neutral (0 degrees) by default
  • MicroPython implementation is memory-efficient and suitable for embedded devices

Use Cases

  • Robotics: Pan-tilt camera mounts, robot arms, legs
  • RC Projects: Model airplanes, cars, boats with servo control
  • IoT: Home automation with servo-controlled blinds, doors, valves
  • Education: Teaching microcontroller PWM and servo control concepts
  • Hobbyist Projects: Camera sliders, gimbal systems, mechanical displays

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License — See LICENSE file for details.

Author

Anton Vanhoucke

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rcservo-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rcservo-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file rcservo-0.1.0.tar.gz.

File metadata

  • Download URL: rcservo-0.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for rcservo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0538ed42c35e4843adf72dd686f784da36f45c972890fdaaf3cb79648936cd95
MD5 492038f455b835371a1ffda740aa8ca9
BLAKE2b-256 f2e2351c98f61ad162c06b17330d0eef46c974e848523ee06dfa2a72b8fb1cf9

See more details on using hashes here.

File details

Details for the file rcservo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rcservo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for rcservo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 09b330875f248acd7970e09f2f3d1d38700e12162dd01b80b8277426f7265fb6
MD5 f9b8bbcfe995362f0a96cb5c780e7526
BLAKE2b-256 c530a76c2ba655bd5aca86c827b62538aa839bd38debc060c53b1b7c5763ee3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page