Hi,
Just moving over from ATmega32u4 based boards to the ItsyBitsy M0. I'm looking for a pwm library for C code.
I see there is one for CircuitPython, but I don't really want to re-write my code in Python if I can help it.
Is there such a thing as pwmio.h?
Thanks.
ItsyBitsy M0 PWM library for C.
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- mikeysklar
- Posts: 13936
- Joined: Mon Aug 01, 2016 8:10 pm
Re: ItsyBitsy M0 PWM library for C.
You can always use analogWrite().
https://docs.arduino.cc/learn/microcont ... log-output
Have you looked at the SAMD21 (M0) turbo PWM library?
https://www.arduino.cc/reference/en/lib ... turbo-pwm/
https://docs.arduino.cc/learn/microcont ... log-output
Have you looked at the SAMD21 (M0) turbo PWM library?
https://www.arduino.cc/reference/en/lib ... turbo-pwm/
- Herrsmitz
- Posts: 3
- Joined: Sun Feb 13, 2022 10:19 am
Re: ItsyBitsy M0 PWM library for C.
Thanks for that. I had missed the turbo PWM library.
The default analogWrite uses values up to 255 i.e. an 8 bit resolution. The turbo PWM library has values up to 1000 i.e. more or less 10 bit.
I'm currently using the PCA9685 with 12 bit resolution. I was hoping to make full use of the M0's 16 bit resolution.
Now that I have a library I can probably make some hacking to get there.
The default analogWrite uses values up to 255 i.e. an 8 bit resolution. The turbo PWM library has values up to 1000 i.e. more or less 10 bit.
I'm currently using the PCA9685 with 12 bit resolution. I was hoping to make full use of the M0's 16 bit resolution.
Now that I have a library I can probably make some hacking to get there.
- mikeysklar
- Posts: 13936
- Joined: Mon Aug 01, 2016 8:10 pm
Re: ItsyBitsy M0 PWM library for C.
Okay, I should have asked if you were using the PCA9685 chip. There is an Adafruit library for that.
https://github.com/adafruit/Adafruit-PW ... er-Library
https://github.com/adafruit/Adafruit-PW ... er-Library
- westfw
- Posts: 2008
- Joined: Fri Apr 27, 2007 1:01 pm
Re: ItsyBitsy M0 PWM library for C.
I get the impression that they are trying to eliminate the PCA9685 by being able to use the M0's higher-resolution timers instead.
With a full 16bits resolution, you'd get a maximum frequency of ~700Hz (48MHz/65536) Is that going to be sufficient
(Hmm. Maybe twice that?)
I don't see why the turbopwm library can't do 16bit resolution with a simple modification.
It looks like the "duty cycle" is intentionally abstracted to 1-1000, but the timer is operating in at least 16bit mode anyway.
See https://github.com/ocrdu/Arduino_SAMD21 ... M.cpp#L144
With a full 16bits resolution, you'd get a maximum frequency of ~700Hz (48MHz/65536) Is that going to be sufficient
(Hmm. Maybe twice that?)
I don't see why the turbopwm library can't do 16bit resolution with a simple modification.
It looks like the "duty cycle" is intentionally abstracted to 1-1000, but the timer is operating in at least 16bit mode anyway.
See https://github.com/ocrdu/Arduino_SAMD21 ... M.cpp#L144
- Herrsmitz
- Posts: 3
- Joined: Sun Feb 13, 2022 10:19 am
Re: ItsyBitsy M0 PWM library for C.
Hi,
I only need maximum 333Hz for servo control. The main requirement for 16 bit is that it is only possible to use a small part of the PWM range. Servo's need a pulse width of 1ms to 2ms. At 333Hz, the PWM period is 3ms, so one can only use 1/3 of the PWM period. This knocks the PCA9685's 12 bits down to 10.5 bits. If you run the servo with a slower period like 50 Hz (more typical). then you end up with 1/20 of the range, so using a 16 bit PWM helps to recover some of the resolution in moving the arm.
For those interested, you can change the _maxDutyCycle constant from 1000 to 65536, then you get 16 bit PWM control (albeit with a limited maximum frequency).
In the SAMD21turboPWM.h file modify:
const unsigned int _maxDutyCycle = 1000; // The maximum duty cycle number; duty cycle will be (dutyCycle / _maxDutyCycle) * 100%
to:
const unsigned int _maxDutyCycle = 65536; // The maximum duty cycle number; duty cycle will be (dutyCycle / _maxDutyCycle) * 100%
Thanks all for your comments.
I only need maximum 333Hz for servo control. The main requirement for 16 bit is that it is only possible to use a small part of the PWM range. Servo's need a pulse width of 1ms to 2ms. At 333Hz, the PWM period is 3ms, so one can only use 1/3 of the PWM period. This knocks the PCA9685's 12 bits down to 10.5 bits. If you run the servo with a slower period like 50 Hz (more typical). then you end up with 1/20 of the range, so using a 16 bit PWM helps to recover some of the resolution in moving the arm.
For those interested, you can change the _maxDutyCycle constant from 1000 to 65536, then you get 16 bit PWM control (albeit with a limited maximum frequency).
In the SAMD21turboPWM.h file modify:
const unsigned int _maxDutyCycle = 1000; // The maximum duty cycle number; duty cycle will be (dutyCycle / _maxDutyCycle) * 100%
to:
const unsigned int _maxDutyCycle = 65536; // The maximum duty cycle number; duty cycle will be (dutyCycle / _maxDutyCycle) * 100%
Thanks all for your comments.
Please be positive and constructive with your questions and comments.