【 Use Push Button Switch with Arduino 】 Step by Step Guide ▷ 2022

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

With Arduino you can develop different models of single-board microcomputers. Being of free software and hardwareoffers the necessary bases so that every person can elaborate, modify, and give it their own utilities.

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

What is the difference between a push button and a switch?

A push button is understood to be a component that It 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 at the moment of pressing. Nevertheless, What is the difference between a push button and a switch?.

For his 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. That is where the main difference lies. between a push button and a switch. While one opens or closes the circuit while holding pressed, returning to its initial state when it is released, the other maintains its state (open or closed) after each activation.

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

First, to understand the step by step on 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 pinsbe it digital or analog.
  • You must configure the selected pin as a digital input.
  • It is essential to consider the presence of noise and bounces in the software.
  • You can make two types of configurations: With Pull-Down resistance or Pull-Up resistance. With respect to the function of the resistors, it can be said that it is to maintain a certain state for the moment in which the button is not being activated. They are necessary either internal or external.
  • When the button is not activated, the Pull-Up resistor maintains a high state. When pressed, the active state goes low.
  • When the button is not activated, the Pull-Down resistor maintains a low state. When pressed, the active state goes high.
See also  【 Track IP Address on Map 】 Step by Step Guide ▷ 2022

Programming a switch in a push button it is a fairly simple practice. All you have to do is create a variable that will change value each time the button is activated. Depending on the state it is in, it will change from 1 to 0 or vice versa when pressed. Finally, you will manage to light 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 Resistor for the button.
  • 1 Resistor for the LED.

The mounting diagram It may vary depending on the configuration of the resistors. Whether these Pull Up or Pull Downinternal 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 oldState = 0; void setup() { pinMode(LED,OUTPUT); //We define the LED pin as output. pinMode(Push,INPUT); //We define the Push pin as an input. } void loop() //We define our sequence. { push_read = digitalRead(Push); //We store the reading of the button in the variable. if((Push_read == 1) && (prevState == 0)) { ledState = 1 – ledState; delay(10); } oldState = push_read; 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 completed the assembly diagram and the programming codeyou should transfer it to the Arduino board through the .

See also  【 Make Triptych in Word 】 Step by Step Guide ▷ 2022

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

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

Let’s see next:

simple traffic light

The simple traffic light is one of the projects Arduino most basic 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 to it and convert it to make it “intelligent”.

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

  • .
  • Breadboard.
  • 3 or 6 resistors of 220 Ω.
  • 2 resistors of 10K ohms.
  • 2 buttons.
  • Red, yellow and green LEDs.

The code to enter is the following:

/** * Simple semaphore */ 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 The objective of this practice is to be able to turn on multiple LEDs, one after another. 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 on the 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. Nevertheless, is a better C++ solution, since it is a convenient means of indicating that it should repeat a specific number of times. To do it the For function is applied in combination with a variable. So, to start at setup() pins 13 through 6 as outputs, you can use the for statement.

Based on that, it would look like this in code:

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

See also  【 Technical Specifications of an Arduino 】 List ▷ 2022

Under the same criteria, you could also write the loop() function as follows:

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

electric dice

It consists of simulating a six-sided dice, and be 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 produce the same sequence.

Materials are:

  • 1 push button
  • 1 7-segment display
  • 1 Resistance 10 k (pull down)
  • 1 resistor 220 ohms

The complete code to use would be the following:

// Pin constants #define SWITCH 10// Multidimensional array to display numbers byte number = { { 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() { // Start the serial monitor Serial.begin(9600);/ / We put the pins of the segments in OUTPUT mode (output) for (int i = 2; i < 10; i++) { pinMode(i, OUTPUT); } // Put the button pin in INPUT mode (input) pinMode(PUSHBUTTON, INPUT); //We set the fixed seed randomSeed(analogRead(A0)); } void loop() { // Read the value of the switch int value = digitalRead(SWITCH); // If it is pressed if (value == HIGH) { //Generate a random number between 1 and 6 int randomNumber = random(1, 7); // Set the pins to the correct state to display the number randomNumber for (int e = 0; e < 8; e++) { digitalWrite(e + 3, number); } delay(500); } }

Loading Facebook Comments ...
Loading Disqus Comments ...