Tutorial 5 – Fading an LED with PWM and a Potentiometer
Welcome back to our Arduino tutorial series at CraftedTech Engineering! In Tutorial 4, we learned how to read analog input using a potentiometer. Now in Tutorial 5, we'll use that input to control the brightness of an LED using PWM (Pulse Width Modulation).
This is a great way to understand how analog input and PWM output work together in real-time applications.
🔌 What You’ll Need
-
1 Arduino Board (e.g., Arduino UNO)
-
1 LED (any color)
-
1 220Ω Resistor
-
1 Potentiometer (10kΩ recommended)
-
Breadboard
-
Jumper wires
-
USB cable for Arduino
You can order the required components here:
🧠 What Is PWM?
PWM (Pulse Width Modulation) is a technique that allows us to simulate analog output using digital signals. By adjusting the duty cycle of the signal, we can control the brightness of an LED or the speed of a motor.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring Diagram
-
Connect the middle pin of the potentiometer to A0
-
Connect the side pins of the potentiometer to 5V and GND
-
Connect the LED anode (long leg) to digital pin 9 via a 220Ω resistor
-
Connect the cathode (short leg) to GND
💻 Step 2: Arduino Code
int potPin = A0; // Analog input pin
int ledPin = 9; // PWM output pin
int potValue = 0; // Variable to store potentiometer value
int ledBrightness = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin); // Read potentiometer
ledBrightness = map(potValue, 0, 1023, 0, 255); // Map to PWM range
analogWrite(ledPin, ledBrightness); // Write PWM signal
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" | Brightness: ");
Serial.println(ledBrightness);
delay(100);
}
🔍 Code Explanation:
-
analogRead(potPin)
reads the voltage level (0–1023) -
map()
scales the value to 0–255 for PWM control -
analogWrite()
sends the PWM signal to the LED -
Serial.print()
outputs data for monitoring
📷 Try This!
-
Swap the LED with a small DC motor or buzzer to see how PWM works with other components.
-
Try different analog pins or PWM pins (3, 5, 6, 9, 10, 11 on Arduino UNO).
🛠️ Troubleshooting Tips
-
Make sure your potentiometer is wired correctly: center pin to A0, side pins to GND and 5V
-
Confirm your LED is connected in the correct polarity
-
Check you're using a PWM-capable pin for
analogWrite()
🧭 What You Learned in This Tutorial
✔️ How to read analog input from a potentiometer
✔️ How to control brightness using PWM with analogWrite()
✔️ How to use map()
to scale analog readings
✔️ How to monitor values using the Serial Monitor
📌 Next Up: Tutorial 6 – Using Pushbuttons for Digital Input
In the next lesson, we’ll dive into pushbuttons, digital inputs, and how to respond to user interactions with your Arduino.
📣 Don’t forget to follow CraftedTech Engineering for more beginner-friendly Arduino tutorials and real-world tech projects!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering