Saturday, 22 October 2011

CIRC-07: Button Pressing

Purpose:
To turn a single LED on and off via a button press.

Equipment:

  • 1x Arduino Uno
  • 1x Breadboard
  • 1x 330 Ohm Resistor
  • 1x 10k Ohm Resistor
  • 1x Pushbutton
  • 1x LED
  • 5x Wires
Reference:

Program Details:
The one new piece of hardware this program has introduced is the Pushbutton. This button is a user controlled device. Our push button model works by completing a connection when pressed, and can complete up to two separate circuits.

In terms of programming, this is the firs time we have had Arduino take in information. Every experiment before this involved setting the pins to Output, but this time we are taking in an input. This is done by the method pinMode(pin #, INPUT). Since we are taking in an input, we will be using digitalRead, rather than digitalWirte. digitalRead(pin #) checks whether or not it is receiving any electricity. For this method, it creates a value of HIGH if there is electricity present, and LOW if there is not.

To put this circuit together we first create an interdependent circuit of the LED. This circuit looks just like the one from Experiment 1. It is a very simple circuit, as it receives its power from Pin 13, and the other end is grounded with a 330 Ohm resistor in the middle of the (-) end and ground.

Another circuit is also created for the pushbutton. 5V travels trough a 10k Ohm resistor to ground. But there is a pushbutton in the middle of this, so the circuit isn't always complete.The resistor is there because it is possible for the voltage to float when the button isn't pressed.  Pin 2 is connected to this circuit as well, and it will only receive voltage if the button is not pressed. Pin 2 checks whether or not it is reviving voltage, and without the resistor it is possible that Pin 2 will continue to receive voltage even though the circuit is grounded.


Time to complete:
10 minutes to build, 10 minutes to code.

Results:
The first time we uploaded the code, the LED turned on even though the button was not pressed. This was due to a programming error, as we accidentally flipped the behavior of Pin 13. After we flipped it back around it worked as intended.

Photos:




Tips:
Make sure that when you press the button that the circuit is actually completed. It is easy to miss align the pins on the pushbutton.

Further Work:
You can reverse the behavior of Pin 13. Doing this means that it would turn off when the button is pressed, and off when it is not. This can be done in the loop() by swapping the two digitalWrites. Change the LOW to HIGH, and vice versa.

Program Modifications:
The program looks just like the one from:
http://www.oomlout.com/a/products/ardx/circ-07

Program:


int ledPin = 13;                //Pin 13 controls LED
int inputPin = 2;                //Pin 2checks the button
int val = 0;                       // Stores pins status (set to LOW)

void setup
{
    pinMode(ledPin, OUTPUT);   //sets Pin 13 to output
    pinMode(inputPin, INPUT);    //sets Pin 2 to input
}

void loop()
{
    val = digitalRead(inputPin)      //Stores the state of the button

    if (val == HIGH){                  //When the button is not pressed
        digitalWrite(ledPin, LOW);    //LED does not receive electricity
 
    }else{                                   //When the button is pressed (else because there are only 2 options)
        digitalWrite(ledPin, HIGH);    //LED receives electricity
    }
}


No comments:

Post a Comment