PWM frequency library

The Google Code page seems to have been orphaned.

You can see some PWM driving high power motors at
www.electronics-freak.com
Roee

Hi guys,

Let me first introduce my project: I'm making a pilot box (with RFID, LCD and interlock (based on a H-bridge) which basically controls the charging proces of an Electric Vehicle.

Everthing is working well except one thing: I'm using the frequency/ PWM library to generate a DC or PWM signal. With this signal the EV can see if he is ready to charge (9V PWM) or not (12V DC) or the vehicle is charging (6V PWM). Pin 10 of the arduino MEGA is translated from +5/0 to +12/-12. I'm using an opamp to get the 12V and -12V signal (see attachment). the -input is used for the divider and the + input for the PWM. It has to generate a 12V DC , a 9V PWM or a 6V PWM signal with a 1kHz frequency. But i'm having some problems with that. I'm getting a 12V DC , 9V DC and 6V DC signal so pin 10 won't generate a pwm signal (see attachment, constant 4V DC).
I'm using pwmWrite to generate different duty cycles for different current's.

My code (very straightforward):

Code:

#include <PWM.h> //We include the library for //changing the frequency of the PWM signal
//http://forum.arduino.cc/index.php?topic=117425.0


int32_t frequency=1000; //We set the frequency of the PWM at 1000 Hz




//Declare all variables in head section

float Vpilot=0;


const int sensorPin = A1;
int sensorValue = 0; //This wil measure the input voltage with a number between 0 annd 1023
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


int PWMsignal=0; //Variable for knowing if the EV supply equipment produces a PWM signal or not
int Ipp=0; //We create a variable to indicate the maximum current that can flows in the cable. If any of the resistors below is connected, then the maximum current will always be 0 A (No current)

int Pin=10; //Generates the PWM with a 1000Hz frequency
int Ivp=32; //We assign the current limit set for the equipment in A (a maximum of 32A with the switch)

int PP=A6; //Pin attached before the resistor of the proximity
int I=0; //Variable that contains the real current that will flow


int VolPP; //Variable to store the voltage before the resistor of the proximity (It is a number between 0 and 1023)
float VPP;
float v2;

//Duty Cycle
float DC; //The duty cycle for PWM
int DCint; //The duty cycle for the output pin



boolean stateA=true; 
boolean stateB=false;
boolean stateC=false;
boolean del = false;
int Mos=13; //Pin for Mosfet (Contacto)r
int prox;

//The setup routine runs once when you press reset:

//-----------------------------------------------------
void setup() {
 
 
 Serial.begin(9600); //Initialize serial communication at 9600 bits per second
   InitTimersSafe();
   bool success=SetPinFrequencySafe(10,frequency);

 

}

