 # Need clarification on the parameters of analogWrite(pin,value)

This is a basic question .

Need clarification on the parameters of analogWrite(pin,value). The reference state that the value here is the Duty Cycle.

But traditionally, Duty cycle is a term in percent . But the reference states that the value is from 0 to 255.

This is a contradiction since duty cycle has no meaning after reaching 100%. Therefore, what is exactly that this value means?

Imagine a dial that you turn to control the duty cycle you talked about and the dial is just a potentiometer with resistance mapped to AnalogWrite range (0-255). Now imagine the analog range mapped to percentage duty cycle so "0" is min and "255" is max. The analogWrite argument is just a value within a range that is later converted to a duty cycle percentage by the uC.

Dividing it into 255 steps gives more precision than 100 steps. These are byte values so there are no decimals.

…R

Robin2: Dividing it into 255 steps gives more precision than 100 steps. These are byte values so there are no decimals.

This means that the PWM counters count to 255 steps, not 256!

raschemmel: 2.55% per count

You think?

Ok.. The 255 steps from the fact that 0 counts as one of the allowed values which total to 256?

And.. is this correct that each step is 2.55% , or do you mean 100%/255 steps =0.392%/step

What about the frequency? It is lock at 980 Hertz.. And there are a lot of instruction going on in the microcontroller. Therefore, isn't a 1 step just getting too close to the device clocking limits?

Sorry about the line of questionings. But I am building an optical device engine 100% with Arduino. It need a lot of precision and will like to understand that the realistic limitations are between the memory latency , speed and instruction.

Thank you all guys for putting up with me.

And.. is this correct that each step is 2.55% , or do you mean 100%/255 steps =0.392%/step

Your right. I got that backwards . I'll delete that post. My bad. (Thanks Paul)

jecalderon: Ok.. The 255 steps from the fact that 0 counts as one of the allowed values which total to 256?

And.. is this correct that each step is 2.55% , or do you mean 100%/255 steps =0.392%/step 0.392%/step What about the frequency? It is lock at 980 Hertz.. And there are a lot of instruction going on in the microcontroller. Therefore, isn't a 1 step just getting too close to the device clocking limits? The analogWrite function is utilizing one of the three internal hardware timers in the AVR 328P chip, so once a duty cycle value is sent to the function the actual PWM is all done continuously and independently of the program, so no real effect on the rest of program performance. Sorry about the line of questionings. But I am building an optical device engine 100% with Arduino. It need a lot of precision and will like to understand that the realistic limitations are between the memory latency , speed and instruction.

Thank you all guys for putting up with me.

It sounds like you could benefit from reading this: http://playground.arduino.cc/Code/Timer1

jecalderon: Ok.. The 255 steps from the fact that 0 counts as one of the allowed values which total to 256?

The values total to 256, so as zero is included, you only have 255 steps "on". So the counter must be counting 255 steps, not 256. Each one of those 255 steps might be on or off, a zero PWM value means all are off, and a 255 PWM value means all are on. But there cannot be a 256th step in the count.

raschemmel: It sounds like you could benefit from reading this .

Tells you about which timers do what, but not how they count. That would be in the 328 datasheet.

Tells you about which timers do what, but not how they count. That would be in the 328 datasheet.

No, it doesn't address the OP's question but if he is going to use PWM wouldn't help to know how ?

I was under the impression that PWM ran at ~488-490 Hz, where'd you get 980 from?

The 255/256 count is a little funny; 0 is no pulses, 255 is no pulses - so you might say that only 1 to 254 are the possible steps.

The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.

Thanks. Don't think I'd read that one before.