Tutorial 8 – Using Multiple LEDs with PWM

Welcome back to the CraftedTech Engineering Arduino tutorial series! In the last tutorial, we learned how to debounce a pushbutton to get more reliable inputs. Now, we’re going to dive into a new project where we control multiple LEDs with Pulse Width Modulation (PWM). This will allow us to adjust the brightness of the LEDs, creating smooth transitions for lighting effects.


🔌 What You’ll Need

  • 1 Arduino Board (e.g., Arduino UNO)
  • 3 LEDs (different colors if you prefer)
  • 3 220Ω Resistors
  • Breadboard
  • Jumper wires
  • USB cable for Arduino

You can order the required components here:


🧠 What Is PWM?

Pulse Width Modulation (PWM) is a technique used to control the amount of power delivered to a load without wasting energy as heat. It works by rapidly switching the power on and off. The duty cycle — the percentage of time the signal is high — determines how bright an LED appears or how fast a motor turns.

In this tutorial, we will use PWM to adjust the brightness of multiple LEDs.


🧾 Step-by-Step Instructions

🔧 Step 1: Circuit Wiring

  • Connect each LED’s anode (long leg) to pins 9, 10, and 11 (PWM-capable pins on the Arduino).
  • Connect the cathode (short leg) of each LED to GND via a 220Ω resistor.
  • Make sure the pins are correctly wired to the LEDs.

💻 Step 2: Arduino Code for Multiple LEDs with PWM

int led1 = 9; // LED connected to pin 9 int led2 = 10; // LED connected to pin 10 int led3 = 11; // LED connected to pin 11 int brightness = 0; // Initial brightness level int fadeAmount = 5; // Amount to change the brightness each time void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); } void loop() { // Fade LEDs in and out analogWrite(led1, brightness); analogWrite(led2, 255 - brightness); // Opposite brightness for effect analogWrite(led3, brightness / 2); // Different brightness level brightness = brightness + fadeAmount; // Reverse direction if the LED is at max or min brightness if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; } delay(30); // Delay to create smooth fading effect }

🔍 Code Breakdown

  • analogWrite(pin, value): This function is used to output a PWM signal to a pin. The value can range from 0 (off) to 255 (full brightness).
  • brightness: A variable that changes over time to create the fading effect.
  • fadeAmount: This controls how quickly the LED fades in and out. Positive values make the LED get brighter, and negative values make it fade.
  • delay(30): This delay ensures the fading happens smoothly.

🧪 Try This!

  • Experiment with different fadeAmount values to make the fade effect faster or slower.
  • Try connecting more LEDs and control their brightness with different PWM signals!

🛠️ Troubleshooting Tips

  • Ensure you’re using PWM-capable pins (9, 10, 11) for controlling brightness.
  • If the LED is flickering erratically, adjust the delay time to control the smoothness of the transition.
  • Use the Serial Monitor to debug and track brightness values in real time.

🧭 What You Learned in This Tutorial

✔️ How to use PWM to control the brightness of multiple LEDs
✔️ How to create smooth lighting effects with fading
✔️ How to work with analogWrite() for controlling output pins
✔️ The concept of duty cycle and its role in PWM


📌 Next Up: Tutorial 9 – Using Sensors with Arduino

In our next tutorial, we’ll explore how to interface sensors with your Arduino and create interactive projects that react to the environment.


📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!

🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering

Back to blog

Leave a comment