# To get a PWM signal with 5khz frequency using Arduino Uno

**URL:** <https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584>\
**Category:** Programming\
**Created:** [September 12, 2021, 3:45am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584 "2021-09-12T03:45:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gowtham\_c\_r](https://avatars.discourse-cdn.com/v4/letter/g/9dc877/32.png) [@gowtham\_c\_r](https://forum.arduino.cc/u/gowtham_c_r)\
**Post date:** [September 12, 2021, 3:45am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/1 "2021-09-12T03:45:34Z")

</div>

Hi,  
I have been trying to generate a PWM 5khz frequency whose duty cycle varies.  
I my project I'm trying to control my motor using Arduino. Where I sense the angle, 3-phase current using sensors and get them as inputs.  
Now, with the help of angle I found the speed then using PI- controller in Arduino I compared ref speed to the actual speed and give the output error to the PI controller. The output of this controller is the reference current which I then compare with all three current inputs and the error would be fed to another PI controller.  
Now, these 3 PI controllers gives 3 different PWM duty cycle values( from 0 to 1 ).  
Now, I need these duty cycle to work as a PWM signal with 5khz frequency) or in other words I need to compare these values with a 5khz repeating sequence.  
I have attached my code below for the reference.

```cpp
#include <PID_v2.h>
#define COUNT_PER_REV 600
#define ENCODER_A 2
#define ENCODER_B 3

boolean Dir = true;

volatile long pulse_count = 0;

int a = 90;
int b = 30;
int c = 60;
int val = 0;
int num = 0;
int count = 1;
int potPin = 0;
int Position = 0;
int interval = 1000;
int refcurrent = 0;
int sensitivity = 100;
int offsetVoltage = 1650;
int rotationalAngle = 0;

const int Ia = A0;
const int Ib = A2;
const int Ic = A3;
const int IS = A4;

long currentMillis = 0;
long previousMillis = 0;

float rpm = 0;
float erroRS = 0;
float erroR1 = 0;
float erroR2 = 0;
float erroR3 = 0;
float refrpm = 0;
float avgcurrent1 = 0;
float avgcurrent2 = 0;
float avgcurrent3 = 0;
float avgcurrentS = 0;
float prevcurrent1 = 0;
float prevcurrent2 = 0;
float prevcurrent3 = 0;
float prevcurrentS = 0;

double adcValue1 = 0.0;
double adcValue2 = 0.0;
double adcValue3 = 0.0;
double adcValueS = 0.0;
double duty_cycle1 = 0.0;
double duty_cycle2 = 0.0;
double duty_cycle3 = 0.0;
double adcvoltage1 = 0.0;
double adcvoltage2 = 0.0;
double adcvoltage3 = 0.0;
double adcvoltageS = 0.0;
double currentValue1 = 0.0;
double currentValue2 = 0.0;
double currentValue3 = 0.0;
double currentValueS = 0.0;
double Kp = 0.000541, Ki = 0.042301, Kd = 0;
double kp = 0.001221, ki = 0.034444, kd = 0;

PID_v2 mySDPID(Kp, Ki, Kd, PID::Direct);
PID_v2 myI1PID(kp, ki, kd, PID::Direct);
PID_v2 myI2PID(kp, ki, kd, PID::Direct);
PID_v2 myI3PID(kp, ki, kd, PID::Direct);

void setup()
{
  Serial.begin(9600); 
  pinMode(5, OUTPUT);
  pinMode(ENCODER_A , INPUT_PULLUP);
  pinMode(ENCODER_B , INPUT_PULLUP); 
  pinMode(Ia,INPUT);
  pinMode(Ib,INPUT);
  pinMode(Ic,INPUT);
  pinMode(IS,INPUT);
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  attachInterrupt(digitalPinToInterrupt(ENCODER_A), pulse, RISING);
  attachInterrupt(digitalPinToInterrupt(ENCODER_A), encoderA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENCODER_B), encoderB, CHANGE);
  
}

void loop() 
{
    mySDPID.Start(erroRS,0,0);
    myI1PID.Start(erroR1,0,0);
    myI2PID.Start(erroR2,0,0);
    myI3PID.Start(erroR3,0,0);
    currentMillis = millis();
    refrpm = (analogRead(potPin)*500)/1023;
    previousMillis = currentMillis;
    rpm = float(pulse_count * 60 / COUNT_PER_REV);
    rotationalAngle = (360 * Position) / (600 * 4);
    num = rotationalAngle % 90;
    adcValue1 = analogRead(Ia);
    adcValue2 = analogRead(Ib);
    adcValue3 = analogRead(Ic);
    adcValueS = analogRead(IS);
    adcvoltage1 = ((adcValue1/1023)*3300);
    adcvoltage2 = ((adcValue2/1023)*3300);
    adcvoltage3 = ((adcValue3/1023)*3300);
    adcvoltageS = ((adcValueS/1023)*3300);
    currentValue1 = ((adcvoltage1 - offsetVoltage)/ sensitivity);  
    currentValue2 = ((adcvoltage2 - offsetVoltage)/ sensitivity);
    currentValue3 = ((adcvoltage3 - offsetVoltage)/ sensitivity);
    currentValueS = ((adcvoltageS - offsetVoltage)/ sensitivity);
    erroR1 = (refcurrent - currentValue1);
    erroR2 = (refcurrent - currentValue2);
    erroR3 = (refcurrent - currentValue3);

    if (num == 90 || num == 0)
    {
      count = 1;
    }
    else
    {
      count++;
    }
      
    avgcurrent1 = (prevcurrent1 + currentValue1)/count;
    avgcurrent2 = (prevcurrent2 + currentValue2)/count;
    avgcurrent3 = (prevcurrent3 + currentValue3)/count;
    avgcurrentS = (prevcurrentS + currentValueS)/count;
    
    if (num == (a))
      {
        prevcurrent1 = 0;
        prevcurrent2 = 0;
        prevcurrent3 = 0;
        prevcurrentS = 0;
      }
    else
      {
        prevcurrent1 = avgcurrent1*count;
        prevcurrent2 = avgcurrent2*count;
        prevcurrent3 = avgcurrent3*count;
        prevcurrentS = avgcurrentS*count;
      }
      
    pulse_count = 0;
    
    if(refrpm > rpm)
      {
        if((refrpm - rpm)>10)
          {
            erroRS = 10;
          }
        else
          {
            erroRS = (refrpm - rpm);
          }
      }
    else
      {
        if((rpm - refrpm) > 10)
          {
            erroRS = -10;
          }
        else
          {
            erroRS = rpm - refrpm;
          }
      }
    if(erroR1>(0.02*refcurrent))
    {
      erroR1=0.02*refcurrent;
    }
    if(erroR2>(0.02*refcurrent))
    {
      erroR2=0.02*refcurrent;
    }
    if(erroR3>(0.02*refcurrent))
    {
      erroR3=0.02*refcurrent;
    }
    mySDPID.SetMode(AUTOMATIC);
    myI1PID.SetMode(AUTOMATIC);
    myI2PID.SetMode(AUTOMATIC);
    myI3PID.SetMode(AUTOMATIC);
    mySDPID.SetOutputLimits(0,1);
    myI1PID.SetOutputLimits(0,1);
    myI2PID.SetOutputLimits(0,1);
    myI3PID.SetOutputLimits(0,1);
    refcurrent = mySDPID.Run(erroRS);
    duty_cycle1 = myI1PID.Run(erroR1);
    duty_cycle2 = myI2PID.Run(erroR2);
    duty_cycle3 = myI3PID.Run(erroR3);

}

void encoderA()
    {
      if(digitalRead(ENCODER_A) != digitalRead(ENCODER_B))
        {
          Position++;
        }
      else
        {
          Position--;
        }
    }
void encoderB()
  {
    if (digitalRead(ENCODER_A) == digitalRead(ENCODER_B))
        {
          Position++;
        }
    else
        {
          Position--;
        }
  }

void pulse() 
  {
    val = digitalRead(ENCODER_B);
    if(val == LOW) 
      {
        Dir = false;
      }
    else 
      {
        Dir = true;
      }
     
    if (Dir) 
      {
        pulse_count++;
      }
    else 
      {
        pulse_count--;
      }
  }

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [September 12, 2021, 3:50am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/2 "2021-09-12T03:50:13Z")

</div>

Please edit your post to add code tags. Select the code and use the "\</\>" editor button.

You generally should not do I/O of any kind in an interrupt routine, and **especially** not serial I/O, as it depends on interrupts.

Remove this line from the ISR:

```cpp
Serial.println(num);

