Arduino Digital Input Code Example
Table of Contents
Arduino Digital Input Code Example:
In this example, I am going to explain how to read a digital signal on any IO Pin on the Arduino. A digital signal may be 0 or 1 and you should be able to read both the types; because there are different types of sensors in the market. Some sensors give 1 at the output while others give 0. Anyway, you will get the idea as we cover different examples. For now let’s concentrate on how to read a button click.
Now, in this example, I am going to use Arduino Nano but you can also use Arduino Uno.
One side of the button is connected to the ground and the other side of the button is connected to the digital pin 2. On the Arduino we don’t need to add a pull-up resistor; because it already has, we only need to enable it; which I will explain in a minute. Anyway, this time I added an LED to the digital pin 5. The power supply on the left side is optional. If you are using your laptop or PC to power up the Arduino then there is no need for an external power supply. But, if you want to externally, power up your Arduino board then you can built yourself this basic 5V regulated power supply based on the 7805 voltage regulator. Let me tell you this not a powerful power supply, you can only use it with basic sensors and 5V relays. Anyway, let’s go ahead and take a look at the programming.
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Arduino Digital Input Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* * Arduino Example: Digital Input " Push Button" * https://www.electroniclinic.com/ */ int led = 5; //Output, an LED is connected to the digital pin 5. int button = 2; // Input, push button. void setup() { pinMode (led, OUTPUT); pinMode (button, INPUT_PULLUP); //internal pullup resistor is enabled. } void loop() { if(digitalRead(button) == LOW) { digitalWrite(led, HIGH); delay(1000); } if(digitalRead(button) == HIGH) { digitalWrite(led, LOW); delay(1000); } } |
I started off by defining the pins for the Led and Push button exactly the same way as previously explained.
In void setup() function I set the Led as output and I set the button as Input.
When connecting a sensor or a button to a pin configured with INPUT_PULLUP, the other end should be connected to the ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed. This is exactly what I did. The two legs of my push button are connected to the GND and pin 2 on the Arduino.
In the void loop() function, I have added two conditions to check if the button is pressed or the button is open. To read a digital signal on any IO pin on the Arduino we use the digitalRead function.
The first condition means, check if a low signal is available on the pin 2. So, if it reads a low signal then it means the button is pressed. And if the button is pressed then we tell the controller to turn ON the Led. And when the button is released or when the button is open; simply turn OFF the Led.
After uploading the program, you will be able to control the LED using a Pushbutton. For the step by step explanation and practical demonstration watch the video tutorial given at the end of this article.
video tutorial