A little curiosity. A whole lot of making.Robotics, CNC & connected ideas
robodatech.
Arduino sample library

PWM Fade 27

Control LED brightness, buzzers, and motor driver speed inputs.

Open in compiler →
/*
  Roboda verified Arduino sample 0654
  Feature: PWM Fade
  Generated for online build, classroom practice, and hardware upload testing.
*/

const byte pwmPin = 6;
int level = 0;
int stepSize = 9;

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

void loop() {
  analogWrite(pwmPin, level);
  level += stepSize;
  if (level <= 0 || level >= 255) {
    stepSize = -stepSize;
  }
  delay(30);
}

How it works

Create a smooth analog-like output using PWM.

  1. Use a PWM-capable pin from the map.
  2. Connect an LED with resistor or a motor driver enable input.
  3. Increase level until it reaches 255.
  4. Reverse stepSize to fade back down.

Connections

PinConnect toNote
D6LED through resistor or motor-driver enable inputMust be a PWM-capable pin for analogWrite().

Good ideas start here.

Robotics lessons, build notes and useful updates. Subscribe by entering your email.