Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.

Arduino- Fading example file confusion
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino- Fading example file confusion

by Nora on Sun Apr 19, 2009 8:55 am

In the example file "Fading" that comes with Arduino it fades an LED using values from 0-255. This example is in the "Analog" folder.
I understand the code, but am confused as to how this works with a digital pin. I tried it using other digital pins with working results.
Isn't the digital pin just on/off?
What am I missing? :oops:
Thank you,
edit: I just went to the Arduino language site and the answer is clear:

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz.
Posts: 14
Joined: Tue Dec 26, 2006 11:28 am
Location: New York

Re: Arduino- Fading example file confusion

by mtbf0 on Sun Apr 19, 2009 10:05 am

they call it analogWrite, but the arduino cannot actually do analog output. what it can and does do is pulse width modulation. this involves turning a digital pin on and off very quickly and varying the proportion of on time to off time from 0/255, always off, to 255/255, always on. with an led this causes a perceived change in brightness. with a speaker and a low pass filter it can be used to make something that sounds a lot more pleasant than the square wave you get just turning the pin on and off at audio frequencies.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.