Sunday, October 30, 2011

Button Pressing (CIRC-07)

PURPOSE:

The purpose of this circuit experiment is to turn a LED on and off via a pushbutton.

EQUIPMENT:
·         1 x Arduino Holder
·         1 x Arduino Uno
·         1 x Arduino Breadboard
·         1 x Breadboard Sheet (CIRC-07)
·         1 x 330 Ohm Resistor (Orange-orange-Brown)
·         1 x 10k Ohm Resistor (Brown-Black-Orange)
·         2 x Pushbutton
·         1 LED (Red or Yellow)
·         7 x Wire (any colour)

PROGRAM DETAILS:

The new component of hardware introduced in this program is called the Pushbutton which is a device controlled by the user. When pressed, the pushbutton completes a circuit, and it can complete up to two different circuits. It looks like a small black square with four silver leads out
at the bottom and a button on the top.

This is the first lab where the pinMode is set to INPUT. The pinMode(pin, INPUT) code line  declares pushbutton as input. Here, the Arduino takes in an input so we must use the digitalRead(pin) method, rather than digitalWirte(pin). The method digitalRead(pin) returns whether it is HIGH(pulled to +5volts) or LOW(pulled to ground).

Putting this circuit together and wiring up the pushbutton was fairly easy.  However, the component, pull up resistor can seem out of place at times. The pull up resistor is included in the circuit because the Arduino Uno doesn’t sense the same way humans do. Instead it requires checking the voltage on the pin and then it decides whether it is HIGH or LOW. The button is set up so that when pressed, the Arduino’s pin can be pulled to LOW. When unpressed, the voltage of the pin will float and this can cause occasional errors. To make the Arduino read the pin as HIGH when the pushbutton is unpressed, the pull up resistor was added.

TIME TO COMPLETE:

10 minutes to build the circuit, 7 minutes to code.

RESULTS:

In our first try, the LED was not turning on. The pushbutton is a small square which is why it’s very easy to attach it to the breadboard in the wrong way. Since we attached it in the wrong way, the LED didn’t blink so we gave it a 90 degree twist and then, it worked as it was supposed to.
PHOTOS OF PROJECT:


Final CIRC-07 Circuit
 TIPS:

The first tip I would give is when you press the pushbutton, make sure you press it hard or else the circuit won’t be complete. Another tip is that you should be carefully choosing the Ohm Resistors. Since there are two different types of resistor (10k and 330) involved in this circuit, you might confuse yourself and choose the wrong one.

FURTHER WORK:

To make the program better and a little more complicated, I can make one button turn the LED on and the other button will turn the LED off. This can be done by changing the code to:  

int ledPin = 13; // choose the pin for the LED
int inputPin1 = 3; // button 1
int inputPin2 = 2; // button 2
 
void setup() {
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin1, INPUT); // make button 1 an input
  pinMode(inputPin2, INPUT); // make button 2 an input
}
 
void loop(){
  if (digitalRead(inputPin1) == LOW) {
    digitalWrite(ledPin, LOW); // turn LED OFF
  } else if (digitalRead(inputPin2) == LOW) {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}

PROGRAM MODIFICATIONS:

My program looks same as the one from:http://www.oomlout.com/a/products/ardx/circ-07

PROGRAM (with comments):

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
    }
}

REFERENCE:

3.      http://www.0j0.org

No comments:

Post a Comment