Tuesday, October 18, 2011

Circuit-07 - Push Buttons

Purpose: To use a push button to complete a circuit.

Equipment:
  • Arduino x 1
  • Breadboard x 1
  • Arduino Holder x 1
  • CIRC-07 Breadboard Sheet x 1
  • Pushbutton x 2
  • 330 Ohm resistor x 1
  • 10k Ohm resistor x 2
  • LED x 1
  • Wire x 7

References: 
Program Details: 
Both the assembly of the circuit and the programming of the circuit are very simple in this exercise. In this circuit, we are basically controlling the LED staying on with the push button. When the push button is held down, the LED stays on, and when it is not pressed down, the LED stays off. This circuit should be easily built by following the schematic or the breadboard.

There is one new method introduced in this exercise, digitalRead().  What this method does is it reads the state of variable in the parameters and gives the output to another variable. In this example, buttonState = digitalRead(buttonPin); what this does is it reads the state (either HIGH or LOW) of buttonPin and assigns that read state as a value to the variable buttonState. Now in the program, according to the value of buttonState, the LED is either turned on or off.  

Time to complete: 5 mins to assemble
                               10 mins to program

Results: The first time we ran it, our push button did nothing. This was because we hadn't connected it to Pin2. The first button worked after that, but the second button still didn't do anything but this was because there was nothing about the second button in the program. The second button is used to turn off the LED in the second part of this circuit (if you decide to do it). 

Photo: 


Tips:
  • If the push button doesn't work, try flipping it around in 90 degree intervals
  • If the push button still doesn't work, try switching it with another one, the first one may have been nonfunctional
  • Don't forget to connect your resistors and wires 

Further Work: I will write the program to make the other button turn off the LED as well as make the LED's fade when they turn off and on.

Program Modifications:
The program is the same as the program in the link below:

Program:

// constants remain the same throughout the program
const int buttonPin = 2;      // pushbutton pin number
const int ledPin =  13;        // LED pin number

int buttonState = 0;           // buttonState 0 means it is currently doing nothing

void setup() 
{
  pinMode(ledPin, OUTPUT);     // initializes ledPin as an output   
  pinMode(buttonPin, INPUT);    // intializes buttonPin as an output    
}

void loop()
{
  buttonState = digitalRead(buttonPin);  // reads the state (HIGH or LOW) of buttonPin
  if (buttonState == HIGH)                   // if state is HIGH (meaning button is pressed) ...
  {     
    digitalWrite(ledPin, HIGH);             // turn on LED
  } 
  else                                                 // otherwise (meaning button is not pressed) ...
  {
    digitalWrite(ledPin, LOW);           // turn off LED
  }
}// end of loop




No comments:

Post a Comment