Go Down

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

Burak93

#120
Apr 19, 2016, 06:17 pm Last Edit: Jul 04, 2016, 06:33 pm by Burak93
Does anyone know if this library broke on arduino 1.6?

I've used this library for outputting 4 PWM signals for my custom aquarium LEDs, it worked fine for over a year.
(running on an arduino mega2560 with a WizNet 5500 ethernet board on top)
Now I wanted to make some changes, recompiled the code using the latest arduino libraries on Arch linux and it simply doesn't work anymore. No errors, but only pin 11 seems to work (but not properly), pins 5 / 6 and 44 simply give no signal as far as I can tell.
Using analogWrite instead of pwmWriteHR turns on the LEDs without problems.

Does anyone have any clue as to what the cause might be?

Wouter
Hello Wouter,
did you solce your problem? i have also the same problem with the library, it only works for pin 11 and 12 but i need one more pwm output. I wrote a simple code to test it and measured the output with oscilloscope.


Code:

Code: [Select]
#include <PWM.h>

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

int32_t frequency = 100; //frequency (in Hz)

void setup()
{
 InitTimersSafe();

 //sets the frequency for the specified pin
 bool success = SetPinFrequencySafe(led, frequency);

 if (success) {
   pinMode(13, OUTPUT);
   digitalWrite(13, HIGH);
 }
 pwmWrite(led, 0);
 delay(2);
 SetPinFrequencySafe(led, frequency);
 pwmWrite(led, 127);

}

void loop()
{

}

raschemmel

#121
Apr 22, 2016, 03:02 am Last Edit: Apr 22, 2016, 04:39 am by raschemmel
Does anyone know which Reply# in this thread would have a working code example of how to get  a PWM frequency of 10 khz ? All I need is a simple pot => analog input A0 that converts that to duty cycle at 10 khz.
Thanks
I installed the library but not sure how to use it and this thread is really long.

Nevermind. I found one here

Code: [Select]
  #include <PWM.h>
int32_t frequency = 10000; // frequency (in Hz)
 int sensorValue =0;

void setup()
{
  // put your setup code here, to run once:
  InitTimersSafe();
  bool success = SetPinFrequencySafe(9, frequency);
  if(success) {
    pinMode(13,OUTPUT);
    digitalWrite(13,HIGH);
   }
 }

void loop()
   {
    // put your main code here, to run repeatedly:
    int sensorValue = analogRead(A0);
    pwmWrite(9,sensorValue/4);
    delay(30);
    }
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

adityan1991

How can I generate 1 MHz signal with this function??

I'm a novice .. Pls Help.

raschemmel

I don't know. Did you try it (try changing the frequency to 1000000?)
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

68tjs

#124
Apr 30, 2016, 10:23 am Last Edit: Apr 30, 2016, 10:25 am by 68tjs
Do you thinks it is possible to generate a 1MHz PWM signal with a 16 MHz system frequency ?
What is the result of 16 MHz divide by 256 ?

You should have a look on 328p (or 2560) datasheet !
You will found PWM frequency formula.

raschemmel

#125
May 05, 2016, 07:15 am Last Edit: May 05, 2016, 03:19 pm by raschemmel
Quote
Hello everyone. I am very new to Arduino, so please go easy on me if this is stupid question. I have loaded the sketch and it is working flawlessly for me. The only thing i would like to change is being able to very the frequency using a pot. I don't have to generate a specific frequency, I just need a linear signal from 10Hz to 200Hz, that i can also vary to Duty Cycle. I have a couple other questions, but I'll tackle them one at a time. Thank you in advance for a great resource and any help you can offer.
I could tell you if you start your post. I can't tell you here because your question is not relevant to THIS post. This post is the PWM Library and the frequency is set ONE TIME in SETUP, so you can't change it with a pot.

Start your own post. You can't post on Reply#125 of someone else's post to ask a totally different question.  That is called POST HIJACKING . (which is a violation of forum policy)

DELETE YOUR POST BY CLICKING ON THE '"More" link in the lower right and select REMOVE.

If you don't , the Global Moderator will.

Then choose a forum Topic section and start a topic to ask your question.
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

meninas92

