Generating PWM from PID to control DC motor

please help to generate PWM from PID using arduino i m not good programmer

  1. download the PID library and study the documentation
  2. try the Basic Example

i read that most exmples are in analogwrite but i want to generate using digitalwrite because i want to change the time period of pwm

i want to change the time period of pwm

Why would you want to do that?

actually am working on buck-boost converter to control speed of dc motor so i need to confirm that pwm i sending from arduino is 20khertz

The timers can be programmed to deliver PWM over a very large range of frequencies. See the MCU data sheet and this tutorial for details.

i knw timer but i am confused in PID output , that how pid output can be converted into our pwm

actually its close loop converter with PID feedback

Feedback isn't PID. It's what you do with feedback (error signal), via the control algorithm, to create a PID output response.

PID is feedback, its not a feedback signal though. Feedback is the whole caboodle of closed loop
control.

i knw timer but i am confused in PID output , that how pid output can be converted into our pwm

I don't understand the difficulty - the output is the PWM drive.

  analogWrite (pin, pid_output) ;

sir i m confused if i write analogwrite(pin,pid output) then how i configure that output wave is square wave and its frequency is 20kh and i m not good programmer main problem is that

i m not good programmer main problem is that

You have two options:

  1. pay someone to write a program for you
  2. learn to write the program.

Arduino is a fine learning platform, with plenty of simple examples to help get you started.

The normal frequency for Arduino PWM is 490 Hz (I think). You can change that, as mentioned in Reply #5.

When you have changed to frequency you can then use analogWrite() as normal. (By the way "analogWrite()" is not a very good description of what the function does - pwmGenerate() might have been a better name.)

...R

jremington:
You have two options:

  1. pay someone to write a program for you
  2. learn to write the program.

Arduino is a fine learning platform, with plenty of simple examples to help get you started.

thnz sir i m trying to learn coding and thats why i posted this here so i get help in learning

Robin2:
The normal frequency for Arduino PWM is 490 Hz (I think). You can change that, as mentioned in Reply #5.

When you have changed to frequency you can then use analogWrite() as normal. (By the way "analogWrite()" is not a very good description of what the function does - pwmGenerate() might have been a better name.)

...R

Sir i didnt find pwmGenerate() in arduino website if u hav anylink please share and am trying to implement manually like this
void loop()
{
digitalWrite(13, HIGH);
delayMicroseconds(10); // Approximately 10% duty cycle @ 1KHz
digitalWrite(13, LOW);
delayMicroseconds(40);
}
only problem i m struck with PID OUTPUT that how that output can be converted into in range of my time period 50us if i get that above code willbe implement i hope so

If you do the modulation in a software loop like that your code cannot do anything else, such as
run the PID algorithm.

Answer #5 guides to how to set the frequency, and more control of the PWM units. That's definitely
how to do this. The hardware generates the PWM, the software regularly updates the duty cycle
(and even frequency if that's needed) from the PID output. The software has a high-level role, the
hardware does the grunt work.

naeemawan36:
Sir i didnt find pwmGenerate() in arduino website

Of course you didn't. It does not exist. I just said that it might have been a more meaningful name for the function that is now called analogWrite().

...R