LED Brightness Control with Two Buttons to Increase and Decrease

Control LED brightness on Arduino Uno using two buttons with PWM on pin 9 and software debounce for smooth increase and decrease in steps of 15.

Brief Introduction

LED Brightness Control with Two Buttons to Increase and Decrease (arduino)
DESCRIPTION:
This project allows you to adjust the brightness of an LED using two buttons: one to increase and one to decrease the brightness. The intensity is gradually modified via PWM on the LED pin.

ADDITIONAL NOTES:
The project uses two buttons with pull-down resistors to avoid floating readings. The LED brightness is controlled via PWM on pin 9. The brightness value ranges from 0 to 255 with steps of 15 for a visible but not too abrupt adjustment. Software debounce prevents unwanted multiple readings when pressing a button.

EXPLANATIONS:
- Pin 9 is used because it supports PWM on Arduino Uno, necessary to modulate the LED brightness.
- The buttons are connected with 10kΩ pull-down resistors to keep the signal LOW when not pressed, avoiding incorrect readings.
- Software debounce filters the electrical bouncing of the button, preventing a single press from being read multiple times.
- The brightness value is incremented or decremented in steps of 15 to have a visible but not too fast variation.
- The analogWrite() function applies the PWM value to the LED, adjusting its brightness.

Example Code: arduino

// LED brightness control with two buttons
// Button on D2 increases brightness
// Button on D3 decreases brightness

const int ledPin = 9;           // PWM pin connected to LED
const int btnUpPin = 2;         // Button to increase brightness
const int btnDownPin = 3;       // Button to decrease brightness

int brightness = 0;             // Brightness value (0-255)
const int step = 15;            // Brightness increment/decrement

// Variables for debounce management
unsigned long lastDebounceTimeUp = 0;
unsigned long lastDebounceTimeDown = 0;
const unsigned long debounceDelay = 50; // ms

int lastButtonUpState = LOW;
int lastButtonDownState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(btnUpPin, INPUT);
  pinMode(btnDownPin, INPUT);

  analogWrite(ledPin, brightness); // LED initially off
}

void loop() {
  // Read increase button state
  int readingUp = digitalRead(btnUpPin);
  if (readingUp != lastButtonUpState) {
    lastDebounceTimeUp = millis();
  }
  if ((millis() - lastDebounceTimeUp) > debounceDelay) {
    if (readingUp == HIGH) {
      brightness += step;
      if (brightness > 255) brightness = 255;
      analogWrite(ledPin, brightness);
    }
  }
  lastButtonUpState = readingUp;

  // Read decrease button state
  int readingDown = digitalRead(btnDownPin);
  if (readingDown != lastButtonDownState) {
    lastDebounceTimeDown = millis();
  }
  if ((millis() - lastDebounceTimeDown) > debounceDelay) {
    if (readingDown == HIGH) {
      brightness -= step;
      if (brightness < 0) brightness = 0;
      analogWrite(ledPin, brightness);
    }
  }
  lastButtonDownState = readingDown;

  delay(10); // Small pause for stability
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Arduino Uno (x1)

5mm LED (x1)

220Ω Resistor (x1)

Button (x2)

10kΩ Resistors (x2) for pull-down

CONNECTIONS: LED Cathode -> GND

CONNECTIONS: LED Anode -> 220Ω Resistor -> Arduino Pin D9

CONNECTIONS: Increase Button -> Arduino Pin D2 and GND via 10kΩ resistor (pull-down)

CONNECTIONS: Decrease Button -> Arduino Pin D3 and GND via 10kΩ resistor (pull-down)

Similar Topics