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( )
- Materials needed to power an LED light with Arduino
- How to do the assembly of a circuit with Arduino?
- Code to use to turn a led on and off with Arduino
- Ways to turn LEDs on and off with an Arduino system
- With Arduino and bluetooth
- Using two pushbuttons
- With a button
- How to turn LEDs on or off in sequence with Arduino? – Steps to follow
- 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:
- const int LED = 13; // variable for the port
- void setup ()
- {
- pinMode (LED, OUTPUT); // set the port as led output
- }
- void loop ()
- {
- digitalWrite (LED, HIGH); // turn on led
- delay (1000);
- digitalWrite (LED, LOW); // turn off the led
- delay (1000)
- }
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:
- cons int led = 13; // declaration for the port
- int option; // variable to read information
- void setup ()
- {
- begin (9600);
- pinMode (led, OUTPUT); // led output set
- }
- void loop ()
- {
- if (Serial.available ()> 0) {// asking for serial input
- char option = Serial.read ();
- if (option> = ‘1’ && option <= ‘9’) {// reading a number between 1 and 9
- option – = ‘0’;
- for (int i = 0; i <option; i ++)
- {
- digitalWrite (led, HIGH);
- delay (1000);
- digitalWrite (led, Low);
- delay (1000);
- }
- }
- }
- }
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:
- const int button = 7; // assigning pin 7 to button variable
- const int led = 13; // assigning pin 13 to led variable
- int statusled = LOW; // initial state of the led
- int laststate button;
- int current state button;
- Declare the inputs and outputs in the setup () section
- pinMode (led, OUTPUT); // assigning led as output
- pinMode (button, INPUT); // assigning button as input
- digitalWrite (led, LOW); // setting output to ‘0’ for led off
- 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:
- laststatebutton = currentstatebutton; // saving the last state of the button
- currentstate button = digitalRead (button); // reading current state of button
- if (laststate button == HIGH && currentstate button == LOW) {// asking for state of pin 7
- estadoled =! estadoled; // change the last state of the led
- digitalWrite (led, ledstatus); // write the status of the led to the output
- }
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:
- int pinLED [4] = {5,6,7,8} // defining the outputs
- int time = 1000; // delay time of one second
- void setup ()
- {
- int i = 0;
- for (i = 0; i <4; i ++) {// with variable i the list is traversed
- pinMode (pinLED [i], OUTPUT); // each output is set for led
- }
- }
- void loop ()
- {
- int i = 0;
- for (i = 0; i <4; i ++) {// run the list to turn the LEDs on and off
- digitalWrite (pinLED [i], HIGH); // turn on led i
- delay (time);
- digitalWrite (pinLED [i], LOW); // turn off led i
- delay (time);
- }
- }
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.