Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
anohren opened this issue Nov 28, 2024 · 0 comments

Comments

@anohren
Copy link

anohren commented Nov 28, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant