Arduino Basics: Potentiometer LED Dimmer
This project demonstrates how to use a potentiometer to control the brightness of an LED using PWM, introducing analog input and output concepts in Arduino.
Hardware Required
- 1 LED
- 1 current-limiting resistor (220–330Ω)
- 1 potentiometer (10kΩ recommended)
- Jumper wires and breadboard
- Arduino board (e.g., Uno, Nano)
Wiring
- Connect the LED anode (long leg) to a PWM-capable digital pin (e.g., pin 9) through a resistor; cathode (short leg) goes to GND.
- Connect the potentiometer:
- Middle pin → analog input pin (e.g., A0)
- Other two pins → VCC (5V) and GND
Basic Code: LED Brightness Control
int ledPin = 9; // PWM-capable pin
int potPin = A0; // Analog input pin
int potValue = 0; // Store raw potentiometer value
int ledBrightness = 0; // LED brightness (0-255)
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Optional: for monitoring
}
void loop() {
potValue = analogRead(potPin); // Read potentiometer (0-1023)
ledBrightness = map(potValue, 0, 1023, 0, 255); // Map to PWM range
analogWrite(ledPin, ledBrightness); // Set LED brightness
// Optional: print values to Serial Monitor
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" → Brightness: ");
Serial.println(ledBrightness);
delay(50); // Small delay for smooth dimming
}
Extensions
- Print Raw Values to Serial Monitor: Already included in the example. Useful for calibration or debugging.
- Control LED Patterns with Knob: Use the potentiometer to change blink speed, fade timing, or switch between multiple LEDs in a sequence.
- Multiple LEDs: Control the brightness of multiple LEDs using the same or multiple potentiometers.
- Integration with Sensors: Replace the potentiometer with a sensor (like a light sensor) to automatically dim LEDs based on environmental conditions.
Tips
- Always use a resistor with LEDs to prevent overcurrent.
- Use PWM-capable pins for analogWrite.
- Map analog input to the correct range (0–255) for smooth dimming.
- Start with small delay values to achieve smooth brightness changes.
This project teaches how analog input and PWM output work together to create interactive controls, such as dimming lights, adjusting fan speed, or controlling other analog devices.
×