Saturday, June 19, 2021

Project 5: Arduino Analog Output - LED Fading

Project 5: Arduino Analog Output - LED Fading 

Main Ideas:

Introduction to the Analog Output (analogWrite function) and Pulse Width Modulation (PWM) is a modulation technique used to encode a message into a pulsing signal, later we will use the PWM to control the Robot speed. In the Arduino Uno - pins 3,5,6,9,10,11 have PWM capability. Introduction to new programming concept Looping (using For statements).    

Project Description

We will use the analogWrite function to implemnt the PWM concept which will shows how to fade an LED by increasing the value in For loop.

 

Circuit

  • RED LED
  • 1* 1K Resistor
  • Arduino UNO


Code

/*

  Fading

  This example shows how to fade an LED using the analogWrite() function.

  The circuit:

  - LED attached from digital pin 9 to ground.

*/

int ledPin = 9;    // LED connected to digital pin 9

void setup() {

  // nothing happens in setup

}

void loop() {

  // fade in from min to max in increments of 5 points:

  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);

    // wait for 30 milliseconds to see the dimming effect

    delay(30);

  }

  // fade out from max to min in increments of 5 points:

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);

    // wait for 30 milliseconds to see the dimming effect

    delay(30);

  }

}

No comments:

Post a Comment