software

How to create a push button and use it as a switch in arduino from scratch? Step by step guide

An essential element that every creator and developer should have is Arduino . A useful platform for creating free hardware and software electronic devices . If you are interested in how to create a push button and use it as a switch, then continue reading this step by step guide .

With Arduino you can develop different models of single board microcomputers. Being free software and hardware, it offers the necessary bases so that everyone can develop, modify, and give it their own utilities.

Arduino boards offer enormous freedom when working, so you can use them for all kinds of things. Learn how to create a push button to use as a switch! .

What is the difference between a pushbutton and a switch?

A button is understood to be a component that is present in almost all electronic devices today . They are also called as keys or buttons. A good example to illustrate a push button would be a doorbell. When you press the button, a signal is activated, which turns off the moment you press. However, what is the difference between a pushbutton and a switch? .

For its part, the switch is a component that is responsible for opening or closing a circuit . When it is activated, it remains in that state until it is executed again by the user. A clear example would be the light switch. There then lies the main difference between a pushbutton and a switch . While one opens or closes the circuit while it is being pressed, returning to its initial state when it is released, the other maintains its state (open or closed) after each actuation.

Learn step by step how to create a push button and program it as a switch with Arduino

First of all, to understand the step by step of how to create a push button and program it as a switch, it is necessary to know the correct way to connect a button in Arduino.

Here are some essential points that you should keep in mind.

  • The push button can be connected to any of the Arduino pins, be it digital or analog.
  • You must configure the selected pin as a digital input.
  • It is essential to consider the presence of noise and bounce in the software.
  • You can make two types of configurations: With Pull-Down resistance or Pull-Up resistance. Regarding the function of the resistors, it can be said that it is to maintain a certain state for the moment when the pushbutton is not being actuated. They are necessary either internal or external.
  • When the pushbutton is not actuated, the Pull-Up resistor maintains a high state. When pressed, the active state becomes low.
  • When the pushbutton is not actuated, the Pull-Down resistor maintains a low state. When pressed, the active state becomes high.

Programming a switch into a pushbutton is a fairly simple practice. All you have to do is create a variable that will change its value each time the button is pressed. Depending on the state it is in, it will change from 1 to 0 or vice versa when pressed. Finally, you will be able to turn on the LED based on the state of the variable and not the button.

To carry it out, the following crafting materials are necessary:

  • 1 Arduino.
  • 1 LED.
  • 1 Breadboard.
  • Cables.
  • Data cable
  • 1 Resistance for the pushbutton.
  • 1 Resistor for the LED.

The mounting scheme may vary depending on the configuration of the resistors. Whether these are Pull-Up or Pull-Down, internal or external.

In the case of being an internal Pull-Down resistor, the code to enter would be the following:

#define Push 2 // We give the alias to our pin 2.
#define LED 3 // We give the alias to our pin 3.
int Push_lee = 0;
int ledstate = 0;
int previousState = 0;
void setup ()
{
pinMode (LED, OUTPUT); // We define the LED pin as an output.
pinMode (Push, INPUT); // We define the Push pin as input.
}
void loop () // We define our sequence.
{
Push_lee = digitalRead (Push); // We store the button reading in the variable.
if ((Push_lee == 1) && (oldState == 0))
{
stateLed = 1 - stateLed;
delay (10);
}
oldState = Push_lee;

if (estadoLed == 1) // Condition that if we have the value of 1, it enters it.
{
digitalWrite (LED, HIGH); // A STOP is sent to the LED.
}
else // Condition that is met if the if is not met.
{
digitalWrite (LED, LOW);
}
}

Once the assembly diagram and programming code are completed, you must transfer it to the Arduino board via the network cable .

The best Arduino projects with switches and pushbuttons to practice your skills

As mentioned above, Arduino’s free hardware and software lends itself to all kinds of creations and utilities . For that reason, we leave you some of the best Arduino projects with switches and buttons to practice your skills .

Let’s see next:

Simple traffic light

The simple semaphore is one of the most basic Arduino projects out there. In basic terms, you must program a code that allows you to simulate a traffic light . Many people decide to make a change and convert it to make it “smart.”

