Tutorial 3: Digital Inputs

Receiving inputs is just as important as creating outputs. In this tutorial I show you how to receive and read a digital input from a push button.

DIFFICULTY
EASY
CIRCUITRY KNOWLEDGE
LITTLE
C++ PROGRAMMING
LITTLE
ABOUT
0
MINUTES
  • How to build a simple button circuit
  • How to use the digitalRead Arduino command
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!


int buttonPin = 8 ;
int LEDPin = 13 ;
bool buttonPress ;

void setup() {
  pinMode(LEDPin, OUTPUT) ;
  pinMode(buttonPin, INPUT) ;
}

void loop() {
  buttonPress = digitalRead(buttonPin) ;

  if (buttonPress) {
    digitalWrite(LEDPin, HIGH) ;
    delay(1000) ;
  }
  else {
    digitalWrite(LEDPin, LOW) ;
  }
  
}