Learn-Arduino-via-simulations

Using a push button to control an LED

Circuit

  1. Connect an LED to a digital pin of your Arduino UNO.
  2. Search for a push button and connect to the arduino as shown below: A push button has 4 terminals image Working:
    • When the button is unpressed, the side terminals are connected vertically(See above image)
    • When the button is pressed, all 4 terminals get interconnected.
    • Now, Focus on the right side of the above image-
    • In the off state, pin 3 of arduino is connected to the ground pin through green+black wire.
    • Whereas in the on state, current flows from 5V of arduino passing via red wire, crossing the switch, then to the pin3. Notice that due to high resistance connected on the other branch, current does not flow that side.

The conclusion is, the above circuit sends a LOW signal to pin3, when button is unpressed and a HIGH signal when the button is pressed.

About digitalRead() function: Remember that the digitalWrite function was used to send a HIGH or LOW signal to a digital pin. The digitalRead() function is its opposite. It receives(reads) whether the signal coming on a digital pin is HIGH or LOW.

Code:

int buttonState = 0;
int myLED = 13;
int button = 2;

void setup()
{
  pinMode(button, INPUT);
  pinMode(myLED, OUTPUT);
}

void loop()
{
  // read the state of the pushbutton value
  buttonState = digitalRead(button);
  // check if pushbutton is pressed.  if it is, the
  // buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(myLED, HIGH);
  } else {
    // turn LED off
    digitalWrite(myLED, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}