//The loop routine runs over and over again forever:
void loop() {
 
pinMode (PP, INPUT);
VolPP=analogRead(A6); //Variable to store the voltage before the resistor of the proximity (It is a number between 0 and 1023)
v2=VolPP*5;
VPP=v2/1023; //We transform the voltage from 0- 1023 to 0-5V and we storage it in the variable VPP
 prox=0;
//We check the voltage before the resistor of the proximity and depending on it we can know how much is the resistor and assign the maximum current

if (VPP>2.9 && VPP<3.15){ //The resistor of the proximity is around 1500Ω
prox=1500;
}
if (VPP>1.80 && VPP<2.18){ //The resistor of the proximity is around 680Ω
prox=680;
}
if (VPP>0.85 && VPP<1){ //The resistor of the proximity is around 220Ω
prox=220;
}
if (VPP>0.42 && VPP<0.51){ //The resistor of the proximity is around 100Ω
prox=100;
}
Serial.print("VPP:");
Serial.println(VPP);
//We check the voltage before the resistor of the proximity and depending on it we can know how much is the resistor and assign the maximum current

switch (prox){
 case 1500: //The resistor of the proximity is around 1500Ω
 Ipp=13;
 break;
 case 680: //The resistor of the proximity is around 680 Ω
 Ipp=20;
 break;
 case 220: //The resistor of the proximity is around 220Ω

 Ipp=32;
 break;
 case 100: //The resistor of the proximity is around 100Ω

 Ipp=63;
 break;
 
 default:
 Ipp=0;
 }
 I=min(Ipp,Ivp);
 Serial.println("Proximity:");
 Serial.print(prox);
 Serial.println(" - "),
 Serial.println(I);
 //lcd.print(I);
 //----------------------------------------------------------------------------------------------------------------------------
 
//We make 12 measurements of the voltage (1.2ms) to make sure in case we have a PWM signal which one is the highest and the lowest voltage:
//int sensorValue = A1;

for (int i=0; i<100; i++){
 
 sensorValue = analogRead(sensorPin); //Read the input on pin A1 (0-1023)
 Vpilot=((sensorValue-512)*24/1023);
 delay(150);
 //Vpilot is now between -12 and +12
 
 if (sensorValue > sensorMax) {
     sensorMax = sensorValue;
   }

   // record the minimum sensor value
   if (sensorValue < sensorMin) {
     sensorMin = sensorValue;
 }
 }
 //Note: 5V corresponds to a range of -12 to +12

 Serial.print("sensorvalue:");
 Serial.println(sensorValue);
 pinMode(Mos, OUTPUT);
 //We determine if there is or not a PWM signal:
   if (sensorValue>=930){
   stateB=false;
   stateC=false;
   stateA=true;
   digitalWrite(Mos, LOW);
   PWMsignal=0; //State A, no pilot
   }
   if (sensorValue>=650 && sensorValue<=730){
   stateB=true;
   stateC=false;
   stateA=false;
   digitalWrite(Mos, LOW);
   PWMsignal=1; //If the pilot´s voltage  isaround 9V, it means we are in state B1  and we create a PWM signal to go t   state B2
   }
   else if(sensorValue<480 && I > 0)
   {
   stateC=true;
   stateB=false;
   stateA=false;
   digitalWrite(Mos, HIGH);
   PWMsignal=1; //If the pilot´s voltage is around 6V, it means we are in state C1 and we create a PWM signal to go to  state C2
   }

//---------------------------------------------------------------------------

   Serial.print("PWMsignal:");
   Serial.println(PWMsignal);
   pinMode(Pin, OUTPUT);
   if (PWMsignal == 0) { //When there is current onthe Pin 9 and there is not a PWM signal,then the pilot´s voltage is 12 V
     
       digitalWrite(10, LOW);
   }
   //
  // float DC = 0.0;
  if (PWMsignal == 1) { //When there is a PWM signal on the pin 9, then the pilot´s voltage is a PWM signal between 12V and - 12V
     // We create the PWM signal in the PIN 9 between 12V and -12V

     if((I>=6) && (I<=51))
     {
     DC = (I / 0.6) / 100;
     }
     //85% < duty cycle <= 96%
     else if (I > 51 && I <= 80) {

       DC = ((I / 2.5) + 64) / 100;
       
     }
     
     else if (I == 80)
     {
       DC = 96;
       //10% <= duty cycle <= 85%
     }
   //  /100  >> percentage
   //float DC = 0.0;
   DCint = DC * 255;
   
   //As Analog output pin is 8bit we can get maximum 2^8=256 or a range of values between 0 to 255.
   
   //Sending the value 255, to the LED input produces 100% duty-cycle, which results in full power on a PWM pin. 
   //Sending the minimum value 0, to the LED input produces 0% duty-cycle, which results in no power on a PWM pin.

   //Next line disabled for testing


   pwmWrite(Pin, DCint); //We create the PWM signal in the Pin 9 with a duty cycle=DCint
   Serial.print("Dutycycle:");
   Serial.print(DCint);
   Serial.print(" - ");
   Serial.println(DC);

  }
}
//------------------------------------

Thanks for the help,

kind regards,

Sepp

#1. Use code tags. It makes it easier to read code.
4V? Seems like your voltage is not being amplified or something.

I did some tests with the scope today:

Apparently it's just PWM pin 10 that won't generate a PWM signal

I guess I will have to use PWM pin 9 (I'm using already the other pin's) but this one's on the same timer (timer 2 of the Mega) of pin 10 so I'm not sure if this will infuence pin 9. using pin 11 gives another weird result (picture 2).

Sepp

Hello sir,
1st of all thanx for the library and effort. but how can i use all of the pwm pins on mega 2560 beacuse i am making a 3 phase inverter and i require all of them operational and currently i am only getting only 11 and 12 operational. looking for a reply from you.

Hi,

with Arduino IDE 1.05 this Library works really nice, but not with IDE 1.6.5.
Any Idea why ?

1 Like

Hi All,

I'm new to the Arduino family and my first project is PWM related. My research has led me to this library but i am unable to install it into IDE1.6 as I get an error that it is not a valid library file. When comparing it to the ones already installed I noticed that the folder didn't include libraray.properties or readme.adoc files. Could this be the problem?

Thanks

