Program On / Off Button with Arduino  Step by Step Guide ▷ 2020

In this article We will teach you to program in the simplest way possible an ON / OFF button in Arduino. After reading this post you will be able to mount any plate and add switches to it like a true professional.

To achieve this, First we will tell you the materials you need and then we will teach you the step by step that you should do to program a button on and off with Arduino.

But this is not all you will see in these paragraphs, we will also present you the best Arduino kits to work with buttons and pushbuttons. Do not miss anything if you want to enjoy your future inventions.

What do I need to program an on / off button with Arduino? Most important materials

The most important materials you will need to program an on / off button with Arduino are:

  • Arduino compatible board. In general, the type One R3, Leonardo or And a. You must ensure that the quality of the assembly and the components are the best possible, this will help you to better develop your project.
  • Insert plate, you can also find it on the market as a Protoboard. This pegboard will help you insert the button ON / OFF you want in a simpler way. Generally, the size should be 4.5 by 7.5 centimeters, but it will depend on your needs.
  • It is important that you buy an experimentation kit, which includes a large number of these elements. You will need for this project a resistance with a power of 10 K ohms and a tolerance of 1%.
  • Switch or Push Button Switch. The component that you want included is given so that it works opening and closing the circuit. You will want to get a friendly 4-pin switch that has a resistance of up to 50 amps.
  • Necessary cables for connecting the parts.

Learn step by step how to program an On / Off button or pushbutton with Arduino

Before starting this task you will have to know that any button can be connected to a pin on the Arduino board, even if it is digital or analog. In addition, there are two types of settings that can be used, with resistance Pull Up or with Pull Down resistance. Once you have all the materials that we mentioned in the previous paragraph, what you should do now than follow this step by step to program an ON / OFF button with Arduino.

See below:

Connect button

When you want to connect the button you will have to take into account if you are going to do it with a Pull Up resistance or with a Pull Down resistance. The first thing you will need to do is place the button on the insert plate via the pins and then attach it. To carry out this stage you have to leave two separate legs on each side of the indentations of the breadboard.

Next, you’ll need to connect the top leg to 5V from the plate Arduino and then plug the lower pin to the resistor. The leg that is freed from resistance must be connected to the pin that supplies the voltage, that is to GND. Then a line from the insert plate should be connected to the place where the button and resistor are connected.

Set Pull Up and Pull Down resistance

Set Pull Up and Pull Down resistance

In this step you will have to establish if you want to have a high state when you do not hold the button or low when the switch is pressed. If you decide to obtain these values ​​you should lean towards a resistance of the type Pull up. On the other hand, if you need the state to be low when the high button is not pressed when the switch is pressed, you will have to choose a resistor to obtain this effect Pull down.

Build Circuit

What you will have to do in this step is to assemble the circuit. For this you will have to place the Arduino board together with the breadboard, in which you must incorporate the push button, the resistance and connect both plates with the necessary cables.

Program the button

The first thing you will have to do to program button is to assign the pins.

For this you will have to write:

const int pinon = 2;

const int pinoff = 3;

const int pinled = 13;

Next, you must indicate the state variables of the buttons:

int estaon = HIGH;

int estaoff = HIGH;

void setup() {

After this, you will have to configure the pins as input and output. You can do this by writing commands.

pinMode(pinon, INPUT);

pinMode(pinoff, INPUT);

pinMode(pinled, OUTPUT);

}

void loop() {

Once you have the above assigned, you will have to enter:

estaon = digitalRead(pinon);

estaoff = digitalRead(pinoff);

Now, you will have to establish what happens when the button is pressed:

if (estaon == LOW) { Por ejemplo, si quieres que se encienda un led tendrás que escribir digitalWrite(pinled, HIGH);

}

In case the button is off, you will have to enter the commands:

if (estaoff == LOW) { Y hacer que se apague el led según el ejemplo que seguimos, digitalWrite(pinled, LOW);

}

}

Eliminate bouncing and noise

Anytime program the on / off button removing bounces and noises you will have to follow a few steps.

For this you will have to enter this command to assign pins:

const int pinon = 2;

const int pinoff = 3;

const int pinled = 13;

Then you must set the state variables of the button, this you can do:

int estaon = HIGH;

int estaoff = HIGH;

void setup() {

When you’re done, you’ll have to type to configure the input pin:

pinMode(pinon, INPUT);

pinMode(pinoff, INPUT);

Then you will have to set the output pin:

pinMode(pinled, OUTPUT);

}

void loop() {

Next, you will have to enter the commands so that set variables can be read in the button state:

estaon = digitalRead(pinon);

estaoff = digitalRead(pinoff);

Then, you will have to indicate what happens when the button is lit:

if (estaon == LOW) {

delay(50);

if (estaon == LOW ) {

digitalWrite(pinled, HIGH);

}

}

In case the button is off, the process will be:

if (estaoff == LOW) {

delay(50);

if (estaoff == LOW) {

digitalWrite(pinled, LOW);

}

}

}

Meet the best Arduino Kits to work with buttons and pushbuttons

Next, we will show you the best Arduino kits you can use to work with buttons and push buttons.

Choose the one that suits you:

LYFEI LDTR-RM012

LYFEI LDTR-RM012

This button and push button kit for Arduino boards supports a voltage of 3 to 5 and the size of the led is 12 x 12 mm, which you can choose in different colors. It can not only be used on plates Arduino but also in Raspberry Pi and 51 singlechip, so it becomes a versatile and useful tool for any project.

ELEGOO

The present set contains an Arduino UNO R3 board, a 9 volt battery, a manual in Spanish and several buttons and pushbuttons so you can work on any type of project. In addition, it includes a 5V relay and different colored LEDs.

ELEGOO connector kit

ELEGOO connector kit

Resistors that you can find in this kit will have a capacity of 10 to 300 Ohm and 1 to 100k. In addition, you will have connectors of up to 40 pins, LEDs of various colors, rotary potentiometers and 6 pieces of buttons.

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will also be of great help to more members of the community. Thank you! 😉

You may be interested:

Leave a Comment