Arduino Projects

Arduino Digital Input Code Example

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.

Arduino course

allpcb circuit

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:

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.

Arduino course



video tutorial

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button