Complete Guide How to Demonstrating a Digital Input using Arduino

In this project, I will show you Demonstrating a Digital Input using Arduino step by step complete process.  main Our aim in this project is to create a button that turns on an LED for half a second when pressed.


≡ Hardware requirement Demonstrating Digital Input Arduino :


Here’s what you’ll require to create this project:

  1. One push button One LED
  2. One 560 Ω resistor
  3. One 10 kΩ resistor
  4. One 100 nF capacitor Various connecting wires
  5. One breadboard
  6. Arduino and USB cable

≡ Schematic Demonstrating Digital Input Arduino


we build the circuit on the breadboard with the schematic shown in Figure 1.  How the 10 kΩ resistors attached within GND and digital pin seven. We call this a pull-down resistor because it pulls the voltage at the digital pin around to zero. Besides, by adding a 100 nF capacitor over the 10 kΩ resistors, we build a simple debounce circuit to help filter out the switch bounce.

If the button pressed, the digital pin goes immediately to high. Just when the button released, digital pin seven pulled down to GND via the 10 kΩ resistors, and the 100 nF capacitor creates a small delay. This completely covers up the bouncing pulses by slowing down the voltage falling to GND, thereby eliminating most of the false readings due to floating voltage and erratic button behavior.

Figure 1. Schematic

Because this is the first time you’re making a circuit with a schematic, follow these step-by-step instructions as you walk through the schematic; this should help you understand how the components connect:

  1. Insert the push button into the breadboard, as shown in Figure 2.
  2. Turn the breadboard 90 degrees counterclockwise and insert the 10 kΩ resistors, a short link wire, and the capacitor, as shown in Figure 3.
10 kΩ resistor, capacitor, and push button
Figure 2
Push button inserted into breadboard
Figure 3

3. Attach one wire from the Arduino 5 V pin to the leftmost vertical column on the breadboard and attach an extra wire from the Arduino GND pin to the vertical row to the right of the 5 V column, as shown in Figure 4.

The 5 V (red) and GND (black) wires
Figure 4. The 5 V (red) and GND (black) wires

4. Run a wire from Arduino digital pin 7 to the breadboard near the top-right edge of the button, as shown in Figure 5.

Connecting the button to the digital input
Figure 5 Connecting the button to the digital input

5. Insert the LED into the breadboard with the short leg (the cathode) joined to the GND column, and the long leg (the anode) in a row to the right. Next, connect the 560 Ω resistor to the right of the LED, as shown in Figure 6.

Inserting the LED and 560 Ω resistor
Figure 6. Inserting the LED and 560 Ω resistor

6.Connect a wire from the right side of the 560 Ω resistor to Arduino digital pin 12, as shown in Figure 7.

Connecting the LED branch to the Arduino
Figure 4-25. Connecting the LED branch to the Arduino

Before continuing, review the schematic for this circuit and verify that your components wired correctly. Examine the schematic on the actual wiring of the circuit.


≡ Sketch Demonstrating Digital Input Arduino


1 #define LED    12
  #define BUTTON 7

  void setup()
  {
2   pinMode(LED, OUTPUT);   // output for the LED
    pinMode(BUTTON, INPUT); // input for the button
  }

  void loop()
  {
    if ( digitalRead(BUTTON) == HIGH )
    {
      digitalWrite(LED, HIGH);   // turn on the LED
      delay(500);                // wait for 0.5 seconds
      digitalWrite(LED, LOW);    // turn off the LED
    }
  }

After you’ve uploaded your sketch, tap the push button briefly and your LED should stay on for half a second.

Modifying Your Sketch

Once you’ve had some success, try modifying your sketch by changing the length of time that the light stays on or by adding a push-button control to Demonstrating a Digital Input.


≡ Understanding the Sketch


Let’s review the new items in the sketch for Demonstrating a Digital Input—specifically, #define, digital input pins, and the if-then function.

Creating Constants with #define

Before, we work #define statements at 1 to create fixed variables: When the sketch is selected, the IDE replaces any instance of the specified word with the number that follows it. For example, when the IDE sees LED in the line at 2, it replaces it with the number 12.

We’re basically using the #define command to label the digital pins for the LED and button in the sketch. Also notice that we do not apply a semicolon after a #define value.

