▷ LED brightness control with two buttons - Arduino

This project consists of adjusting the brightness of an LED using two buttons connected to an Arduino. One of the buttons increases the brightness, while the other decreases it. To achieve this, PWM (Pulse Width Modulation) control is used to vary the intensity of the LED smoothly and in a controlled manner.

Materials:

The materials we will use for this project are as follows:

  • 1 Arduino Uno.
  • 2 Buttons
  • 2 Resistors of 1k
  • 1 Resistor of 220 Ohms.
  • 1 LED diode (Any color).

Connection Diagram:

Explanation of the operation:

For this project, the LED must be connected to a pin with PWM; the buttons are connected to pins A1 and A2 respectively.

Just like in the previous project, to control the brightness of the LED, we will modify the PWM value; in this case, we will not use any function, we will simply create a variable called pwm, the value of this variable will be added or subtracted depending on which button we press.

Documented code:

/*

* Code written by: Esteban Carrillo for EDEPTEC.
* Website: https://www.edeptec.com
* Facebook: @edeptec
* Youtube: https://youtube.com/c/EDEPTEC

*/

// Definition of pins used in the project
#define led 3         // Pin to which the LED controlled by PWM is connected
#define pulSuma A1 // Pin to which the button for increasing brightness is connected
#define pulResta A2 // Pin to which the button for decreasing brightness is connected

// Variable declaration to store the PWM value
byte pwm = 0;

void setup() {
// Configuration of pins as input or output
  pinMode(led, OUTPUT); // Configures the LED pin as output
  pinMode(pulSuma, INPUT); // Configures the sum button pin as input
  pinMode(pulResta, INPUT); // Configures the subtract button pin as input
}

void loop() {
// Increment or decrement the PWM value based on the state of the buttons
  (digitalRead(pulSum) == HIGH) && pwm++; // If the add button is pressed, increment the PWM
  (digitalRead(pulResta) == HIGH) && pwm--; // If the subtract button is pressed, decrement the PWM
 
  delay(20); // Delay to avoid button bouncing

// Limit the PWM value range between 0 and 255
  if (pwm > 255) pwm = 255; // If the PWM value exceeds 255, limit it to 255
  if (pwm < 0) pwm = 0; // If the PWM value drops below 0, limit it to 0

// Update the LED PWM value
  analogWrite(led, pwm);     // Write the PWM value to the LED pin
}

0/Leave a comment/Comments

Hello! We're so glad you've made it this far and are reading this article on Edeptec.

This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.

» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?

Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.

Thank you for being part of this learning community! Your participation is what makes this project grow.