Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
PCBX
Published

Control LED Brightness and Blink Patterns with Arduino UNO

Unlock creativity with our Arduino project! Control LED brightness and blink patterns using a potentiometer and a push button.

BeginnerProtip124

Things used in this project

Story

Read more

Schematics

led_control_cover_yWJM1kxo6G.png

Code

Untitled file

Arduino
const int potPin = A0; // The potentiometer is connected to analog input A0
const int buttonPin = 2; // The push button is connected to digital input pin 2
const int ledPin = 3; // The LED is connected to digital output pin 3

void setup() {
    pinMode(ledPin, OUTPUT); // Set the LED pin to output mode
    pinMode(buttonPin, INPUT); // Set the push button pin to input mode
}

void loop() {
    int potValue = analogRead(potPin); // Read the value of the potentiometer (0-1023)
    int ledBrightness = map(potValue, 0, 1023, 0, 255); // Map the value of the potentiometer to 0-255

    if (digitalRead(buttonPin) == HIGH) {
        analogWrite(ledPin, ledBrightness); 
        delay(500);
        analogWrite(ledPin, 0);
        delay(500);
    }
    else
        analogWrite(ledPin, ledBrightness); 
}

Credits

PCBX
33 projects • 9 followers
Customer Success: Your one-stop solution for PCB and PCBA services, plus component sourcing. Enjoy FREE online simulation and EDA.
Contact

Comments

Please log in or sign up to comment.