software

How to program an on / off button or pushbutton with arduino like an expert? Step by step guide

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

To achieve this, we will first tell you the materials you need and then we will teach you the step by step you must 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 . Don’t miss out on 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. Typically Uno R3, Leonardo or  Yun type are used . 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 ON / OFF button 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 experiment kit, which includes a large number of these items. 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 you want included is given so that it works by opening and closing the circuit. You will want to get a friendly 4-pin switch that has a resistance of up to 50 amps.
  • Cables necessary for connecting the parts.

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

Before starting with 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 configurations that can be used, with a Pull Up resistor or with a Pull Down resistor . 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 resistor or with a Pull Down resistor . 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 will need to connect the upper pin to 5V of the Arduino board and then plug the lower pin to the resistor. The leg that is free of the resistor must be connected to the pin that supplies the voltage, that is, to GND. Then a line from the insert plate will need to be connected to the place where the button and resistor are connected.

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 keep the button pressed or low when the switch is pressed. If you decide to obtain these values ​​you should lean towards a Pull Up type resistor . 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 Pull Down resistor to obtain this effect .

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 resistor and join both boards with the necessary cables.

Program the button

The first thing you will have to do to program the button is 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 is = HIGH;

int estaoff = HIGH;

void setup () {

After this, you will have to configure the pins as input and output. You can do this by typing the 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) {For example, if you want a led to turn on you will have to write digitalWrite (pinled, HIGH);

}

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

if (estaoff == LOW) {And make the led turn off according to the example we follow, digitalWrite (pinled, LOW);

}

}

Eliminate bouncing and noise

When you want to program the on and off button eliminating bounces and noise, 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 is = 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 (are == LOW) {

delay (50);

if (are == 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 that you can use to work with buttons and pushbuttons.

Choose the one that suits you:

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 from different colors. It can be used not only on Arduino boards but also on Raspberry Pi and 51 singlechip, making it a versatile and useful tool for any project.

ELEGOO

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

ELEGOO connector kit

The 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button