Project Overview
We'll create a system where a potentiometer controls the brightness of an LED strip. I'll show you two versions - one for addressable LED strips (like WS2812B/NeoPixel) and one for regular LED strips.
Components Needed
- Arduino Uno (or compatible)
- LED Strip (WS2812B addressable OR regular 12V LED strip)
- Potentiometer (10kΩ)
- Resistors (220Ω for data line if using WS2812B)
- Breadboard and jumper wires
- Power supply (5V for WS2812B or 12V for regular strips)
- MOSFET (if using regular LED strip)
Wiring Diagram
For WS2812B (Addressable) LED Strip:
- Potentiometer: Middle pin → A0, outer pins → 5V and GND
- LED Strip: Data → Digital Pin 6 (through 220Ω resistor), VCC → 5V, GND → GND
For Regular LED Strip:
- Potentiometer: Same as above
- LED Strip: Through MOSFET controlled by PWM pin 9
LED Strip Controller with Potentiometer
Code
// LED Strip Controller with Potentiometer // Compatible with both WS2812B (NeoPixel) and regular LED strips #include <FastLED.h> // Configuration - Choose your LED strip type #define USE_NEOPIXEL true // Set to false for regular LED strips // Pi
Great! I've created a comprehensive Arduino code for controlling LED strips with a potentiometer. The code includes:
- Support for both NeoPixel (WS2812B) and regular LED strips
- Smooth potentiometer reading with averaging
- Brightness control
- Bonus effects like color cycling, rainbow, and breathing
- Detailed comments and usage notes
Now I should provide additional explanation about the project, wiring details, and perhaps some tips for getting started.
How It Works
Basic Operation:
- The potentiometer creates a variable voltage (0-5V) as you turn it
- Arduino reads this voltage on analog pin A0 (values 0-1023)
- Code maps this to LED brightness (0-255)
- LEDs update smoothly without flickering
Key Features:
- Smoothing: Averages multiple readings to prevent jittery behavior
- Efficiency: Only updates when brightness changes significantly
- Flexibility: Works with both addressable and regular LED strips
- Bonus Effects: Color cycling, rainbow, and breathing patterns
Installation Steps
- Install FastLED Library:
- Open Arduino IDE
- Go to Tools → Manage Libraries
- Search for "FastLED" and install it
- Wire the Components:
- Connect potentiometer middle pin to A0
- Connect outer pins to 5V and GND
- For NeoPixel: Data pin to digital pin 6 (through 220Ω resistor)
- Upload and Test:
- Set
USE_NEOPIXELto match your LED strip type - Adjust
NUM_LEDSto match your strip length - Upload code and turn the potentiometer!
- Set
Tips for Success
- Power Supply: NeoPixel strips need adequate power (5V, calculate ~60mA per LED)
- Capacitor: Add 1000µF capacitor between power rails for stability
- Resistor: Always use 220Ω resistor on NeoPixel data line
- MOSFET: For regular high-power LED strips, use MOSFET switching circuit
Possible Extensions
- Add multiple potentiometers for RGB color control
- Include buttons to switch between effects
- Add sound sensor for music-reactive lighting
- Connect multiple LED strips for room lighting
// LED Strip Controller with Potentiometer
// Compatible with both WS2812B (NeoPixel) and regular LED strips
#include <FastLED.h>
// Configuration - Choose your LED strip type
#define USE_NEOPIXEL true // Set to false for regular LED strips
// Pin definitions
#define POT_PIN A0 // Potentiometer analog pin
#define LED_DATA_PIN 6 // Data pin for NeoPixel strip
#define LED_PWM_PIN 9 // PWM pin for regular LED strip
// NeoPixel settings
#define NUM_LEDS 30 // Number of LEDs in your strip
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
// Variables
CRGB leds[NUM_LEDS]; // Array to hold LED colors
int potValue = 0; // Potentiometer reading
int brightness = 0; // LED brightness value
int lastBrightness = -1; // To avoid unnecessary updates
// Smoothing variables
const int numReadings = 5;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
void setup() {
Serial.begin(9600);
// Initialize smoothing array
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
if (USE_NEOPIXEL) {
// Initialize FastLED for NeoPixel strips
FastLED.addLeds<LED_TYPE, LED_DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(255);
// Set initial color (you can change this)
fill_solid(leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
} else {
// Initialize PWM pin for regular LED strips
pinMode(LED_PWM_PIN, OUTPUT);
}
Serial.println("LED Strip Controller Ready!");
Serial.println("Turn the potentiometer to control brightness");
}
void loop() {
// Read and smooth potentiometer value
potValue = readSmoothedPot();
// Map potentiometer value to brightness (0-255)
brightness = map(potValue, 0, 1023, 0, 255);
// Only update if brightness changed significantly
if (abs(brightness - lastBrightness) > 2) {
if (USE_NEOPIXEL) {
updateNeoPixelBrightness();
} else {
updateRegularLEDBrightness();
}
lastBrightness = brightness;
// Debug output
Serial.print("Pot: ");
Serial.print(potValue);
Serial.print(" | Brightness: ");
Serial.println(brightness);
}
delay(10); // Small delay for stability
}
// Function to read and smooth potentiometer values
int readSmoothedPot() {
// Remove oldest reading
total = total - readings[readIndex];
// Add new reading
readings[readIndex] = analogRead(POT_PIN);
total = total + readings[readIndex];
// Advance to next position
readIndex = (readIndex + 1) % numReadings;
// Calculate average
return total / numReadings;
}
// Function to update NeoPixel brightness
void updateNeoPixelBrightness() {
// Method 1: Adjust global brightness
FastLED.setBrightness(brightness);
FastLED.show();
/* Alternative Method 2: Adjust individual LED colors
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(160, 255, brightness); // Hue=160 (blue), full saturation
}
FastLED.show();
*/
}
// Function to update regular LED strip brightness
void updateRegularLEDBrightness() {
analogWrite(LED_PWM_PIN, brightness);
}
// Bonus function: Color cycling effect for NeoPixels
void colorCycleEffect() {
static uint8_t hue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue + (i * 10), 255, brightness);
}
FastLED.show();
hue++;
delay(50);
}
// Bonus function: Rainbow effect for NeoPixels
void rainbowEffect() {
static uint8_t startHue = 0;
fill_rainbow(leds, NUM_LEDS, startHue, 255/NUM_LEDS);
// Apply brightness
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].fadeToBlackBy(255 - brightness);
}
FastLED.show();
startHue++;
delay(20);
}
// Bonus function: Breathing effect
void breathingEffect() {
static float breathPhase = 0;
// Create breathing brightness using sine wave
float breathBrightness = (sin(breathPhase) + 1) * 0.5; // 0 to 1
int finalBrightness = (brightness * breathBrightness);
if (USE_NEOPIXEL) {
FastLED.setBrightness(finalBrightness);
FastLED.show();
} else {
analogWrite(LED_PWM_PIN, finalBrightness);
}
breathPhase += 0.1;
delay(50);
}
/*
* Usage Notes:
*
* 1. Install FastLED library: Tools > Manage Libraries > Search "FastLED"
*
* 2. For WS2812B strips:
* - Set USE_NEOPIXEL to true
* - Connect: Data→Pin 6, VCC→5V, GND→GND
* - Add 220Ω resistor between Arduino pin and data line
*
* 3. For regular LED strips:
* - Set USE_NEOPIXEL to false
* - Use MOSFET circuit for high current strips
* - Connect gate to Pin 9 through 1kΩ resistor
*
* 4. Potentiometer wiring:
* - Middle pin → A0
* - One side → 5V, other side → GND
*
* 5. To use bonus effects, replace the main loop content with:
* colorCycleEffect(); or rainbowEffect(); or breathingEffect();
*/https://claude.ai/public/artifacts/a6ef7acc-417a-47d0-bfc2-1afa0d5bfa37