Why divide the potentiometer value by 4?

Asked

Viewed 72 times

-1

int ledPin = 13; // pino do led

int analogPin = 3; //pin for potentiometer reading

int val = 0; //variable to store the read value

void setup()

{

pinMode(ledPin, OUTPUT); // sets pin as output

}

void loop()

{

val = analogRead(analogPin); // le the analog value

analogWrite(ledPin, val / 4); // triggers led with the analog value read divided by 4

}

  • What version of Arduino are you using? Uno, Due, Mega, Mini?

  • I’m using the UNO

2 answers

2


Boas, The output resolution of Arduin is 8 bits (2 8=256), the input resolution is 10 bits (2 10=1024). If the potentiometer is at the maximum value, that is, in 5V the value that the Arduino reads is 1023 (because it starts at zero) and as the output resolution is of less than 2 bits the maximum output value has to be 255 (1023/4=255) which is also equivalent to 5V.

  • Now I understand the reason to use the value 4. See if I am correct. As the output has the maximum value 255. I use the value 4 to force my output to work within the recommended limit value, IE, picking 1023/4 will have an integer of 255 value as you said yourself is the maximum that this output can have. Thank you so much for your help.

  • exactly, that’s just it

1

This division is only to limit the value passed, see below:

val = analogRead(analogPin);  // lê o pino de entrada analógica
analogWrite(ledPin, val / 4);

analogRead returns values from 0 to 1023

analogWrite receives from 0 to 255 so the division by 4

For more information on the link to the documentation of analogWrite()

  • Thank you very much for your help, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.