Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: naeemawan36 on Sep 06, 2016, 04:50 pm

Title: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 06, 2016, 04:50 pm
please help to generate PWM from PID using arduino i m not good programmer
Title: Re: Generating PWM from PID to control DC motor
Post by: jremington on Sep 06, 2016, 05:29 pm
1. download the PID library and study the documentation
2. try the Basic Example (http://playground.arduino.cc/Code/PIDLibaryBasicExample)
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 06, 2016, 07:02 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: jremington on Sep 06, 2016, 07:30 pm
Quote
i want to change the time period of pwm
Why would you want to do that?
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 06, 2016, 07:40 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: jremington on Sep 06, 2016, 09:06 pm
The timers can be programmed to deliver PWM over a very large range of frequencies. See the MCU data sheet and this tutorial (http://www.gammon.com.au/timers) for details.
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 07, 2016, 07:02 am
i knw timer but i am confused in PID output , that how pid output can be converted into our pwm
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 07, 2016, 07:18 am
actually its close loop converter with PID feedback
Title: Re: Generating PWM from PID to control DC motor
Post by: jackrae on Sep 07, 2016, 08:31 am
Feedback isn't PID.  It's what you do with feedback (error signal), via the control algorithm, to create a PID output response.
Title: Re: Generating PWM from PID to control DC motor
Post by: MarkT on Sep 07, 2016, 07:22 pm
PID is feedback, its not a feedback signal though.  Feedback is the whole caboodle of closed loop
control.

Quote
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.
Code: [Select]

  analogWrite (pin, pid_output) ;
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 08, 2016, 04:57 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: jremington on Sep 08, 2016, 05:00 pm
Quote
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.
Title: Re: Generating PWM from PID to control DC motor
Post by: Robin2 on Sep 08, 2016, 05:49 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 08, 2016, 09:59 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: naeemawan36 on Sep 08, 2016, 10:14 pm
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
Title: Re: Generating PWM from PID to control DC motor
Post by: MarkT on Sep 09, 2016, 12:09 am
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.
Title: Re: Generating PWM from PID to control DC motor
Post by: Robin2 on Sep 09, 2016, 09:35 am
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