Local Host
Published

Controlling a PWM Brushless ESC with Raspberry Pi Pico

Learn how to control a brushless ESC and ducted fan using a Raspberry Pi Pico and MicroPython, with safe indoor throttle limiting.

BeginnerWork in progress30 minutes46
Controlling a PWM Brushless ESC with Raspberry Pi Pico

Story

Read more

Code

Drive motor (microPython)

Python
A microPython code snippet for driving an electric motor to rotate.
from machine import Pin, PWM
import time

# Hardware configuration
PWM_PIN = 15
MAX_THROTTLE_LIMIT = 0.15  # Limit to 15% throttle

# Initialize PWM at 50Hz
esc = PWM(Pin(PWM_PIN))
esc.freq(50)

def set_duty_us(us):
    """
    Convert microsecond pulse width to Pico's 16-bit duty cycle (0-65535)
    Formula: (us / 20000) * 65535
    """
    duty = int((us / 20000) * 65535)
    esc.duty_u16(duty)

def arm_esc():
    """ESC arming sequence: send 1000us signal until ESC confirms"""
    print("Arming ESC...")
    set_duty_us(1000)
    time.sleep(3)  # Wait for startup melody to finish
    print("ESC ready")

# Main logic
try:
    arm_esc()

    throttle_rate = 0
    while throttle_rate < MAX_THROTTLE_LIMIT:
        # Calculate target pulse width, e.g. 30% throttle: 1000us + (1000us * 0.30) = 1300us
        target_us = 1000 + int(1000 * throttle_rate)
        print(f"Throttle: {throttle_rate * 100:.1f}%...")
        set_duty_us(target_us)
        time.sleep(0.4)
        throttle_rate += 0.005

    set_duty_us(1000)

except KeyboardInterrupt:
    # Force throttle to zero to prevent motor runaway
    set_duty_us(1000)
    print("\nEmergency stop")

Credits

Local Host
1 project • 0 followers

Comments