5 – Arduino digital inputs




    • Understand digital inputs.
    • Read data from a push-button switch.
    • Introduce the boolean data type.
    • An operator: logical NOT.


Bill of materials

Arduino Uno  Arduino Uno or equivalent. We can use any other Arduino board in this chapter.

We will also need a PC computer with the Arduino IDE properly installed and set up.

Breadboard A solderless Breadboard.
Red LED A LED diode.
Push-switch button  A push-button switch.
330 Ohm Resistor  Two 330 Ohm resistors.
Jumper wires Some jumper wires

Digital inputs


In electronics we often need to know whether a light is on or off, if somebody has pushed a button or if a door is open or closed.
The type of signals used to represent this kind of situations are called digital signals and they can only have two values:

all/nothing, yes/no, true/false, 0/1. In Arduino we can handle this type of signals using the pins from 0 to 13, that’s why we call them digital pins.

A lot of sensors and actuators in the real world are digital. Let’s see some examples:

  • As digital actuators we can mention lights, sirens, electronic door locks …
  • As digital sensors we can mention buttons, push-button switches, limit switches, level overflow sensors, flame sensors, smoke or toxic gas sensors…

We have seen that Arduino can use the digital pins as HIGH / LOW outputs to handle a LED diode. In the same way we can read HIGH/LOW values from the outside world.

In this chapter we will see that digital pins in Arduino can be used either as inputs or outputs. We are going to read data from a push-button switch to turn on or off a LED diode, depending on whether the button is pressed or not.



We will assemble a prototype with a LED diode and a resistor connected to the digital pin 10 of the Arduino, as we saw in previous chapters. We will later assemble a second prototype with a push-button switch connected to the pin 6 via a resistor, as is shown in the following circuit schematic diagram.

5 Fritzing diagram

Note that while we don’t press S1, the pin 6 of Arduino is connected to +5V via the R3 resistor setting it to a high level voltage, HIGH. However, when we press S1 we will close the circuit from pin 6 to ground setting it to a low level voltage, LOW. In both cases we have a defined voltage value in pin 6.

If we omit the R3 resistor, when we press S1 we will read correctly a low voltage level in pin 6. But when we release S1 the pin 6 would be in a floating state, neither HIGH or LOW but indefinite. As this is not acceptable, in digital circuits we force a high level voltage in pin 6 by using the R3 resistor.

  • This resistor, that provides a high value when there isn’t any inputs from the push-button switch, is known as a pull up resistor. If we connect it to ground to provide a low value to the digital pin, then we would call it a pull down resistor.
  • The resistor is vital for the readings of the push-button switch to be consistent. The circuit simply won’t work if it is omitted. We will go back to this

This is the circuit wiring diagram for the breadboard:

Chapter 5, Breadboard wiring diagram


  • In this circuit wiring diagram we have followed the usual practice of using black wires for grounding and red wires to connect voltage (+5V).
  • Notice that the S1 push-button switch (the one below the horizontal resistor), has four pins because each input has two pins connected. In our circuit we will simply ignore these secondary pins.



Let’s start making a program to turn on the LED diode when the push-button switch is pressed and off when it is released.
To do that, we tell Arduino to set up the digital pin 10 as an output to handle the LED and the digital pin 6 as an input to read the status of the push-button switch.

Normally when we write simple programs it is enough to type the pin number in the instructions. But as the program gets complicated this makes more difficult to detect bugs.

So it is good practice to define variables with the pin numbers we use, so we can modify them in one place (and not having to search through all the program). Let’s write an smarter sketch:

 int LED = 10 ;
 int button = 6;
 void setup()
    pinMode(LED,OUTPUT);        // LED as output 
    pinMode(button,INPUT) ;     // button as input
  • Watch out: C++ is case sensitive, that is, it distinguishes between uppercase and lowercase letters, so LED, Led or led represent three different variables. In the same way, pinMode is a correct keyword while pinmode will generate a devastating compiler error.

We saw that in order to turn on the LED it was enough to use the instruction digitalWrite( LED, HIGH). We can do something similar to read a button: digitalRead(button). Let’s see how could the loop() function be written:

     void loop()
              int value = digitalRead(button);    // Stores the button state in the variable value              

Easy, isn’t it? The LED is always on unless we press the push-button switch.

How could we do the opposite? In this case the LED should always be off unless we press the push-button switch. Then, we only need to write in the LED pin the opposite value to the reading from the push-button switch.

There is an operator that does exactly that, the logical NOT operator, “ ! “. If the value of a given variable, x, is HIGH, then !x is LOW and vice versa.

  • An operator is a symbol that binds several values together or modifies the value of a variable in a predictable way.
  • There are a several types of operators in C++, mathematical operators as +, -, * and /, others as the logical NOT operator and so on. We will go back to them in following chapters.

In fact, these kind of operations are so frequent that C++ provides a boolean data type that can only accept two values: logic true and logic false. These two values are completely equivalent to 1/0 or HIGH/LOW.

This new program could be written like this:

void loop()
   int valor = digitalRead(button);   // Stores the button state in the variable value
   digitalWrite(LED,!value);          // Stores the value negated in the variable LED 

We have defined the variable as boolean because we can assign TRUE to the high voltage value and FALSE to the low voltage value.
If we do not press the push-button switch, the pin 6 will read TRUE and so will turn off the LED. Otherwise it will turn the LED on.
In fact, we can write a curious variant of the blinking LED using the logical NOT operator:

Sketch 5.1
     void loop()
             bool value = digitalRead(LED);

  • We can read the current status of a pin even though we have not set it up as an output. However we can’t write any value in a pin set up as an input.

The first line reads the LED status and saves the value in a boolean variable. The second line writes its value negated in the pin associated to the LED. We can even do it using only two lines of code, beating a record.

Sketch 5.2
     void loop()
  • Instructions written between braces are executed before those written outside. That is why the digitalRead() instruction runs before digitalWrite().




    • We have seen a way to read and write digital signals from and to the outside world, respectively:
      • digitalRead(pin)
      • digitalWrite(pin, value)
    • We have known a new component: the push-button switch.
    • We know a new data type in C++, the boolean data type and also a new operator, the logical NOT.



No Comments

Give a Reply