| Crates.io | spwm |
| lib.rs | spwm |
| version | 0.1.6 |
| created_at | 2025-11-22 16:39:50.890742+00 |
| updated_at | 2025-11-22 23:38:21.502157+00 |
| description | Software PWM for microcontrollers |
| homepage | |
| repository | https://github.com/vpetrigo/spwm |
| max_upload_size | |
| id | 1945472 |
| size | 55,931 |
A no_std Rust library for generating software-based Pulse Width Modulation (PWM) signals on microcontrollers and
embedded systems. This crate provides a flexible, interrupt-driven PWM implementation that doesn't require dedicated
hardware PWM peripherals.
no_std compatible - Works in embedded environments without the standard libraryAdd this to your Cargo.toml:
[dependencies]
soft-pwm = "0.1"
use spwm::{Spwm, SpwmState};
// Create SPWM manager with hardware timer frequency of 100 kHz
// and space for 4 channels
let mut spwm = Spwm::<4>::new(100_000);
// Create a channel with 1 kHz frequency and 50% duty cycle
let channel = spwm
.create_channel()
.freq_hz(1_000)
.duty_cycle(50)
.on_off_callback(|state: &SpwmState| {
match state {
SpwmState::On => {
// Turn your output pin HIGH
}
SpwmState::Off => {
// Turn your output pin LOW
}
}
})
.period_callback(|| {
// Called at the end of each PWM period
})
.build()?;
let channel_id = spwm.register_channel(channel)?;
// Enable the channel to start PWM generation
spwm.get_channel(channel_id).unwrap().enable()?;
#[interrupt]
fn TIMER_IRQ() {
spwm.irq_handler();
}
That example configures 4 channels:
Oscillogram that shows PC9 and PC8 output waveforms:

Simple LED PWM control example:
use spwm::{Spwm, SpwmState};
static mut LED_STATE: bool = false;
fn led_callback(state: &SpwmState) {
LED_STATE = matches!(state, SpwmState::On);
// Update your LED pin based on LED_STATE
}
let mut pwm = Spwm::<1>::new(100_000);
let channel = pwm
.create_channel()
.freq_hz(100) // 100 Hz PWM frequency
.duty_cycle(25) // 25% brightness
.on_off_callback(led_callback)
.period_callback(|| {})
.build()?;
let id = pwm.register_channel(channel)?;
pwm.get_channel(id).unwrap().enable()?;