```

---

<div class="post-metadata">

**Author:** ![gowtham\_c\_r](https://avatars.discourse-cdn.com/v4/letter/g/9dc877/32.png) [@gowtham\_c\_r](https://forum.arduino.cc/u/gowtham_c_r)\
**Post date:** [September 12, 2021, 4:01am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/3 "2021-09-12T04:01:47Z")

</div>

Thank you, I have removed the serial and the ISR too right now I just need the dity\_cycle\_1,2,3 output to work at 5lhz

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [September 13, 2021, 9:23pm UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/4 "2021-09-13T21:23:23Z")

</div>

To get three channels of PWM on an UNO you need to use two timers. Pick a PWM mode and calculate the value of TOP that will get you the 5kHz frequency.

---

<div class="post-metadata">

**Author:** ![gowtham\_c\_r](https://avatars.discourse-cdn.com/v4/letter/g/9dc877/32.png) [@gowtham\_c\_r](https://forum.arduino.cc/u/gowtham_c_r)\
**Post date:** [September 14, 2021, 2:43am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/5 "2021-09-14T02:43:14Z")

</div>

As I'm new to Arduino and I am not that good with the coding I don't know how to use timers to generate pwm with the 3 dutycycles that I have.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [September 14, 2021, 4:24am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/6 "2021-09-14T04:24:27Z")

</div>

I recommend to study this **excellent** tutorial on timers: [https://www.gammon.com.au/timers](https://www.gammon.com.au/timers)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 12, 2022, 4:25am UTC](https://forum.arduino.cc/t/to-get-a-pwm-signal-with-5khz-frequency-using-arduino-uno/904584/7 "2022-01-12T04:25:18Z")

</div>

Closed on January 12, 2022, 4:25am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
