How to turn an LED light on and off with Arduino

Currently there are various technological advances that allow diversifying the activities that you can carry out at the same time, which is why microcontrollers have gained ground. In this last scenario, Arduino offers you, together with Google Assistant, the possibility of creating a controlled environment at home by using tools to carry out automatic processes . However, if you are interested in learning how to turn an LED light on and off with Arduino using one or more buttons, you will not 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.

Index(  )

  1. Materials needed to power an LED light with Arduino
    1. How to do the assembly of a circuit with Arduino?
  2. Code to use to turn a led on and off with Arduino
  3. Ways to turn LEDs on and off with an Arduino system
    1. With Arduino and bluetooth
    2. Using two pushbuttons
    3. With a button
  4. How to turn LEDs on or off in sequence with Arduino? – Steps to follow
  5. Other applications for an Arduino board

Materials needed to power an LED light with Arduino

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

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

How to do the assembly of a circuit with Arduino?

Before anything else , the polarity of the LED must be known so that a correct connection is made. Usually the longest leg is the anode while the cathode side has a flat edge. Once the polarity of it is known, you can proceed to assemble the circuit with Arduino.

As seen in the image, the anode will be connected to pin 13 of the Arduino and the cathode to ground (or ground). The resistor is placed to activate the LED active high , this will be understood in the code to be carried out. Usually a breadboard is used to create electronic circuits for testing with an Arduino board, just like 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 the port
  2. void setup ()
  3. {
  4. pinMode (LED, OUTPUT); // set the port as led output
  5. }
  6. void loop ()
  7. {
  8. digitalWrite (LED, HIGH); // turn on led
  9. delay (1000);
  10. digitalWrite (LED, LOW); // 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. Then the output of the LED is set with the ‘pinMode’ function where it is specified through the variable that pin 13 will be where the LED will be located. Next, between the ‘void loop’ keys (which the Arduino does continuously) the LED is turned on and off with one second per second in between.

The type of connection made is important because the ‘digitalWrite’ function turns the LED on with ‘HIGH’ which means that it sends a high signal (a value of 1) through the set output (in this case pin 13). Similarly, 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 to have the programming software for the Arduino boards , to obtain this tool you can go to the official Arduino 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 board and have it do the job you want. In this specific case, we will take care of turning an LED on and off via bluetooth, a button or two buttons.

With Arduino and bluetooth

It is possible to use a mobile application that connects via bluetooth with the Arduino board. To do this, we first need to connect the HC-05 module that incorporates 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.

Connecting the HC-05 module to the Arduino is very simple. We only feed through ‘Vcc’ and ‘GND’ while in the ‘TXD’ port of the Arduino , which is its transmission, we connect it to the ‘RXD’ port of the module , which is the receiving port . We do the same with the ‘RXD’ port of the Arduino that we connect it to the ‘TXD’ of the module.

After this process is done, the coding is really simple, similar to the previous one we have done. Next, to turn an LED on and off via bluetooth in Arduino , the following instructions are created and implemented:

  1. cons int led = 13; // declaration for the port
  2. int option; // variable to read information
  3. void setup ()
  4. {
  5. begin (9600);
  6. pinMode (led, OUTPUT); // led output set
  7. }
  8. void loop ()
  9. {
  10. if (Serial.available ()> 0) {// asking for serial input
  11. char option = ();
  12. if (option> = ‘1’ && option <= ‘9’) {// reading a number between 1 and 9
  13. option – = ‘0’;
  14. for (int i = 0; i <option; i ++)
  15. {
  16. digitalWrite (led, HIGH);
  17. delay (1000);
  18. digitalWrite (led, Low);
  19. delay (1000);
  20. }
  21.  }
  22.     }
  23.         }

It should be noted that the customary indentation in a programming language is not being fulfilled , however, this code is fully functional. The explanation of this code begins with the declaration of two integer variables, one constant and the other variable. Pin 13 is assigned as the output for an LED and then the Arduino is constantly asking about the information sent via bluetooth. In this case, a number between 1 and 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, you must follow the steps described above , taking into consideration the code and the connection that will be shown below:

With a button

Before starting the programming it is very necessary to establish 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 at hand, proceed to make the connections in the figure.

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

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

At the end of the programming code, connect your Arduino board to the PC and choose the type of board that you have in tools, in addition, you must place the port that the PC shows you when you connect the board to it and click on the ‘Upload button ‘which is represented by a horizontal arrow pointing to the right. By doing 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

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

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

The assembly is simple, having to connect each anode of each LED to ground or ground while each cathode will go to a different port of the Arduino . In this case it will be from port 5 to 8. You cannot forget to place a 120 ohm resistor between the LED and the Arduino port for protection.

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

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

In this way it is possible to turn an LED light on and off sequentially , as simple as the instructions written above. Using two for cycles it is possible to turn 4 LEDs on and off simply and constantly. Likewise, the time between ignition 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 the use of sensors to measure states of various variables (Temperature, pressure, speed, among others) and device controls domestic 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 as an Arduino board. In general, you can make use of these devices to create  inventions or improvements of different instruments , devices, equipment. In addition, you will have many Arduino options available on the market to complement the developments you want.


by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment