ITP blog ยท physical-computing

Week 3 - Lab 2 - Digital Input and Output with an Arduino

25 September 2018

Lab 2 documentation

A simple circuit to flip the state of two LEDs when a pushbutton is pressed. One of the LEDs is on by default (before interaction), while the other one is off.

This circuit uses a 10M ohm pulldown resistor (sorry, forgot to label on the diagram) connected to the pushbutton switch and two 150 ohm resistors for the LEDs.

pushbutton-1 pushbutton-2

Code

void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  if (digitalRead(2) == HIGH) {
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  } else {
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
  }
}