Hello,
I want to get 2 kHz pwm signal from pins 5 and 6, can you help?
you might want to take a look into this
When using the MCPWM of a ESP32 to set 2Khz just do it like so.
/* Initialize PCNT's counter */
int PCNT_H_LIM_VAL = 3000;
int PCNT_L_LIM_VAL = -10;
// 1st PCNT counter
// Anemometer
pcnt_config_t pcnt_config = {};
pcnt_config.frequency = 2000; //frequency = 2000Hz
pcnt_config.pulse_gpio_num = GPIO_NUM_15;// Set PCNT input signal and control GPIOs
pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED;
pcnt_config.channel = PCNT_CHANNEL_0;
pcnt_config.unit = PCNT_UNIT_0;
// What to do on the positive / negative edge of pulse input?
pcnt_config.pos_mode = PCNT_COUNT_INC; // Count up on the positive edge
pcnt_config.neg_mode = PCNT_COUNT_DIS; // Count down disable
// What to do when control input is low or high?
pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // Keep the primary counter mode if low
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // Keep the primary counter mode if high
// Set the maximum and minimum limit values to watch
pcnt_config.counter_h_lim = PCNT_H_LIM_VAL;
pcnt_config.counter_l_lim = PCNT_L_LIM_VAL;
pcnt_unit_config(&pcnt_config); // Initialize PCNT unit
// 12.5ns is one APB_CLK cycle 12.5*500, debounce time
pcnt_set_filter_value( PCNT_UNIT_0, 500); //Configure and enable the input filter, debounce
pcnt_filter_enable( PCNT_UNIT_0 );
pcnt_counter_pause( PCNT_UNIT_0 );
pcnt_counter_clear( PCNT_UNIT_0 );
pcnt_counter_resume( PCNT_UNIT_0); // start the show
The problem is that the OP did not specify which board
If he/she is lucky, it is indeed an ESP32 ![]()
Is it Arduino UNO or MEGA that you are using?
Are those two PWM signals having same phase?
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on (nor for problems with) your project
See About the Installation & Troubleshooting category.
I saw that the OP did not specify and decided to post the thing anyways.
I did not have a problem with that ![]()
You can post what can work ![]()
It was a good exercise and I was hoping the OP would get the hints.
Per https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm pins 5&6 on an Uno, if you are using an Uno, use Timer0, and adjusting Timer0 to from 980Hz to 2kHz would mess with timing for millis() and delay().
DPin-5 and DPin-6 of UNO uses TC0.
DPin-9 (Ch-A) and DPin-10 (Ch-B) of UNO can be comfortably used to generate the desired 2 kHz PWM signals;
Codes: for Ch-A
#define OC1A 9
void setup()
{
Serial.begin(9600);
pinMode(OC1A, OUTPUT); //Ch-A
//-------------------------------
TCCR1A = 0x00; //reset
TCCR1B = 0x00; //TC1 reset and OFF
//fOC1A/B = clckSys/(N*(1+ICR1); Mode-14 FPWM; OCR1A controls duty cycle
// 2000 Hz = 16000000/(64*(1+ICR1) N=(1,8,)64,(256,1024)==> ICR1 = 124
TCCR1A |= (1 << WGM11); //Mode-14 Fast PWM
TCCR1B |= (1 << WGM13) | (1 << WGM12); //Mode-14 Fast PWM
TCCR1A |= (1 << COM1A1) | (0 << COM1A0); //Non-invert: HIGH-LOW
ICR1 = 124; // TOP for 2000 Hz frequnecy
OCR1A = 62; //= 50% duty cycle
TCNT1 = 0;
TCCR1B |= (1 << CS11)|(1 << CS10);//TC1 statrt with N = 64;
}
void loop(){}
If using TimerOne.h Library, then the following sketch should work:
#include <TimerOne.h>
//UNO only
void setup()
{
pinMode(9, OUTPUT);
Timer1.initialize(500); // arg = period in us/2 kHz
Timer1.pwm(9, 50); // 50% DC on pin 9
}
void loop(){}
i should do it for arduino uno, wouldn't it be with timers?
dude the timer-one library for pin5 is not working.
Is it working for DPin-9? If so, then what is the harm to use it?
Is it too aggressive towards the person who helped you?
I want to get 2khz from all pwm pins. that's why i asked
forgive me, workload is a bit tiring
To get 2 kHz PWM signals, you must manipulate the TC Modules (8-bit TC0, 16-bit TC1, and 8-bit TC2) of the MCU with compliance to the PWM Modes of operations (see data sheets).
As TC0 is used by the IDE of UNO to control millis() and delay() functions, one should not try to manipulate them unless there is a standalone design.
You may observe in Fig-1 which TC Module does produce PWM signal on which DPin. The Fig-1 includes examples of PWM signals generated by analogWrite(arg1, arg2) function.
Figure-1:
The software you wrote for pin9 can be written in pin5 and pin6, so is it? this " As TC0 is used by the IDE of UNO to control millis() and delay() functions, one should not try to manipulate them unless there is a standalone design." What you said confused me.
What type of Arduino are you using?
