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, such is the case of the automation of both industrial and home processes. 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, since it is a manual procedure that requires in principle that you know the terms and definitions of the Arduino board in any of its presentations.

Procedure to turn an LED light on and off using a push button on Arduino

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 using one or more buttons.

LED light control by push button on Arduino

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 cables, the LED itself, electrical resistances to limit the current that flows through the device and protect it from possible failures that cause a fault 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 is carried 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. statusled =! statusled; // 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 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 button «Upload »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.

LED light control by two pushbuttons on Arduino

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:

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 undertake 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