I managed to get the PWM library working with IDE 1.6 by doing the following:

  1. Download and unzip Arduino PWM Frequency Library v-05, here you will find three folders, "PWM", "PWM_lib_example" & "PWM_lib_resolution_example". Move the the two example folders into the PWM folder.

  2. The go to C:\Program Files (x86)\Arduino\libraries and copy the "PWM" folder here.

  3. When complete your libraries folder should look like the attached picture.

  4. Restart the ide and PWM should now be available in your list of libraries.

JcanReefMan:
I managed to get the PWM library working with IDE 1.6 by doing the following:

  1. Download and unzip Arduino PWM Frequency Library v-05, here you will find three folders, "PWM", "PWM_lib_example" & "PWM_lib_resolution_example". Move the the two example folders into the PWM folder.

  2. The go to C:\Program Files (x86)\Arduino\libraries and copy the "PWM" folder here.

  3. When complete your libraries folder should look like the attached picture.

  4. Restart the ide and PWM should now be available in your list of libraries.

Thanks! This was a big help.

You're welcome

hello,
I am trying to use this library to set frequency for my pins, but this is also presetting some duty cycle as well. I want to change duty cycle through labview interface, It works well normally, but when I set the pin frequency through this library I don't have control over duty cycle. Any help?

There's no github with this library exported? Google Code is closed! Thanks

Not getting this to work with the NANO. The LED example only turns it on and off, it's acting like a binary 0 and 1, not fading just on and off blinking (255=on,0=off), not pwm'ing.

I was doing something wrong. IT works flawlessly. Just got to be sure of the pins you are using, that they are capable of the frequency you are requesting (8bit limitation on some pins)

RichMo:
I made a 1Hz to 2MHz Adjustable frequency PWM library for Arduino. Maybe this will help you.
http://www.rmcybernetics.com/projects/code/index.htm

Hi, I have seen your PWM library.
What I don't fully onderstand is how to use the pulsewidth.
If I have a frequency of 75Hz, what should the pulsewidth be for 50% dutycycle?
75Hz is 13,33 msec, so 50% is 6,67 msec, so pulsewidth should be about 6666667 nsec?

If I have a frequency of 75Hz, what should the pulsewidth be for 50% dutycycle?

To create a signal with 50/50 duty cycle, timer in CTC mode is more effective than timer in PWM mode.

but yes, calculation are correct. We can assume the width is of the HIGH signal, so a 25/75 duty is ~3200000ns

Good work runnerup! This library seems very useful!

I am having difficulties to make it compile in my Arduino 1.6.6 environment. I read the entire thread and searched the Arduino site on library implementation. I have used other libraries before and even written a few of my own, but i cant get this one properly installed. I have the folders and files arranged like this:

../libraries/PWM/examples/PWM_lib_example/PWM_lib_example.ino
../libraries/PWM/examples/PWM_lib_resolution_example/PWM_lib_resolution_example.ino

../libraries/PWM/utility/ATimerDefs.cpp
../libraries/PWM/utility/ATimerDefs.h
../libraries/PWM/utility/BTimerDefs.cpp
../libraries/PWM/utility/BTimerDefs.y

../libraries/PWM/keywords.txt
../libraries/PWM/PWM.h

What am I doing wrong? I'm sure it is right there in front of me and that I should see it, but I don't. Any help is much appreciated.

Never mind this post. My Arduino installation was corrupt. It compiles after I reinstalled the inviroment. Thank you for this awesome library!

Kind regards,
Simen

Good morning,

Is it possible, that there is an issue within this library?

I have tried to set the Frequency at PIN 2 form my Mega 2560 at 1kHz.

I have tried to manage this on several ways, by setting the "pinfrequency" and later the "timerfrequency".

I need to set the frequency to 1kHz at 11 Pins. Pin 13 to 11 are working well with this library.
But i have no idea, how to manage the Frequency at the other Pins.

#include <PWM.h>


void setup() {
  // put your setup code here, to run once:
InitTimers();
SetPinFrequency(2,500);
pinMode(2,OUTPUT);
}

void loop() {

//pwmWriteHR(testpin,20000);
pwmWrite(2,255);
  // put your main code here, to run repeatedly:

}

Can someone help me?

Here is the code:

Thank you very much!

Mikey3110:
Is it possible, that there is an issue within this library?

I have tried to set the Frequency at PIN 2 form my Mega 2560 at 1kHz.

I have tried to manage this on several ways, by setting the "pinfrequency" and later the "timerfrequency".

But what is going wrong? Have you tried to write 127 instead of 255 for testing?

How did you implement the library? I am having trouble getting it to compile.