Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Help me to setup the PWM frequency to 10khz  (Read 1569 times)
0 Members and 1 Guest are viewing this topic.
PT
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a project of a converter for Photovoltaic modules and
I am using an Arduino Uno board to generate a PWM to controll a IGBT.
I need at least 10khz frequency pwm and using analogWrite i get 490 hz.

Can you help me configuring the timers to have a 10khz frequency??? smiley-mr-green

here is the code:

/*
 * Two Stage Single Phase Inverter control
 *
 * JN. Barbosa - MERCEUS - DEE - FCT-UNL
 */
 
 int Vpv0 = 0; // inicials conditions
 int Ipv0 = 0;
 int P0 = 0; 
 int Vpv1 = 0;
 int Ipv1 = 0;
 int P1 = 0;
 int D = 0.500; // duty-cycle
 int dD = 0.001; // perturbation
 
 void setup()
 {
   pinMode(3, OUTPUT);  // sets the pin as output
 }
 
 void loop()
 {
   Vpv0 = analogRead(A1); // read the pv voltage
   Ipv0 = analogRead(A2); // read the pv current
   P0 = Vpv0*Ipv0;                // calculate instant power
 
  delay(1);
  Vpv1 = analogRead(A1);
  Ipv1 = analogRead(A2);
  P1 = Vpv1*Ipv1;

  int dV = Vpv1-Vpv0;
  int dP = P1-P0;
 
 if (dP > 0){
   if (dV > 0){
     D = D+dD;
   }else{
     D = D-dD;
   }
 }else {
   if (dV > 0){
     D = D-dD;
   }else{
     D = D+dD;
   }
 }
 
 analogWrite(pwmPin,D*255);
 }
 
   
 Thank you a lot!!!
Logged

Portugal
Offline Offline
God Member
*****
Karma: 6
Posts: 962
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this:
http://hekilledmywire.wordpress.com/2011/08/03/introduction-to-pwm-part-7/
Logged

PT
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for the reply Senso!!
the text is long but i will read it all.

thanks smiley-mr-green
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int D = 0.500; // duty-cycle
 int dD = 0.001; // perturbation

This isn't going to work. An integer is a whole number, without a fractional or decimal part.  Maybe you meant:

Code:
float D = 0.500; // duty-cycle
 float dD = 0.001; // perturbation

But, remember that 'float' variables and calculations are much slower than 'int's. Could you scale all your calculations to use integers, say in the range of 0..1023 or 0..255?
Logged

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 223
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check this out, too:  http://www.arduino.cc/playground/Code/PwmFrequency
Tops out at 65kHz before you get deeper into the various timer registers.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int D = 0.500; // duty-cycle
 int dD = 0.001; // perturbation
Think of your calculations like this:
Code:
int D = 0; // duty-cycle
 int dD = 0; // perturbation
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PT
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello guys!!
Anachrocomputer, you are right. I shall use float numbers for dutycycle and perturbation.

AWOL, the perturbation must be greater than zero.

Thank you!! smiley-mr-green

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
AWOL, the perturbation must be greater than zero.
So don't assign a sub-unity value to an integer - it will be truncated to zero.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: