Aqib
Published

Arduino PWM Tutorial

You will lean what PWM is and how you can get the PWM output from the digital pins of Arduino using analogwrite() function.

BeginnerProtip1 hour567,285
Arduino PWM Tutorial

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram for fading an LED

How to fade an LED using Arduino

Code

Code snippet #1

Plain text
//Initializing LED Pin
int led_pin = 6;

void setup() {
  //Declaring LED pin as output
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //Fading the LED
  for(int i=0; i<255; i++){
    analogWrite(led_pin, i);
    delay(5);
  }

  for(int i=255; i>0; i--){
    analogWrite(led_pin, i);
    delay(5);
  }
}

Code snippet #2

Plain text
int led_pin = 6; 
int pot_pin = A0;
int output;
int led_value;

void setup() {
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //Reading from potentiometer
  output = analogRead(pot_pin);
  //Mapping the Values between 0 to 255 because we can give output
  //from 0 -255 using the analogwrite funtion
  led_value = map(output, 0, 1023, 0, 255);
  analogWrite(led_pin, led_value);
  delay(1);
}

Credits

Aqib

Aqib

21 projects • 276 followers

Comments