How to turn an LED light on and off with Arduino with one or more buttons

Contents

Currently there are various technological advances that allow diversifying the activities that can be carried out at the same time, reason why microcontrollers have gained ground. In this last scenario, Arduino offers you, junto with Google Assistant, the ability to create a controlled environment at home using the use of tools to perform automated processes. However, if you are interested in learn how to turn an LED light on and off with Arduino if you use one or more buttons, you won't need advanced process automation software.

This is a manual procedure that requires in principle that you know the terms and definitions of operation of the Arduino board in any of its presentations.

Materials needed to power an LED light with Arduino

To turn an LED light on and off, one of the first practices done when studying microprocessors or microcontrollers, assembly must be done, that is not so difficult, only needing the following materials:

  • An LED of any color.
  • A resistance of 220 ohms.
  • Arduino Uno-R3 or Mega board 2560.
  • A computer.
  • Cables for the connections on the breadboard.
  • Circuit board.
  • USB printer cable.

How to make the assembly of a circuit with Arduino?

First of all the polarity of the LED must be known for a correct connection to be made. As always, the longest leg is the anode while the cathode side has a flat edge. Once the polarity of the same is known, you can proceed to assemble the circuit with Arduino.

arduino-led-turn-on-4824089

As seen in the image, the anode will connect to the pin 13 from the Arduino and the cathode to ground (or land). The resistance is placed to be able activate high active LED, we will understand it in the code to be made. As usual, a breadboard is used to create electronic circuits for testing with an Arduino board, just as we are doing.

Code to use to turn a led on and off with Arduino

To create the code you must have a basic experience in Arduino programming, starting with knowing the syntax correctly. But basically it would be the following algorithm that will be explained later:

  1. const int LED = 13; // variable for port
  2. empty configuration ()
  3. {
  4. pinMode (LED, EXIT); // set the port as led output
  5. }
  6. empty loop ()
  7. {
  8. digital writing (LED, HIGH); // turn on the led
  9. delay (1000);
  10. digital writing (LED, BAJA); // turn off the led
  11. delay (1000)
  12. }

As you can see, the code starts with the declaration of a constant variable of type integer or integer. Later, the LED output is configured with the function ‘pinMode’ where it is specified through the variable that the pin 13 will be where the LED will be located. Then, between the keys ‘Empty loop’ (what the Arduino does continuously) the LED turns on and off with a second of a second in between.

The type of connection made is important because the function ‘digitalWrite’ turn on the LED with ‘HIGH’ which means it sends a high signal (a value of 1) via configured output (in this case pin 13). In the same way, it turns off with 'LOW’ because a low signal is sent (a value of 0).

Ways to turn LEDs on and off with an Arduino system

Before starting the procedure it is essential have the programming software for the Arduino boards, to obtain this tool you can go to the Arduino official page and download the Arduino IDE in its latest version.

Once you have downloaded and installed the software, you can start the procedure to program the plate and let me do the work you want. In this specific case we will take care of turning on and off an LED via bluetooth, one button or two pushbuttons.

With Arduino and bluetooth

It is possible to use a mobile application that connects via bluetooth with the Arduino board. For this we first need to connect el modulo HC-05 that incorporate communication via bluetooth to the Arduino. This will be paired with our mobile through a specific application depending on the operating system. In Google Play Store we will find applications that establish communication through the serial port.

Connect the HC module-05 to the Arduino is very simple. We only feed through 'Vcc’ and 'GND’ while we are in the port ‘TXD’ by Arduino, what is the transmission of it, we connect it to the port Módulo ‘RXD’, what is the reception. We do the same with the ‘RXD port’ of the Arduino that we connect it to the 'TXD’ of the module.

