Input Devices

So far, you have been making the Aruduino control LEDs based on a programmed sequence and time delays. It would be more interesting and useful if you could make the Arduino change things while it is running, without having to change the sketch. You can. In this section you will learn how to add input devices such as buttons and knobs. You will also learn some new code that lets the Arduino choose between different alternatives.

Switch Circuit

The basic circuit for input devices is a connection between the +5V power pin and one of the ground (GND = 0V) pins. *** This connection MUST include a resistor. *** Another wire will then connect back to a digital or analog input pin on the Arduino. The switch will then control whether this third wire is more closely connect to the +5V (HIGH) or the ground (LOW). When the switch is closed, the input will be HIGH, when the switch is open, the input will be LOW.

Switch Circuit

Switch Code

To make the Arduino pay attention to this switch, we need the following code:

       /*Switch Detection
        Mr. Taylor
        2015-11-11  */
            
        int switchPin=8; //The pin that the input is connected to
        
        void setup() {
            pinMode(switchPin, INPUT);
        }
        
        void loop() {
            if (digitalRead(switchPin)==HIGH) {
                do some commands
            }
            else {
                do some different commands
            }
        } //end of loop function
        

There are three new items of code in this example:

  1. In the setup function, we are making the pinMode an INPUT pin. This is very similar to what we have done before. All the the digital pins along the top of the Arduino board can be used for either OUTPUT or INPUT - we just have to tell the Arduino which is which.
  2. In the loop, to get information about what the input pin is connected to, we have to do a digitalRead operation. The result of this will be either HIGH or LOW. This is sometimes call a "binary digit" with the values 1 (HIGH) or 0 (LOW). It is also known as a boolean, with values True or False.
  3. The "if...else" statement is the decision making part of the code. This kind of statement always starts with the keyword "if", followed by a pair of round brackets containing a conditional statement. You've probably seen this kind of thind in Math class like "x>y", "a<=b", "s=t". The crazy difference is that in computer languages "=" is used to give variables their values, so when we want "=" in a conditional statement, we have to use "==". Missing this double "==" is one of the most common mistakes that computer programmers make! Finally, after closing the round brackets, we have to have a pair of curly brackets enclosing a set of commands that you want to occur if the condition is true. Later, and optionally, you may include the keyword "else" and a set of curly brackets that you want to occur if the condition is false.

Switch Example

Here's a complete circuit and sketch that makes a pushbutton switch control an LED via the Arduino. This could actually be built without using the Arduino at all, but by using the Arduino, we start to see how the code works and by playing with the code, we can make switches control behaviours that are impossible any other way.

Switch Circuit with LED

To make the Arduino pay attention to this switch, we need the following code:

       /*Switch Controlled LED
        Mr. Taylor
        2015-11-11  */
            
        int switchPin=8; //The pin that the input is connected to
        int ledPin = 10; //The pin that the LED is connected to
        
        void setup() {
            pinMode(switchPin, INPUT);
            pinMode(ledPin, OUTPUT);
        }
        
        void loop() {
            if (digitalRead(switchPin)==HIGH) {
                digitalWrite(ledPin, HIGH);
            }
            else {
                digitalWrite(ledPin, LOW);
            }
        } //end of loop function
        

This example operates the LED in the normal way: when you press the button switch, the LED turns on, when you resease it, the LED turns off. Now play with the code. See if you can make it operate backwards (press turns off, release turns on.) Can you make the LED change from off to on when you click the switch (press and release) and change from on to off the next time you click the switch? Watch this video for ideas about how to do this:

Element 14 Lesson 2

Next assignment: Input devices - Pots

Back to the main menu.