analogWrite? Only Takes "int" not "byte" ?

Hey there =)
so i read on the website that analogWrite takes the "int" datatype, but no mention of other datatypes. Im trying to optimize my code to pass a PWM value to an ArduinoUNO analog-PIN.

I thought it would be more efficient to use the "byte"-datatype, but it seems to me like the output is actually lower when i use byte than when i use int? Is this placebo?
Im unsure now, since I read something about analogWrite not taking byte datatypes or halving the value?
What is the truth? :stuck_out_tongue:

There are many confusing things in the Arduino documentation.

Learn C and relax.

Google and read about it.

promotion in C

You could also just try it. Post any code where you think it is messing with you.

a7

2 Likes

"seems" does not actually cut it in a technical world :wink: You also might want to tell us which processor you're using.

I did check two processors, AVR and ESP32, and this is the function

void analogWrite(uint8_t pin, int val)
{
  ...
  ...
}

So the documentation seems incorrect, at least for certain boards.

He did that

Thanks; apologies to OP.

How so? The documentation says it takes an int , the code says it takes an int.

The question is what happens when you pass an int with a value greater than 255.

I did not see any clear answer, but perhaps when assigned to registers that are byte sized something like you might want to have happen does.

Experimentation woukd be faster for me if I cared.

a7

That is not possible.
I suspect that you are not measuring correctly.
Are you using an oscilloscope to measure the pulse width?

Not for the pin number :wink:

1 Like

Lower in what way ?
How are you measuring the level of the output ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.