1

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:

https://i.ibb.co/dDMJJCg/Cir.png

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);
/*
*/

}
1
  • 1
    even filtered, you won't see a value between the LED's Vf and 5v, keep that in mind.
    – dandavis
    Feb 5, 2019 at 18:11

2 Answers 2

5

As CrossRoads says, there really isn't any such thing as analog output on an Arduino. (Any Arduino unless it has a built-in DAC.) 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.

3
  • 1
    @Gerben you edited my answer to talk specifically about Arduino Uno. My answer applies to all Arduinos. (Arduini?)
    – Duncan C
    Feb 5, 2019 at 19:11
  • 1
    both the Arduino Zero and Arduino Due have built-in DACs (Digital to Analog Converter). And then there are the numerous Arduino-compatibles from Sparkfun, Adafruit and other vendors that use chips with DACs. For example the ESP32 based boards, or the popular SAMD21. So definitely not all Arduini (Arduinos?).
    – Gerben
    Feb 6, 2019 at 15:24
  • I stand corrected then. I made my answer not specific to the UNO, but to all Arduinos that lack DACs
    – Duncan C
    Feb 6, 2019 at 15:59
3

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.

4
  • Thanks, if the OUTPUT voltage isn't really varying, how does the LED brightness vary? streamable.com/vc106
    – Huskarnov
    Feb 5, 2019 at 15:58
  • 1
    @Huskarnov It flickers too fast for your eye to catch, the more it is on the brighter it seems. Feb 5, 2019 at 16:00
  • 1
    The amount of on-time is perceived by the eye as dimmer or brighter. It's the way our brain works.
    – CrossRoads
    Feb 5, 2019 at 16:01
  • 1
    @Huskarnov this GIF might help visualize it.
    – Gerben
    Feb 5, 2019 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.