WHAT’S HOT NOW

Code

Arduino

General

Electronics

» » » » » » CONNECTING AND PROGRAMING PUSH BUTTONS ON THE ARDUINO

 HOW TO CONNECT AND PROGRAM PUSH BUTTONS ON THE ARDUINO


Switches as well as sensors or other electronic devices like LEDs can be controlled by means of push buttons.



In this article, we will examine the process of connecting and enabling a button on an Arduino. We will also learn about floating pins, pull-up and pull-down resistors, the digitalRead() function and Arduino's internal pull-up resistor. After reading through this article, you will be able to add push buttons to any project.

INTRODUCTION TO PUSH BUTTONS

Push buttons for a variety of uses exist in different setups: from the desk to the desk.



The push button referred to in this article is sometimes called a tactile switch or momentary push button.


The pins on either side of the button have contacts inside the button housing. The button itself has a conductive piece of metal attached to it. When the button is pressed, the circuit is closed between the pins on each side and electric current is allowed to flow between them:


PROJECT example

To create a circuit that will turn the light on when a button is pressed, let's build a circuit that will operate on an LED. The LED is just a simple example, you can use this circuit to control any device that is powered by a 5-volt signal.

These are the parts required to finish the undertaking:

Follow this diagram to set up the circuit.


The limiting resistor's value can range from 200 Ohms to 1 K Ohms.

As one side of the push button is dedicated to 5Vs, and the other is dedicated to pinning 7, when the button is pressed, current from the other side of the push button will flow to pin 7 making it go high. Through this, we'll use the digitalRead() function to detect when this happens. Then we will use the digitalWrite() function to set pin 11 high, causing the LED to light up.

PROGRAMing A PUSH BUTTON ON THE ARDUINO

When you have connected the circuit to the Arduino, upload this code to it.
int buttonPin = 7;
int ledPin = 11;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
digitalWrite(ledPin, buttonState);
}


In the loop(), an int variable named button state is declared and assigned the value of digitalRead(button in). If the button is not pressed, the voltage at the button it will be low, and digitalRead() will return a low value, which is stored in the button state. If the button is pressed, the voltage at the button in will be high, resulting in a high value being stored in the button state.

FLOATING PINS

You'll notice that something strange is happening if you build the project mentioned above and test it. When you put your hand near the button, the LED is likely to turn on and off. What might be the root of that?

The digital pins on the Arduino are incredibly delicate. The Arduino can detect even faint electromagnetic fields produced by your hand. And the digitalRead() method records those as high signals.


GPIO pins are referred to as floating pins when they are permitted to detect errant electromagnetic fields. Making ensuring the button in stays low when the button is not pressed will help us to fix the issue. So how can we go about doing that?

The simplest method is to connect a resistor from the push button's left side to the ground as shown in the example below:

Stray electromagnetic energy will pass through the resistor and into the ground when the button is not pressed. When the button is touched, the resistor prevents electricity from flowing to the ground and instead directs it to pin 7. Because it links a pin to the ground to maintain low voltage, this is known as a pull-down resistor. The pull-down resistor's value might vary, however, it is often greater than 10K Ohms.

PULL UP RESISTORS

Pull-up resistors are less common than pull-down resistors. Pull-up resistors are wired directly to a voltage source and retain the prong in a high-voltage state.


In this circuit, the pull-up resistor is connected to 5 volts, and the right side of the button is connected to the ground. Pressing the button will send a low signal to pin 7, turning the LED on. The pull up resistor is tied to 5 volts and keeps pin 7 high until the button is pressed.


Supposedly, the code for using a pull-up resistor looks like this:
int buttonPin = 7;
int ledPin = 11;
void setup()
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
}
if (buttonState == HIGH) {
digitalWrite(ledPin, LOW);
}
}

We declare variables for the buttonPin and ledPin variables and set them as outputs, the same as in the previous program. The voltage status of the button pin is then determined using the digitalRead() function, and it is then saved in the button state variable.

When the button state variable is low, we want the Arduino to send a high signal to the ledPin. Two if statements are used in the loop() section to determine what happens when the buttonState variable is high or low. The first if statement is carried out and the ledPin is written high if the buttonState variable is low. The software enters the second if statement and writes a low voltage to the ledPin if the buttonState variable is high.

The LED should now come on when you press the button and stop flickering as you move your hand across the circuit. The floating pin issue has been resolved.

ARDUINO’S INTERNAL PULLUP RESISTOR

The pull up and pull down resistors that we examined in this article are components of an external circuit. But you can accomplish the same thing with the inbuilt pull-up resistor that the Arduino contains. Use INPUT PULLUP as the second argument in the pinMode() function for the buttonPin to use the inbuilt pull-up resistor built into the Arduino
int buttonPin = 7;
int ledPin = 11; void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == LOW) { digitalWrite(ledPin, HIGH); } if (buttonState == HIGH) { digitalWrite(ledPin, LOW); } }

Now that the pull-up resistor is connected to 5 volts, the circuit can be wired. The internal pull-up resistor reduces the number of components and simplifies the design.

I hope this post has given you a better understanding of the various uses for a push button when using Arduino to control gadgets. If you have any questions, make sure to leave a comment below!






Tutorials World

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply