Skip to content

Question: Why does updating the duty of PWM with a period of 500 give me this result? #142

@anohren

Description

@anohren

Board Type

RaspberryPi3

Operating System

Raspberry Pi OS bookworm arm64 lite

Swift Version

6.0.1 release build

Description

Describe your hardware setup (e.g. board connected to i2c sensor) and describe briefly the issue, specifying the steps needed to reproduce it, if necessary.

Gpio18 → LED → 220 Ohm → GND

I was trying out the PWM example in the readme with its default 500 ns period argument by making an LED pulse softly. What I experienced was that instead of my expected 20 updates per second of the duty cycle, that I got a "5 FPS animation" so to speak — it didn't pulse softly but instead jumped about 5 times/second to distinct brightnesses.

I simply used the example value of 500 and scratched my head for a while before trying changing it to 1000, after which everything worked smoothly and the LED pulsed beautifully.

I'm wondering if anyone can explain why this happens? Is 500 not a reasonable value when updating duty this often?

import Foundation import SwiftyGPIO let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)! let pwm = (pwms[0]?[.P18])! pwm.initPWM() let breathingPeriod = 2 let stepsPerSecond = 20 let stepsPerPeriod = breathingPeriod * stepsPerSecond let pwmPeriodNs = 500 // changing this to 1000 solves the problem while true { for i in 0...stepsPerPeriod { let duty = Float(Double(30) * abs(cos(Double(i) / Double(stepsPerPeriod) * Double.pi))) pwm.startPWM(period: pwmPeriodNs, duty: duty) usleep(UInt32(breathingPeriod * 1_000_000/stepsPerPeriod)) } } 

Also, I read in the readme:

This feature uses the M/S algorithm and has been tested with signals with a period in a range from 300ns to 200ms

I'm not sure exactly what this means. I didn't find this algorithm in a search — what is it referring to? Since this is hardware PWM, is it a hardware implemented algorithm?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions