Go Down

Topic: The Timer or something in FADE doesn't work (Read 1 time) previous topic - next topic


As the topic.

I used the code below. All I need is a signal that goes from 0 to 5v steadily( the first half of a triangle signal)

But the signal only growth from 40mV to 116mV then straightly jumped to 1.04V

and then from 1.14v straight to 3.2v then it increases towards 5v.

i don't understand how could things go wrong with just a single function.
please help.

Code: [Select]
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

void setup()  {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);


void loop()  {
  // set the brightness of pin 9:
  analogWrite(9, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    brightness = 0;
  // wait for 30 milliseconds to see the dimming effect   


Your confusion is with the arduino analogWrite function. It's doesn't output a pure DC voltage but rather a digital PWM signal that is always either +5vdc or ground at any given instant in time. It's the duty cycle (ratio of on to off) that can be manipulated with the analogWrite function in 8 bits of resolution. If you require a true analog 0-5vdc output then you must add an external low pass filter to convert the PWM signal to a true analog output voltage. You also may need to add a op-amp buffer stage if you need to provide any significant current to the device using the signal.



Thanks a lot for the quick reply.
i'll go check it out. :D


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131