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

Hardware PWM? #129

Open
shetty-777 opened this issue Oct 29, 2023 · 1 comment
Open

Hardware PWM? #129

shetty-777 opened this issue Oct 29, 2023 · 1 comment

Comments

@shetty-777
Copy link

I tried my best (I am a complete noob) but couldn't get it to work. I tried the hardware_PWM() function (is it a class?) of pigpio. It worked independently when I ran it, just set a frequency of 50 and duty of 500000. But it tried to go over the limit of my servo. And when I replaced the set_PWM_frequency() in plotter.py with the hardware_PWM class, nothing happened. I just thought hardware PWM would be more reliable and accurate, and when it is available on the RPi (on 2 channels, 4 pins) why not use each channel for elbow and shoulder? Has anybody tried it? Or did I just waste my time and it is useless?

@evildmp
Copy link
Owner

evildmp commented Dec 16, 2024

Your duty cycle of 500'000 is an order of magnitude too big.

A duty cycle of 500'000 is 50% of the time. At 50Hz a full cycle is 20'000 microseconds, so that means a duty cycle of 10'000 microseconds.

The formula you need is:

duty_cycle = PWM_freq * pulse_width

Where the duty_cycle is out of 1 million, PWM_freq is Hz, and pulse_width is in microseconds.

A 1500 microsecond duty cycle would be 75'000 (at 50Hz).

I have tried this and it works as expected to swing the arm back and forth, where b.rpi is my instance:

pwm_freq = 50
start = 600
end = 2400
step = 1
x = (*range(start, end, step), *range(end, start, -step))
while True:
    for pw in x: 
        b.rpi.hardware_PWM(18, pwm_freq, pw*pwm_freq)

Is there any advantage? I don't know. See What advantage is there in using hardware PWM with pigpio? on StackExchange.

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

2 participants