int led=9; // PWM pin led is attached to is 9
int brightness=0; // how bright the light is
int fadeAmount=5; // how many oints to fade led by
void setup() {
// put your setup code here, to run once:
// here pin 9 given above=LED is set to be an output
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// digitalWrite(ledPin, HIGH); used then can't change brightness so written analog
analogWrite(led,brightness); // set brightness to pin 9
brightness=brightness+fadeAmount; //change brightness for next time in loop
if(brightness<=0 || brightness>=255){
fadeAmount= -fadeAmount;
}
delay(10); //wait 10ms to see dimming effect
}
// Anode (+, long leg) → Pin 9 of Arduino (through a 330Ω resistor)
// Cathode (-, short leg) → GND on Arduino