Arduino Code: Potentiometer-Controlled LED
LEDs arduino
Negative legs GND
Positive leg (red) Pin 12
Positive leg (blue) Pin 11
cpp// Constants
const int potPin = A0; // Pin where potentiometer is connected
const int ledPin = 9; // Pin where LED is connected (PWM pin)
// Variables
int potValue = 0; // Variable to store the potentiometer value (0-1023)
int ledBrightness = 0; // Variable to store the converted LED brightness (0-255)
void setup() {
// Set up the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the potentiometer value (from 0 to 1023)
potValue = analogRead(potPin);
// Map the potentiometer value to a range of 0 to 255 (for PWM)
ledBrightness = map(potValue, 0, 1023, 0, 255);
// Set the LED brightness using PWM
analogWrite(ledPin, ledBrightness);
// Small delay for stability
delay(10);
}
guilgikjllkjhipioiip
Comments
Post a Comment