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.

Potentiometer (Pot) Circuit

The basic circuit for input devices is a connection between the +5V power pin and one of the ground (GND = 0V) pins. A potentiometer is a variable resistor with a rotating knob that controls the amount of resistance. The one in your kit has three wires: red, black and yellow. The red wire should be connected to the +5V and the black wire should be connected to the ground GND. There is a pretty high resistance between these two wires (about 1,000 Ohms) so this is a safe connection. The yellow wire slides along the resistor in between and can vary between having no resistance with the red connection but 1,000 Ohms with the black connection, or 1,000 Ohms of resistance with the red connection and no resistance with the black connection. Do not connect the yellow wire to +5V nor to GND. Connect the yellow wire to one of the ANALOG IN pins at the bottom of the Arduino board (labelled A0 to A5.)

Pot Circuit

Pot Code

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

       /*Pot Detection
        Mr. Taylor
        2015-11-11  */
        
        int potPin = 0;  //Input pin is A0    
        int potValue;  //This variable will hold the value of the input
        
        void setup() {
            //The analog pins are always for input, so no need to set them up that way
        }
        
        void loop() {
            potValue = analogRead(potPin);    // read the value from the input
            //The potValue will be somewhere between 0 and 1024
            if (potValue > 500) {
                do some commands
            }
            else {
                do some different commands
            }
        } //end of loop function
        

The only really new command here is analogRead. It gets a value from the specified pin. The value will be an integer between 0 and 1024. We can do many different things with these numbers. In this first example, I have shown an if statement checking to see if the value is greater than 500.

Pot Example

Here's a complete circuit and sketch that makes a potentiometer control the blinking of an LED. This is very similar to the first sketch you saw. The only difference is that the delay is not a fixed number, but a variable, and the value of that variable is changed by the pot input.

Pot Circuit with LED

       /*Pot Controlled LED
        Mr. Taylor
        2015-11-11  */
            
        int potPin = 0;  //Input pin is A0    
        int potValue;  //This variable will hold the value of the input
        int ledPin = 8; //The pin that the LED is connected to
        
        void setup() {
            pinMode(ledPin, OUTPUT);
        }
        
        void loop() {
            potValue = analogRead(potPin);     // read the value from the input
            digitalWrite(ledPin, HIGH);        //turn on the LED
            delay(potValue);                   //delay time controlled by pot
            digitalWrite(ledPin, LOW);         //turn off the LED
            delay(potValue);                   //delay time controlled by pot
        } //end of loop function
        

This example just keeps turning the LED on and off, but the delay time is the variable potValue, and each time around the loop, the Arduino reads the latest potValue from the analog input pin, so it could be anywhere from 0 to 1024 milliseconds. Play with this sketch and see if you can make the length of time the LED is on different from the time it is off, but both controlled by the pot. (Hint: the Arduino can do arithmetic to calculate a delay time.)

Next assignment: Output devices - Speakers

Back to the main menu.