Hi i have tried to contact the supporter of the library "runnerup". i have some minor fixes to the library i have created a zip compatible with newer versions of arduino. does anyone knows how to contact runnerup?

raschemmel

Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

junktronix

I'm curious what you fixed.  I've noticed that occasionally the PWM doesn't start immediately.  The PWM output will be high or low for 5-10mS before the pulses start.

Hi i have tried to contact the supporter of the library "runnerup". i have some minor fixes to the library i have created a zip compatible with newer versions of arduino. does anyone knows how to contact runnerup?

vikramrobo

Hello Sir,


I a trying to use below code from PID loop to set the frequency of two different PWM pins (12 from Timer 1 & 44 from timer 5) , but I have seen only Timer1 will work & no PWM observe on Pin 44. can you help me in this

void motor1_set_speed(unsigned int frq)
{


   Timer1_SetFrequency(frq);
   pwmWriteHR(11, 32768); // Keeping duty cycle 50 % always fix , based on motor specf

}

void motor2_set_speed(unsigned int frq)
{

  Timer4_SetFrequency(frq);
  pwmWriteHR(44, 32768); // Keeping duty cycle 50 % always fix , based on motor specf
}

vikramrobo

Hello Sir,

I am trying to use Time1 pin 12 & Timer 5 pin 44 for PWM purpose , and using PID loop I  want to update
Timer 1 & timer 5 frequency every 100 ms, I have observed only one Timer will run at a time, I mean I observed
 In this case either Time1 work or Timer5 , how can get different PWM frequency from Timer 1 & Timer 4 at a same time, please help

void motor1_set_speed(unsigned int frq)
{

   Timer1_SetFrequency(frq);
   pwmWriteHR(12, 32768); // Keeping duty cycle 50 % always fix , based on motor specf

}

void motor2_set_speed(unsigned int frq)
{

  Timer5_SetFrequency(frq);
  pwmWriteHR(44, 32768); // Keeping duty cycle 50 % always fix , based on motor specf
}

husain081

Hi friends,

Here is my sketch for the motor control using specified PWM frequency,

#include <PWM.h>
const int32_t frequency = 25000 ; //frequency (in Hz)
const int pwm = 9 ;  //initializing pin 9 as pwm
const int in_1 = 2 ;
const int in_2 = 3 ;

void setup()
{
  InitTimersSafe();

  bool success = SetPinFrequencySafe(pwm, frequency);

  if(success){
   pinMode (pwm, OUTPUT);
   pinMode (in_1, OUTPUT);
   pinMode (in_2, OUTPUT);
  }
}

void loop()
{
  digitalWrite(in_1,HIGH) ;
  digitalWrite(in_2,LOW) ;
  pwmWrite(pwm,127) ;
  delay(3000) ;

  digitalWrite(in_1,HIGH) ;
  digitalWrite(in_2,HIGH) ;
  delay(200) ;
}


Now, I would like to read the PWM frequency and the duty cycle through serial port.  I'm also thinking, to set the PWM frequency and duty cycle through serial port and reading them back. I would appreciate if somebody could help me on this as I'm very new to this Arduino world.

Thanks in advance,

Cheers,
Husain


raschemmel

#132
Jun 27, 2016, 06:50 am Last Edit: Jun 27, 2016, 07:43 am by raschemmel
To the last two posters:

Thíe forum has rules regarding the posting of code .Please follow these rules and "Modify" your posts by highlighting the code and clickinv the CODE TAGS [</>] button ( first on the left)

@Hussain081,
Not sure if I understand you correctly.
You can send any information you want on the serial port and read it back. What difference does it make if it happens to be a frequency value or a duty cycle ? It' the same as sending
any other values.
The place to start is the Serial.Write and Serial.Read() Playground pages.
Both are numbers and everything is ASCII so you are in fact asking how to send ASCII code for numbers. Look up the values on the ASCII code table . You need to send a termination character like a CR as well.
Google : "arduino sending and receiving multi-digit integers - Bald Engineer"
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

husain081

Oops, sorry I wan't aware of the forum rules. I will follow it in coming posts...

Thanks for your info mate, I will check...

raschemmel

Modify your last post.( the code)
Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Go Up