Digital input with Arduino (Push button) – How does work push button?

Hello guys, Welcome back to my blog. I have described in previous articles how to get analog outputs. Do you remember it? We used analog pins for this. Okay, let’s go to the post today. Today we are going to talk about how to get digital input to the Arduino board. Digital pins can be used for this project. These pins are numbered D2 to D13. Do not use 1 or 2 pins for digital readings. These pins are used for Serial communication. We can get the analog reading value from 0 to 1024 but the digital reading value is 0 or 1. It’s a boolean value. We can get this value using various sensors or components connected to the Arduino board. We will build robots using these methods in the next articles.

Let’s do this project using the push button. This circuit can be done in two ways. It is PULL UP and PULL DOWN. These methods are described below. Let’s do this project step by step. The components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Arduino Nano

I used the Arduino Uno board in previous articles but I used the Arduino Nano board for this project. Because the Arduino Nano board is a very cheap price.

Push button

The push-button has four legs but this project uses two legs.

10k Resistor and 1k Resistor

This component is mainly used to control the current. According to this project, the 10k resistor is used to PULL UP and PULL DOWN. The 1k resistor is used to control the current.

Breadboard

The breadboard is very important for testing circuits.

LED bulb
Jumper wires

I have used five (male-to-male) jumper wires for this project.

Step 3

Thirdly, attach the push button and the Arduino Nano board to your breadboard.

Step 4

Ok, let’s test the PULL-UP and PULL-DOWN circuits.

PULL-UP

The D2 pin and GND pin on the Arduino board are connected to the push button. Then, the D2 pin is connected to a 5v supply through a 10K resistor from the connection point. In this case, the value is 0 when the push buttons are pressed. Otherwise, you can see the 1 on the serial monitor. You can watch this process with your serial monitor. For that, use the code below.

PULL-DOWN

In this case, the D2 pin and 5v pins of the Arduino board are connected to the push button. Then, the D2 pin is connected to a GND through a 10K resistor from the connection point. In this case, the value is 1 when the pushbutton is pressed. Otherwise, you may see 0 on the serial monitor. You can watch this process with your serial monitor. Use the following code for that.

Step 5

OK, now let’s see how to connect an LED bulb to the PULL-UP circuit. First, assemble this circuit.

For this, I have connected the LED bulb to the PULL-UP circuit itself using a 1k resistor. Use the Arduino D3 pin for that.

Step 6

Now, copy and paste the following program to the Arduino IDE.

  • The complete program of this project – Download
void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
}
void loop() {
  bool value = digitalRead(2);
  if (value == 0) {
    digitalWrite(3, HIGH);
  } else {
    digitalWrite(3, LOW);
  }
}

In the void setup, the D2 pin defines an input pin and the D3 pin defines the output pin. One by one the code parts of the void loop code are described below.

bool value =digitalRead(2);

This code is read to the digital value of the component connected to the D2 pin. Then it is put into the Boolean variable. Only 1 or 0 can be stored in the Boolean data type.

  if (value == 0) {
    digitalWrite(3, HIGH);
  } else {
    digitalWrite(3, LOW);
  }

This code checks that the Boolean variable is equal to 0. If it is equal to 0 then the code inside it is run. Then the LED bulbs light up. If the value of the variable is 1, the bulb will be switched off.

Step 7

OK, now select the board and port. After, click the upload button.

Now, you can check this project by pressing the push button. The full video guide is below. So, we hope to see you in the next project or tutorial. Have a good day.

Digital input with Arduino (Push button) – How does work push button.

Leave a Comment

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

Shopping Cart
Select your currency
USD United States (US) dollar
EUR Euro
Scroll to Top