[Arduino] Led Fade In / Fade Out

 Hi,


so, you have nothing to do on this afternoon? Good, you can waste your time doing this: a simple fade in/ fade out effects with your christmas red led...


What you need:

  • Arduino Uno
  • Led
  • Some cables
  • Resistor 220 ohm

The fade in/fade out effect is realized through the Pulsar-Width Modulation (PWM) that according wikipedia is : 
 "a method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts"

 The connection is this:

  • GND (Arduino) -> Resistor -> LED (short leg)
  • PIN 9 (Arduino) -> LED (the other leg) 

You can follow this schema


And this is the code you cane use



int LED = 9
int msDelay = 5; //ms

void setup() { 
   pinMode(LED, OUTPUT); 

void loop() {

   // PWM from 0 to 255 (fade in)
   for(int pwmValue = 0pwmValue <= 255pwmValue++){ 
      analogWrite(LEDpwmValue);
      delay(msDelay);
   }
   
   // PWM from 255 to 0 (fade out) 
   for(int pwmValue = 255pwmValue >= 0pwmValue--){
      analogWrite(LEDpwmValue);  
      delay(msDelay); 
   }



Now your afternoon has more sense....

Commenti