Arduino: Controlling the Onboard LED: Part 3

Introduction: Arduino: Controlling the Onboard LED: Part 3

About: My hobbies are electronics and photography. I have an Arduino which I like to program, and I make stop motion videos with my camera. I also have a great interest in DIY.

Now for part three: fading. The voltage in the Arduino is a regulated 5 or 3.3 volts. You can't change it, except with resistors. And the LED_BUILTIN doesn't have any way for a resistor to be attached. But we can use our eyes to make it look like the LED is fading. If the LED blinks very fast, we can't see the blinking, so the LED looks plain on, albeit a little bit dimmer. So let's take this to our advantage.

Step 1: Bill of Materials

Step 2: LED

If you have seen my last two instructables, you would know that the onboard LED would be blinking if brand new. Open the IDE and set everything up.

Type, copy, or download.

void setup() {
pinMode(LED_BUILTIN, OUTPUT); } void loop() { for(int i = 0; i <= 255; i++){ analogWrite(LED_BUILTIN, i); delay(10); } for(int i = 255; i >= 0; i--){ analogWrite(LED_BUILTIN, i); delay(10); } }

This code fades the LED. analogWrite means turn something on using PWM. PWM is Pulse Width Modulation and is used to control the apparent brightness of the LED by blinking it on and off very fast. You can type any number between 0 and 255 to change the brightness between 0 and full brightness. It makes the LED blink on and off very fast. typing 51 means 20% and the LED is on 20% of the time, but it is blinking so fast that we can't see the blinking.

Upload the program. The LED should start fading between bright and dim.

int l = LED_BUILTIN;
void setup() { pinMode(l, OUTPUT); } void loop() { analogWrite(l, 255); }

Type, copy or download.

This code means the LED is at full brightness. Change the 255 to 5. Upload, and the LED will become dimmer.

Try out many other numbers and see what happens.

Congratulations! You now know how to fade the LED.



    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Metalworking Contest

      Metalworking Contest