Once this process is done, the coding is really simple, similar to the previous one we have done. Then, to turn an LED on and off via bluetooth on Arduino The following declarations are created and implemented:

  1. cons int led = 13; // port declaration
  2. int option; // variable to read information
  3. empty configuration ()
  4. {
  5. Serial.begin (9600);
  6. pinMode (led, EXIT); // led output set
  7. }
  8. empty loop ()
  9. {
  10. if (Serial.available ()> 0) {// requesting serial entry
  11. option char = Serial.read ();
  12. if (option> = ‘1’ && option <= '9') {// reading a number between 1 Y 9
  13. option – = ‘0’;
  14. in order to (int i = 0; i
  15. {
  16. digitalWrite (led, ALTO);
  17. delay (1000);
  18. digitalWrite (led, under);
  19. delay (1000);
  20. }
  21. }
  22. }
  23. }

Notably the usual indentation in a programming language is not being metHowever, this code is fully functional. The explanation of this code begins with the declaration of two integer variables, one constant and the other variable. The PIN 13 it is assigned as an output for an LED and then the Arduino constantly asks about the information sent via bluetooth. In this case, a number between 1 Y 9 will make the LED turn on and off the same times as the number received.

Using two pushbuttons

To control an LED light by means of two buttons on Arduino, should follow the steps described above, taking into account the code and the connection that will be shown below:

arduino-connection-two-push-buttons-led_13884-5892566

code-led-light-two-buttons_13884-2970444

With a button

simulation-board-arduino-led-light_13884-7854586

Before starting the programming it is very necessary configure the hardware or circuit to turn on the LED light. For this you will need connection cables, the LED itself, an electrical resistance to limit the current and protect it from possible failures and the button to control the on and off. Once you have the materials on hand, proceed to make the connections in the figure.

Now that you have the connection, we will proceed to program the device. For it, you must follow the following steps, establishing the correct order and without forgetting to place the punctuation marks that you will observe:

  • Declare the variables to be used:
  1. botón int const = 7; // assigning the pin 7 to button variable
  2. const int led = 13; // assigning the pin 13 a la variable led
  3. int statusled = LOW; // initial state of the led
  4. botón int laststate;
  5. int current state button;
  • Declare the inputs and outputs in the setup section ()
  1. pinMode (led, EXIT); // assigning led as output
  2. pinMode (button, ENTRY); // assigning button as input
  3. digitalWrite (led, UNDER); // setting the output to ‘0’ for led off
  4. current status button = digitalRead (button); // Read current button state
  • Now that the main settings have been established, proceed to perform the control loop to inquire about button status and turn the LED on or off accordingly. In the loop () you must write the following:
  1. laststatebutton = currentstatebutton; // saving last button state
  2. current status button = digitalRead (button); // reading the current state of the button
  3. if (laststate button == HIGH && currentstate button == LOW) {// asking for pin status 7
  4. statusled =! status; // changes the last state of the led
  5. digitalWrite (led, ledstatus); // write the status of the led to the output
  6. }

code-arduino-luz-led_13884-7791059

At the end of the programming code, connect your Arduino board to PC and choose the type of plate you have in tools, what's more, you must place the port that the PC shows you when you connect the board and click on the button “Increase” which is represented by a horizontal arrow pointing to the right . By performing these steps, you will have your Arduino ready to control your LED light with a single button to turn it on and off.

How to turn LEDs on or off in sequence with Arduino? – Steps to follow

How we have been programming in Arduino, now we will face a more entertaining challenge, turn on and off a led light but sequentially with 4 different LEDs. For this we will need the following materials:

  • Arduino board.
  • Circuit board.
  • Connection cables.
  • 4 LEDs.
  • 4 electrical resistances 120 ohms.

Assembly is simple, having to connect each anode of each LED to ground or ground weather each cathode will go to a different port on the Arduino. In this case it will be from the port 5 al 8. You cannot forget to place a resistance of 120 ohms between the LED and the Arduino port for protection.

There are two ways to create the Arduino code to perform this action, here we will go for the simplest one that is to use a list variable, as well as a for loop to turn each LED on and off. Below are the instructions that Arduino performs:

  1. int pinLED[4] = {5,6,7,8} // defining the outputs
  2. int time = 1000; // one second delay time
  3. empty configuration ()
  4. {
  5. int i = 0;
  6. for (i = 0; i <4; i ++) {// with the variable i the list is traversed
  7. pinMode (pinLED[i], PRODUCTION); // each output is configured for led
  8. }
  9. }
  10. empty loop ()
  11. {
  12. int i = 0;
  13. for (i = 0; i <4; i ++) {// run the list to turn the LEDs on and off
  14. digital writing (pinLED[i], HIGH); // turn on the led i
  15. delay time);
  16. digital writing (pinLED[i], UNDER); // turn off the led i
  17. delay time);
  18. }
  19. }

In this way it is possible turn an LED light on and off sequentially, as simple as the instructions written above. Using two for cycles it is possible to turn on and off 4 LED simply and consistently. In addition, the time between ignitions can be varied with the time variable.

Other applications for an Arduino board

There are many applications that can be used with the use of the different models of Arduino boards, among them you can find industrial applications with use of sensors to measure states of various variables (Temperature, Pressure, velocity, among others) and controls of home devices through the use of home automation.

However, you will see that you have tutorials available where they explain for example how to build a clock like an Arduino board. In general, you can use these devices to create inventions or improvements of various instruments, devices, equipment. What's more, you will have many Arduino options available in the market to complement the developments you want.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.