- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
- Description: Control the color of an RGB LED using three potentiometers for red, green, and blue components.
- Requirements: RGB LED, 3 potentiometers, resistors.
- Learning Points: PWM (analogWrite), RGB color blending.
Schematic:
Code:
// File: RGB_LED_Color_Mixer.ino
// Define RGB LED pins
const int redPin = 11; // Red LED (PWM)
const int greenPin = 10; // Green LED (PWM)
const int bluePin = 9; // Blue LED (PWM)
// Define potentiometer pins
const int redPot = A0; // Red potentiometer
const int greenPot = A1; // Green potentiometer
const int bluePot = A2; // Blue potentiometer
void setup() {
// Set RGB pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Start Serial Monitor for debugging (optional)
Serial.begin(9600);
}
void loop() {
// Read potentiometer values (0 to 1023)
int redValue = analogRead(redPot);
int greenValue = analogRead(greenPot);
int blueValue = analogRead(bluePot);
// Map potentiometer values (0 to 255) for PWM
redValue = map(redValue, 0, 1023, 0, 255);
greenValue = map(greenValue, 0, 1023, 0, 255);
blueValue = map(blueValue, 0, 1023, 0, 255);
// Apply PWM values to RGB LED
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
// Debugging: Print values to Serial Monitor
Serial.print("Red: ");
Serial.print(redValue);
Serial.print(" | Green: ");
Serial.print(greenValue);
Serial.print(" | Blue: ");
Serial.println(blueValue);
delay(50); // Small delay for stability
}
Colors:
white:
Blue:
Green:
Red:
Black:
Blend1:
Blend2:
How It Works
Potentiometer Values:
- Each potentiometer adjusts a single color component (Red, Green, or Blue).
- The potentiometer’s position determines an analog value between 0 and 1023.
Mapping Values:
- The analog input values (0–1023) are mapped to the PWM range (0–255) using
map()
.
- The analog input values (0–1023) are mapped to the PWM range (0–255) using
PWM Output:
analogWrite()
is used to set the brightness of each color component.
Color Mixing:
- By combining different brightness levels of Red, Green, and Blue, you can create millions of colors.
Or just Download the Project here:
https://drive.google.com/file/d/1lMa3po8TXfzRmLPR1ty03hLwuU_fmvQc/view?usp=drive_link
And watch my youtube channel, for more tutorials!
- Get link
- X
- Other Apps
Comments
Post a Comment