It’s a good plan to label pin numbers and other fixed values (such as a time delay) in this way, because if the value done repeatedly the sketch, then you won’t have to edit the same item more than once. In this example, LED is used three times the sketch; to edit this value, we’d only  have to change its meaning once in its #define statement.

Reading Digital Input Pins

To read the state of a button, we first set a digital I/O pin as an input in void setup() using the following:

pinMode(BUTTON, INPUT); // input for button

Next, to see whether the button is connecting a voltage through to the digital input (that is, it’s being pressed), we use digitalRead(pin), where pin is the digital pin number to read. The function repeats either HIGH (voltage is close to 5 V at the pin) or LOW (voltage is close to 0 V at the pin).

Using if, we can make choices in our sketch and tell the Arduino to run different code, depending on the choice . For example, in the sketch for Project Demonstrating a Digital Input.

if (digitalRead(BUTTON) == HIGH)
{
    digitalWrite(LED, HIGH);    // turn on the LED
    delay(500);                 // wait for 0.5 seconds
    digitalWrite(LED, LOW);     // turn off the LED
}

The first line in the code begins with if tests for a state. If the condition is true (that is, the voltage is HIGH), then it means that the button is pressed and the code that follows inside the curly braces will run.

To decide whether the button is pressed (digitalRead(BUTTON) is set to HIGH), we use a comparison operator, a double equal sign (==). If we were to replace == with != (not equal to) in the sketch, then the LED would turn off when the button is pressed instead.

Making More Decisions with if-then-else

Using else forces the Arduino to run another section of code if the test in the if statement is not true.
#define LED    12
#define BUTTON 7

void setup()
{
  pinMode(LED, OUTPUT);   // output for the LED
  pinMode(BUTTON, INPUT); // input for the button
}

void loop()
{
  if ( digitalRead(BUTTON) == HIGH )
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
}

Boolean Variables

Sometimes you require to record whether something is in either of only two states, such as on or off, or hot or cold. A Boolean variable is the legendary computer “bit” whose value can be only a zero (0, false) or one (1, true). This is where the Boolean variable is helpful: It can only be true or false. Like any other variable, we require to declare it in order to use it:

boolean raining = true; // create the variable "raining" and first make it true

In the sketch, you can change the state of a Boolean with a simple reassignment, such as this:

raining = false;

It’s simple to utilize Boolean variables to make decisions utilizing an if test structure. Because Boolean comparisons can either be true or false, they work well with the comparison operators != and ==. Here’s an example:

if ( raining == true )
{
      if ( summer != true )
      {
            // it is raining and not summer
      }
}

Comparison Operators

We can utilize various operators to make decisions about two or more Boolean variables or other states. These include the operators not (!), and (&&), and or (||).

The not operator is denoted by the usage of an exclamation mark (!). This operator is used as an abbreviation for checking whether something is not true. Here’s an example:

if ( !raining  )
{
    // it is not raining (raining == false)
}

The and Operator

The logical and operator is indicated by &&. Using and helps reduce the number of separate if tests. Here’s an example:

if (( raining == true ) && ( !summer ))
{
    // it is raining and not summer (raining == true and summer == false)
}

The or Operator

The logical or operator is denoted by ||. Using or is very simple; here’s an example:

if (( raining == true ) || ( summer == true ))
{
    // it is either raining or summer
}

Making Two or More Comparisons

You can also use two or more comparisons in the same if. Here’s an example:

if ( snow == true && rain == true && !hot )
{
    // it is snowing and raining and not hot
}

And you can utilize parentheses to set the orders of operation. In the next example, the comparison in the parentheses is checked first, given a true or false state, and then compared with the rest in the if-then statement.

if (( snow == true || rain == true ) && hot == false))
{
    // it is either snowing or raining and not hot
}

Finally, just like the examples of the not (!) operator before a value, simple tests of true or false can be made without requiring == true or == false in each test. The following code works out the same as in the previous example:

if (( snow || rain ) && !hot )
{
    // it is either snowing or raining and not hot
    // ( snow is true OR rain is true ) AND it is not hot
}

All Arduino tutorial available Click here

 ARDUINO TUTORIAL 

 41 total views,  5 views today