Both projects are relatively easy and consist of almost the same materials:

  • Arduino UNO .
  • Breadboard.
  • 3 or 6 220 Ω resistors.
  • 2 10 K Ω resistors.
  • 2 pushbuttons.
  • Red, yellow and green LEDs.

The code to enter is the following:

/ **
* Simple traffic light
* /

void setup () {
pinMode (13, OUTPUT);
pinMode (12, OUTPUT);
pinMode (11, OUTPUT);
digitalWrite (13, LOW);
digitalWrite (12, LOW);
digitalWrite (11, LOW);
}

void loop () {
digitalWrite (13, LOW);
digitalWrite (11, HIGH);
delay (5000);
digitalWrite (11, LOW);
digitalWrite (12, HIGH);
delay (1000);
digitalWrite (12, LOW);
digitalWrite (13, HIGH);
delay (5000);
}

LED circuit

The goal of this practice is to be able to light multiple LEDs, one after the other. It is a device that can be achieved by following the same sequences and instructions already known .

Your materials would be:

  • Arduino Uno.
  • Cables.
  • Resistors.
  • Multiple LEDs.
  • Breadboard.

If you connect different LEDs to different digital pins in Arduino, you have to declare it in the setup () function which can be as follows:

void setup ()
{
// initialize the digital pins as an output
pinMode (13, OUTPUT);
pinMode (12, OUTPUT);
pinMode (11, OUTPUT);
…………………………
pinMode (6, OUTPUT);
}

In turn, the loop () should be repeated as many times as there are LEDs . However, it is a better C ++ solution, as it is a convenient means of indicating that you should repeat a specific number of times. To do this , the For function is applied in combination with a variable . So, to start pins 13 through 6 as outputs in setup (), you can use the for statement.

Based on that, it would be as follows in code: 

void setup ()
{
int i = 0; // We initialize the variable i as an integer
for (i = 6; i <14; i ++)
pinMode (i, OUTPUT);
}

Under the same criteria, the loop () function could also be written as follows:

void loop ()
{
int i = 0; // We initialize the variable i as an integer
for (i = 6; i <14; i ++)
{
digitalWrite (i, HIGH);
delay (500);
digitalWrite (i, LOW);
delay (500);
}
}

Electric socket

It consists of simulating a six-sided die, and being able to choose a random number between 1 and 6 by pressing a button. It is a simple project, but in which you have to be very careful with random results when programming, since it could always return the same sequence.

Materials are:

  • 1 Pushbutton
  • 1 7 segment display
  • 1 10 k resistor (pull down)
  • 1 Resistor 220 Ω

The complete code to use would be the following:

// Constant pins
#define PUSHBUTTON 10 // Multidimensional array to display the numbers
byte number [10] [8] =
{
{1, 1, 1, 1, 1, 1, 0, 0}, // 0
{0, 1, 1, 0, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1, 0}, // 2
{1, 1, 1, 1, 0, 0, 1, 0}, // 3
{0, 1, 1, 0, 0, 1, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1, 0}, // 5
{1, 0, 1, 1, 1, 1, 1, 0}, // 6
{1, 1, 1, 0, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1, 0}, // 8
{1, 1, 1, 0, 0, 1, 1, 0} // 9
}; void setup () {
// We start the serial monitor
Serial.begin (9600); // We put the segment pins in OUTPUT mode
for (int i = 2; i <10; i ++)
{
pinMode (i, OUTPUT);
}

// We put the pushbutton pin in INPUT mode (input)
pinMode (PUSH BUTTON, INPUT);

// We set the fixed seed
randomSeed (analogRead (A0));
}

void loop () {
// We read the value of the button
int value = digitalRead (PUSH BUTTON);

// If it's pressed
if (value == HIGH)
{

// Generate a random number between 1 and 6
int randomNumber = random (1, 7);

// We put the pins in the correct state to show the number randomNumber
for (int e = 0; e <8; e ++)
{
digitalWrite (e + 3, number [randomNumber] [e]);
}

delay (500);
}
}

Leave a Reply

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

Back to top button