Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Minimal example for running PWM with custom frequency #1

Closed
rfayat opened this issue Oct 13, 2021 · 2 comments
Closed

Minimal example for running PWM with custom frequency #1

rfayat opened this issue Oct 13, 2021 · 2 comments
Labels
wontfix This will not be worked on

Comments

@rfayat
Copy link

rfayat commented Oct 13, 2021

Hi,

I am quite new to arduino and need to run PWM with a custom frequency on a pin in a precisely timed fashion at a slow frequency (30/100Hz) on an arduino nano every in an experimental setup (I work in a neuroscience lab).

I guess this toolbox could help doing so, I am however a bit confused by the provided examples which are relatively complex.

I for instance managed to do something similar on an arduino uno using the Arduino PWM Frequency Library (which is unfortunately not compatible with arduino nano every). Using this library, I was for instance able to run PWM at 100Hz with a duty cycle of 50% on pin 3 using:

#include <PWM.h>
#define PIN_PWM 3
int32_t FREQUENCY = 100;  // frequency, Hz
byte DUTY_CYCLE = 128; // 0-255, 128 corresponds to a 50% duty cycle

void setup(){
  InitTimersSafe();
  bool success = SetPinFrequencySafe(PIN_PWM, FREQUENCY);
  pinMode(PIN_PWM, OUTPUT);
}

void loop(){
  // Start PWM on PIN_PWM
  pwmWrite(PIN_PWM, DUTY_CYCLE);
  delay(1000);
  // Stop PWM on PIN_PWM
  digitalWrite(PIN_PWM, LOW);
  delay(1000);
}

Would it be possible to provide a similar minimal example for setting up the frequency/duty cycle and running/stopping PWM on a given pin ? Or did I misunderstand the purpose of this library and should I use a different one ?

Thanks !

rfayat

@khoih-prog
Copy link
Owner

Would it be possible to provide a similar minimal example for setting up the frequency/duty cycle and running/stopping PWM on a given pin ? Or did I misunderstand the purpose of this library and should I use a different one ?

The library can certainly do that too simple job, provided you know how to program and use it.
I'm sorry this is not a place to train or write code for you. It's better that you ask in other forum for help as I don't have time now for it.

Good Luck,

@khoih-prog khoih-prog added the wontfix This will not be worked on label Oct 13, 2021
@rfayat
Copy link
Author

rfayat commented Oct 13, 2021

Ok no worries, the point was not to get you to write code for me, ISR_PWM.setPWM is documented in the README but starting the PWM is not obvious from reading the example (whose purpose is to demonstrate how powerful the library is). I simply thought that adding a minimal example could help me and others to know how to program and use the library.

Cheers

khoih-prog added a commit that referenced this issue Feb 7, 2022
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add support to `MegaCoreX` core, including ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
3. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](khoih-prog/ESP8266_PWM#1 (comment))
4. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](khoih-prog/ESP8266_PWM#2)
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
7. Update examples accordingly
khoih-prog added a commit that referenced this issue Feb 7, 2022
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add support to `MegaCoreX` core, including ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
3. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](khoih-prog/ESP8266_PWM#1 (comment))
4. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](khoih-prog/ESP8266_PWM#2)
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
7. Update examples accordingly
khoih-prog added a commit that referenced this issue Feb 7, 2022
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add support to `MegaCoreX` core, including ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
3. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](khoih-prog/ESP8266_PWM#1 (comment))
4. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](khoih-prog/ESP8266_PWM#2)
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
7. Update examples accordingly
khoih-prog added a commit that referenced this issue Feb 7, 2022
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add support to `MegaCoreX` core, including ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
3. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](khoih-prog/ESP8266_PWM#1 (comment))
4. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](khoih-prog/ESP8266_PWM#2)
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
7. Update examples accordingly
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants