// Define pin of microcontroller leading to the gate of the MOSFET int mosfet_pin = D1; void setup() { // Declare pin as output pinMode(mosfet_pin, OUTPUT); } void loop() { // Increase the duty cycle and by this brightness for (int val = 0; val <= 100; val++) { // PWM: Set the duty cycle to val/255 analogWrite(mosfet_pin, val); delay(10); // Wait } // Decrease the duty cycle and by this brightness for (int val = 100; val >= 0; val--) { // PWM: Set the duty cycle to val/255 analogWrite(mosfet_pin, val); delay(10); // Wait } }