AnalogRead value constant for a varying voltage (for LED brightness variation)
Having the 11~ pin on OUTPUT mode that delivers a varying voltage (from 0 to 5 : 0 to 255).
and the A0 pin on INPUT mode to read the voltage, I find that the value read by A0 being either 0 or 1016 instead of it gradually taking multiple values between the two. what is the catch?
The circuit and the code i am using:

int volt = 0;
int sign = 1; //values: (1, -1), increments or decrements "volt" by 5.
float potential;
void setup() {
pinMode(11, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
if (volt == 0){sign = 1;}
if (volt == 255){sign = -1}
volt = volt + sign * 5; //raises brightness to 255 from 0, then to 0 from 255.
analogWrite(11, volt);
potential = analogRead(A0);
Serial.println(potential);
delay(30);
/*
*/
}
arduino-uno analogread
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Having the 11~ pin on OUTPUT mode that delivers a varying voltage (from 0 to 5 : 0 to 255).
and the A0 pin on INPUT mode to read the voltage, I find that the value read by A0 being either 0 or 1016 instead of it gradually taking multiple values between the two. what is the catch?
The circuit and the code i am using:

int volt = 0;
int sign = 1; //values: (1, -1), increments or decrements "volt" by 5.
float potential;
void setup() {
pinMode(11, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
if (volt == 0){sign = 1;}
if (volt == 255){sign = -1}
volt = volt + sign * 5; //raises brightness to 255 from 0, then to 0 from 255.
analogWrite(11, volt);
potential = analogRead(A0);
Serial.println(potential);
delay(30);
/*
*/
}
arduino-uno analogread
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago
add a comment |
Having the 11~ pin on OUTPUT mode that delivers a varying voltage (from 0 to 5 : 0 to 255).
and the A0 pin on INPUT mode to read the voltage, I find that the value read by A0 being either 0 or 1016 instead of it gradually taking multiple values between the two. what is the catch?
The circuit and the code i am using:

int volt = 0;
int sign = 1; //values: (1, -1), increments or decrements "volt" by 5.
float potential;
void setup() {
pinMode(11, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
if (volt == 0){sign = 1;}
if (volt == 255){sign = -1}
volt = volt + sign * 5; //raises brightness to 255 from 0, then to 0 from 255.
analogWrite(11, volt);
potential = analogRead(A0);
Serial.println(potential);
delay(30);
/*
*/
}
arduino-uno analogread
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Having the 11~ pin on OUTPUT mode that delivers a varying voltage (from 0 to 5 : 0 to 255).
and the A0 pin on INPUT mode to read the voltage, I find that the value read by A0 being either 0 or 1016 instead of it gradually taking multiple values between the two. what is the catch?
The circuit and the code i am using:

int volt = 0;
int sign = 1; //values: (1, -1), increments or decrements "volt" by 5.
float potential;
void setup() {
pinMode(11, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
if (volt == 0){sign = 1;}
if (volt == 255){sign = -1}
volt = volt + sign * 5; //raises brightness to 255 from 0, then to 0 from 255.
analogWrite(11, volt);
potential = analogRead(A0);
Serial.println(potential);
delay(30);
/*
*/
}
arduino-uno analogread
arduino-uno analogread
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
HuskarnovHuskarnov
262
262
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Huskarnov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago
add a comment |
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
The PWM output pin does not vary voltage - it only outputs 0, or 5V. The amount of time that 5V is on, from 1/255 of about a 2mS period, to 255/255 of the same period, determines how bright the LED is perceived to be.
You really need a current limit resistor in series with the LED to keep from burning out the uC output pin, or from burning up the LED.
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
add a comment |
As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino.) It uses pulse-width modulation to vary the "duty cycle" of the output from 100% on to 0% on, which simulates an analog voltage.
If you drive an LED with PWM (and the required current limiting resistor) your eyes will not see the flashes, and will average out the brightness based on the duty cycle of the PWM signal.
If you want an actual analog voltage from PWM you will need to add a filter capacitor to average out the voltage over time. With a simple single capacitor filter you'll still have flutter in the output but it will be closer. If you want a clean constant voltage you'll need a more complex filter.
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "540"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Huskarnov is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f61288%2fanalogread-value-constant-for-a-varying-voltage-for-led-brightness-variation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The PWM output pin does not vary voltage - it only outputs 0, or 5V. The amount of time that 5V is on, from 1/255 of about a 2mS period, to 255/255 of the same period, determines how bright the LED is perceived to be.
You really need a current limit resistor in series with the LED to keep from burning out the uC output pin, or from burning up the LED.
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
add a comment |
The PWM output pin does not vary voltage - it only outputs 0, or 5V. The amount of time that 5V is on, from 1/255 of about a 2mS period, to 255/255 of the same period, determines how bright the LED is perceived to be.
You really need a current limit resistor in series with the LED to keep from burning out the uC output pin, or from burning up the LED.
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
add a comment |
The PWM output pin does not vary voltage - it only outputs 0, or 5V. The amount of time that 5V is on, from 1/255 of about a 2mS period, to 255/255 of the same period, determines how bright the LED is perceived to be.
You really need a current limit resistor in series with the LED to keep from burning out the uC output pin, or from burning up the LED.
The PWM output pin does not vary voltage - it only outputs 0, or 5V. The amount of time that 5V is on, from 1/255 of about a 2mS period, to 255/255 of the same period, determines how bright the LED is perceived to be.
You really need a current limit resistor in series with the LED to keep from burning out the uC output pin, or from burning up the LED.
answered 3 hours ago
CrossRoadsCrossRoads
1,1977
1,1977
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
add a comment |
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
– Huskarnov
3 hours ago
1
1
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
@Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems.
– ratchet freak
3 hours ago
1
1
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
– CrossRoads
3 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
@Huskarnov this GIF might help visualize it.
– Gerben
2 hours ago
add a comment |
As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino.) It uses pulse-width modulation to vary the "duty cycle" of the output from 100% on to 0% on, which simulates an analog voltage.
If you drive an LED with PWM (and the required current limiting resistor) your eyes will not see the flashes, and will average out the brightness based on the duty cycle of the PWM signal.
If you want an actual analog voltage from PWM you will need to add a filter capacitor to average out the voltage over time. With a simple single capacitor filter you'll still have flutter in the output but it will be closer. If you want a clean constant voltage you'll need a more complex filter.
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
add a comment |
As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino.) It uses pulse-width modulation to vary the "duty cycle" of the output from 100% on to 0% on, which simulates an analog voltage.
If you drive an LED with PWM (and the required current limiting resistor) your eyes will not see the flashes, and will average out the brightness based on the duty cycle of the PWM signal.
If you want an actual analog voltage from PWM you will need to add a filter capacitor to average out the voltage over time. With a simple single capacitor filter you'll still have flutter in the output but it will be closer. If you want a clean constant voltage you'll need a more complex filter.
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
add a comment |
As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino.) It uses pulse-width modulation to vary the "duty cycle" of the output from 100% on to 0% on, which simulates an analog voltage.
If you drive an LED with PWM (and the required current limiting resistor) your eyes will not see the flashes, and will average out the brightness based on the duty cycle of the PWM signal.
If you want an actual analog voltage from PWM you will need to add a filter capacitor to average out the voltage over time. With a simple single capacitor filter you'll still have flutter in the output but it will be closer. If you want a clean constant voltage you'll need a more complex filter.
As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino.) It uses pulse-width modulation to vary the "duty cycle" of the output from 100% on to 0% on, which simulates an analog voltage.
If you drive an LED with PWM (and the required current limiting resistor) your eyes will not see the flashes, and will average out the brightness based on the duty cycle of the PWM signal.
If you want an actual analog voltage from PWM you will need to add a filter capacitor to average out the voltage over time. With a simple single capacitor filter you'll still have flutter in the output but it will be closer. If you want a clean constant voltage you'll need a more complex filter.
edited 14 mins ago
answered 3 hours ago
Duncan CDuncan C
1,5211617
1,5211617
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
add a comment |
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
@Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
– Duncan C
13 mins ago
add a comment |
Huskarnov is a new contributor. Be nice, and check out our Code of Conduct.
Huskarnov is a new contributor. Be nice, and check out our Code of Conduct.
Huskarnov is a new contributor. Be nice, and check out our Code of Conduct.
Huskarnov is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f61288%2fanalogread-value-constant-for-a-varying-voltage-for-led-brightness-variation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
– dandavis
1 hour ago