Arduino Nano 33 IoT - LED - Fade

This guide shows you how to use:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×LED Kit with resistor
1×LED (red)
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of LED

LED Pinout

An LED has two metal legs:

  • Cathode (-) pin: Plug this pin into the ground (0V).
  • Anode (+) pin: Use this pin to turn the LED on or off.
LED Pinout

How LED Works

After connecting the negative (-) side to the ground:

  • Connect GND (ground) to the positive (anode) side, and the LED will be off.
  • Connect VCC (power) to the positive side, and the LED will be on.
  • Apply a PWM (Pulse Width Modulation) signal to the positive side, and the LED's brightness will change depending on the PWM duty cycle. The duty cycle can be from 0 to 255—the higher the number, the brighter the LED.
    • A PWM value of 0 acts like connecting to GND, so the LED stays off.
    • A PWM value of 255 acts like connecting to VCC, so the LED shines at full brightness.
    How LED Works

    ※ NOTE THAT:

    Most of the time, you need a resistor to keep the LED from burning out. The resistor’s value depends on the LED's specifications.

    Arduino Nano 33 IoT - fade LED

    The digital output pins on the Arduino Nano 33 IoT can be set to create a PWM signal. By connecting the positive side of an LED to a pin on the Arduino Nano 33 IoT and the negative side to the ground, and programming the Arduino to change the PWM duty cycle, you can make the LED slowly turn on and off.

Wiring Diagram between LED and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Set up a pin on the Arduino Nano 33 IoT as an output using the pinMode() function. For example, use pin D5 as an output.
pinMode(5, OUTPUT);
  • Adjust the LED brightness by creating a PWM signal with the right on/off ratio using the analogWrite() function.
analogWrite(5, brightness);

Brightness is a number between 0 and 255.

Arduino Nano 33 IoT Code - Simple Fade Example

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code below and paste it in the Arduino program.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED int brightness = 0; // how bright the LED is int fade_amount = 5; // how many points to fade the LED by // The setup function runs once on reset or power-up void setup() { // configure the Arduino Nano 33 IoT pin as an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // set the brightness analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_amount; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_amount = -fade_amount; } // wait for 30 milliseconds to see the dimming effect delay(30); }
  • Compile your code and send it to your Arduino Nano 33 IoT board by clicking the Upload button in the Arduino software.
Arduino IDE Upload Code
  • Look at how bright the LED is.

Line-by-line Code Explanation

The Arduino Nano 33 IoT code above has an explanation for each line. Please check the notes in the code!

※ NOTE THAT:

The example above uses the delay() function to slowly turn the LED on and off. However, delay() does not make the fading smooth and stops other parts of your program from running at the same time. In the next sections, we will learn how to create a smooth fade with the LED without stopping other code, by using the millis() function.

How to fade-in LED in a period without using delay()

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // configure the Arduino Nano 33 IoT pin as an output start_time = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - start_time; if (progress <= FADE_PEDIOD) { long brightness = map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { start_time = millis(); // restart fade again } }

How to fade-out LED in a period without using delay()

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-led-fade */ #define LED_PIN 5 // The Arduino Nano 33 IoT pin D5 connected to LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long start_time; // The setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // configure the Arduino Nano 33 IoT pin as an output start_time = millis(); } // fade-out in loop, and restart after finishing void loop() { unsigned long progress = millis() - start_time; if (progress <= FADE_PEDIOD) { long brightness = 255 - map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { start_time = millis(); // restart fade again } }

Video Tutorial

Language References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!