# Digital Inputs

{% tabs %}
{% tab title="Part A - One Input" %}
In this lesson, you will learn to use push buttons with digital inputs to turn an LED on and off. Pressing the button nearer the top of the breadboard will turn the LED on, pressing the other button will turn the LED off. ![](/files/PNFzm6Ao6sbwTaleOqfy)

Parts:&#x20;

1 - 5mm red LED

1 - 220 ohm resistor

1 - push switches

1 – Assorted Wires &#x20;

1 - Breadboard

1 - Elego Uno R3 ![](/files/uqvtW8mGVFNnou8ivQZ4)

Wiring Diagram:

![](/files/unOSfQorXa8JFPtb0j1U)

Code:&#x20;

```cpp
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the LED pin


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input pullup:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (digitalRead(buttonPin) == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } 
  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
```

If done right, your LED should turn on when you press the button and it should turn off when you let go of it. &#x20;

After messing around with this one for a while, try the next  one too!
{% endtab %}

{% tab title="Part B - Multiple Inputs" %}
![Wiring Digram](/files/enY7FzHBO8B8SW1JllSE)

```
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(buttonApin) == LOW and digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }

  else if (digitalRead(buttonApin) == LOW or digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }

  else
  {
    digitalWrite(ledPin, LOW);
  }
}
```

Now your LED should flash when you hold down both buttons, stay of when you are holding down one button, and turn off if you let go of both buttons.&#x20;
{% endtab %}
{% endtabs %}

For an overview of several concepts used in these examples, check out the slideshow below.&#x20;

{% embed url="<https://docs.google.com/presentation/d/1rZR01dn5n1pfqQXfWNTrlvWXY9Jm3x00RqOMsNi4tEA/edit?usp=sharing>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guides.mccaskeyrobotics.org/arduino/digital-inputs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
