Multiple Pin Configuration For PWM

Dear all

I want PWM signal in arduino uno328p R3 ,

In this project i have problem to define multiple pin for PWM signal. here is my code for single pin,

But I want Pwm In Pwm pin ~3,~5,~6,~9,~10,~11

please help me in this,

#include <PWM.h>

int led = 9;
int32_t frequency = 50000;

void setup()
{

InitTimersSafe();

bool success = SetPinFrequencySafe(led, frequency);

if(success) {
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
}
}

void loop()
{

pwmWrite(led, 127);

}

pwm_arduino.ino (644 Bytes)

What are PWM.h and pwmWrite()?

What’s wrong with analogWrite()

I want to give this pwm to mosfets in this and i'm beginner in arduino so i don't know about this can you please suggest me something code or any program ?

please suggest me something code or any program ?

I did; I gave you the link to analogWrite() which has an example.

Just do it for many pins:

analogWrite(3, 100); // or whatever pin, value
analogWrite(6, 52); // or whatever pin, value
analogWrite(10, 195); // or whatever pin, value

but how to set the frequency and duty cycle ?

ashishnet1986:
but how to set the frequency and duty cycle ?

The frequency's fixed for the pin, although I think you can fiddle with it deep down in the timers... I've never tried that. Some are 490, some are 980Hz, check the reference. Why do you need to set the frequency? (Maybe that "PWM" library you use allows you to change it?- I wouldn't know.)

The value you give is the duty cycle, or at least the value/255 * 100 is the % on time. So 0 is 0%, 127 is 50%, 255 is 100%.

With analogWrite, the frequencies are fixed; check the reference. The duty cycle is the second argument; it's a number from 0 to 255 where 255 equals 100%'

Here’s a 'scope trace showing the same duty cycle on a 490 and a 980 pin:

pwm hi and low freq with labels.jpg

Thanks for this information i think now i understand lat me try your example

thanks again

I’m still intrigued to know where you got these lines from:

#include <PWM.h>
... 
pwmWrite(led, 127);

I got this code from YouTube user i can’t remember which

#include <PWM.h>
... 
pwmWrite(led, 127);

Seems from this thread that the "PWM" library allows you to change the PWM frequency. I suppose there are times when that's needed? But I would be inclined to use normal analogWrite() in the first instance and only mess with the frequency if necessary.

You can use it please check below the library is attached

check and tall my that is there any way to define multiple pin .

pwm_lib-master.zip (41.1 KB)

ashishnet1986:
is there any way to define multiple pin .

How about this:

byte greenPin=3;
byte redPin=6;
byte bluePin=10;