Skip to content

Latest commit

 

History

History

02

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Arduino controlling LED & Button

This example is functionally equivalent to the LED & Button example, however, the Arduino controls the behavior of the LED. This is the first time in this tutorial that a program (usually referred to as a sketch) is uploaded to the Arduino. This requires a so-called Integrated Development Environment, or IDE for short. While there are a variety of IDEs available to develop for the Arduino, the sketches part of this tutorial assume the standard Arduino IDE.

The button is connected to pin 2 and the LED is connected to pin 6 of the Arduino. The following sketch will light up the LED when the button is pressed:

const int PIN_LED = 6;
const int PIN_BUTTON = 2;

void setup() {
  pinMode(PIN_LED, OUTPUT);
  pinMode(PIN_BUTTON, INPUT_PULLUP);
}

void loop() {
  bool pressed = digitalRead(PIN_BUTTON) == LOW;
  digitalWrite(PIN_LED, pressed ? HIGH : LOW);
}

Function setup() defines pin 2 as input and pin 6 as output via function pinMode(). The constant INPUT_PULLUP will activate an internal pull-up resistor which is why the wiring below does not need a dedicated resistor connected to the button. Function loop() then uses functions digitalRead() to determine if the button is pressed or not and digitalWrite() to turn the LED off or on.

Tutorial

Tutorial