▷ LED blinking control with buttons on Arduino.

In this project you will learn to control the LED blinking using three buttons connected to an Arduino:

  • Turning On/Off the LED with a button.
  • Increasing the blinking speed.
  • Decreasing the blinking speed.

This example is ideal for beginners who want to learn how to work with digital inputs and digital/PWM outputs on Arduino.

Necessary materials

  • 1 Arduino UNO (or compatible).
  • 1 LED with resistor (220Ω or 330Ω).
  • 3 Buttons.
  • Connection wires.
  • Breadboard.

Wiring Diagram:

Code Explanation

The code defines three buttons: one to turn the LED on/off, another to increase the blinking time and a third to decrease it.

The variable tiempo controls the blinking speed in milliseconds, with minimum and maximum limits defined by tiempoMinimo and tiempoMaximo.

Full code

/*

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

*/

#define buttonSubtract A0 // Subtract button on pin A0
#define buttonAdd A1 // Add button on pin A1
#define buttonOnOff A2 // On/off button on pin A2

#define led 2 // led on pin 2

bool activateDeactivate = 0; // Variable to control the state of the led
boolpreviousStatePulOnOff = 0; // Variable to control the previous state of the on/off button
int time = 100; // Variable to control the blinking speed

int minimumTime = 50; // Variable to control the minimum time
int maximumTime = 5000; // Variable to control the maximum time

/**
* Initializes the pins of the buttons and the LED as input/output
* respectively.
 */
void setup()
{
  pinMode(buttonSubtract, INPUT);
  pinMode(buttonAdd, INPUT);
  pinMode(buttonOnOff, INPUT);
  pinMode(led, OUTPUT);
}

/**
* The main loop of the program.
 *
* In this loop, the state of the power button is checked and
* turning off the LEDs (pulOnOff), and it controls the state of the LED
* (on or off) and its blinking speed.
 */
void loop()
{
  if (turnOnOffLed())
  {
    digitalWrite(led, HIGH);
    pause(time);
    digitalWrite(led, LOW);
    pause(time);
  }
}
/**
* Checks the state of the on/off button and updates the variable
* activateDeactivate.
 *
 * @return true if the led should be on, false otherwise.
 */
bool turnOnOffLed()
{
  bool buttonOnOffState = digitalRead(buttonOnOff);
  (buttonOnOffState != previousPulOnOffState && buttonOnOffState) && (activateDeactivate = !activateDeactivate);
previousStatePulOnOff = buttonOnOffState;

  delay(1);
  return activateDeactivate;
}
/**
* Pauses the execution of the program for a time
* determined by the pauseTime parameter.
 *
* During the pause, the state of the on/off button is checked and
* the activateDeactivate variable is updated. If the on/off button
* is in LOW state, it exits the loop and ends the
* pause.
 *
 * @param pauseTime The pause time in milliseconds.
 */
int pause(int pauseTime)
{
// Spanish
  for (int i = 0; i < pauseTime; i++)
  {
    manageTime();
    if (!turnOnOffLed())
      break;
  }
}

/**
* Adjust the value of the time variable based on the state of the
* add and subtract buttons.
 *
* If the add button is in HIGH state, the value
* of time is increased by 10 milliseconds and a delay of 10 milliseconds occurs.
 *
* If the subtract button is in HIGH state, the value
* of time is decreased by 10 milliseconds and a delay of 10 milliseconds occurs.
 *
* It ensures that the value of time is not less than timeMinimum nor
* greater than timeMaximum.
 */
void handleTime()
{
  if (digitalRead(addButton))
  {
time = time + 10;
    delay(10);
  }
  if (digitalRead(subtractButton))
  {
time = time - 10;
    delay(10);
  }
time = (time < minimumTime)
? minimumTime
           : (time > maxTime)
? maxTime
: time;
}

Conclusion

This project demonstrates how to use buttons to interact in real time with an LED. You learned how to turn it on, turn it off, and adjust the blinking speed. With this foundation, you can expand the logic to other projects like motor control, lighting systems, or navigation menus in Arduino.

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.