Lesson 3: Working with Buttons

Now you can work with LED light, and know some features of Arduino and Breadboard. Let's add some interactivity to our lessons. Presenting you... BUTTONS

Here is how we place buttons on the Breadboard


After you place the button, you have to be sure that it holding tight in its place. You can make simple example like shown in Scheme, you connect one side of the button to the 5V and other side will be connected with 220 Ohm resistor, which connects led. LED finishes the connection with Ground. Without programming code you may see that LED will turn on if you press the button, and turn off when you release the button. By this actions you can understand that your Scheme is working.


Let's repeat previous example but it will work separately. We need to connect Button and LED to different input pins. Let it be 2nd pin for Button and 8th pin for LED.

Note: you'll need 10K Ohm resistor on one side of the button, connection of wires are made in cross way.


When you finish the scheme, it's time to write a code.

int button = 2;
int led = 8;

void setup() {
    pinMode(led, OUTPUT);
    pinMode(button, INPUT);  //instead of "INPUT" you may use "INPUT_PULLUP". Try, and say                                                      //what is the difference
}

void loop(){
    if (digitalRead(button) == HIGH) {
            digitalWrite(led, HIGH);
    }
    else {
            digitalWrite(led, LOW);
    }
}

SELF-STUDY: Use the same scheme. When you press Button once it will change the state of LED. If LED was turned off, it will turn on. If LED was turned on, it will turn it off. Hint: you have to make changes in programming code.

Comments

Popular posts from this blog

Lesson 9: Piezo Buzzer

Lesson 10: Piezo Buzzer controlled with Potentiometer

Lesson 6: Potentionmeter & LED