Explanation of program Controlling LEDs with pushbutton

                The first time it is run, the first LED will be extinguished. when we press the pushbutton, the LED will light up. LED will return extinguished when the pressure button is released. In Sketch Controlling LEDs with pushbutton above there are several lines of new code:

   pinMode (pinButton, INPUT);
   pinMode (pinLED, OUTPUT);
  
   // Turn on pull-up resistor
   digitalWrite (pinButton, HIGH);

                Line 6 serves to set pinButton as INPUT. If a pin is set as INPUT, the microcontroller will take data from the pin. If a pin is set as OUTPUT, the microcontroller will write data at the pin. in this case, the microcontroller will take data from the pushbutton.
Led circuit Using the Push Button

Led circuit Using the Push Button


               Note the line 5th. PinButton value initially set HIGH. Why set HIGH? Why could set the value of the pin becomes HIGH, when the pin is not connected to + 5V?

               You see, the selection of the initial setting to HIGH or LOW for pinButton depending on the circuit that will be used. The circuit connecting pinButton previous post (pin 8) to GND, that is, when the pushbutton is pressed it pinButton (pin 8) will be 0 (LOW). Yet default, each pin is worth LOW. If the pin was originally worth LOW, then pressed still worth LOW, then what is the point pushbutton?

               In fact, the main function of the switch (in this case is a pushbutton) is changing the initial value becomes HIGH LOW, or vice versa. Well, since when the pushbutton is pressed will be worth LOW (to GND), then we should initially be set to HIGH. So the logic of the pushbutton is: when not pressed HIGH, LOW when pressed.

               The conditions that will be used to detect whether the pushbutton is pressed or not. Please note the line 18th.

 if(digitalRead(pinButton) == LOW){ 
 digitalWrite(pinLED, HIGH);
  }else{
    digitalWrite(pinLED, LOW);
  } 


               At line 18, the function digitalRead () to read the logic on pinButton. If pinButton pressed (LOW), then turn on the LED with the command digitalWrite (pinLED, HIGH). When pinButton worth HIGH, turn off the LED. Simple is not it?

               For the second question, why INPUT pin can be set HIGH?


               When we become INPUT pin as HIGH, then internally Arduino will connect the pin on the pull-up resistor is worth 20k ohms. What's pull-up resistor? If there is a pull-up, whether there is also a pull-down resistor?

Scheme pull-up resistor

Scheme pull-up resistor


               Well, in digital electronics, if a pin is set as INPUT, then the pin is not connected to VCC or GND, the logic pin is still (floating). Therefore, it must be determined whether the pin will be given a pull-up resistor (so valuable HIGH) or by pull-down (so it is worth LOW).

               If the pin is set HIGH, the internal microcontroller pins are to be connected to VCC with a resistor safety termed the pull-up resistor. Likewise, if the pin is set LOW, then the pin will be connected to GND with a safety resistor is then termed a pull-down resistor. Pictured above is a basic series pull-up resistor to a string above without LED.

Subscribe to receive free email updates:

0 Response to "Explanation of program Controlling LEDs with pushbutton"

Post a Comment