Go Down

Topic: PWM frequency library (Read 114081 times) previous topic - next topic

nadeemqandeel

Hi, I am having the same problem as you did. I need all 4 pins and pin 11 is not giving me an output on oscilliscop! did you figure it out, please let me know if you did. Thank you very much.

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

alamocreek

Using Arduino Mega R3.

PWM Library v05 (latest I can find)

Example runs perfectly on pin 11, checked frequency and duty cycle with scope at 20KHz PWM freq.

Change example code to pin 46 as follows (this is the only change)

int led = 46;                // the pin that the LED is attached to

Code loads, pin 13 goes high but no PWM output, I only see pin 46 slowly (every second or two) toggle between low and high.

What did I do wrong?

best

Albert

Dirk67

Any luck with Arduino Leonardo?
Thank you
For leonardo I found this article: --> http://r6500.blogspot.de/2014/12/fast-pwm-on-arduino-leonardo.html
not a ready to use library,
but really good explained how to do...

Isaac96

The Google Code page seems to have been orphaned.
I use UnoJoy and Hoodloader for HID. No Leonardo!
Do not PM me for help.Only links to topics.

CONNECT THE GROUNDS!

roeebloch

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

svacker

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

Isaac96

#97
Today at 03:15 am Last Edit: Today at 03:17 am by Isaac96
#1. Use code tags. It makes it easier to read code.
4V? Seems like your voltage is not being amplified or something.
I use UnoJoy and Hoodloader for HID. No Leonardo!
Do not PM me for help.Only links to topics.

CONNECT THE GROUNDS!

Go Up