Skip to content

Instantly share code, notes, and snippets.

@s-estay
Last active April 9, 2017 12:18
Show Gist options
  • Save s-estay/0cae81e10bda1d6162c61cf03977eb2f to your computer and use it in GitHub Desktop.
Save s-estay/0cae81e10bda1d6162c61cf03977eb2f to your computer and use it in GitHub Desktop.
arduino basics: control LED using switch
// turn LED ON and OFF using switch
// we read the state of the switch through pin 7
// switch is connected between pin 7 and GND
// we use the microcontroller's internal 20k pullup resistor
// why do we use a pullup resistor? read here: https://learn.sparkfun.com/tutorials/pull-up-resistors
// you can choose between LED ON or LED OFF when switch is pressed
// pin 8 will source current to the LED
// refer to source_current.ino file to see how to connect the LED
// code executed on Arduino Leonardo
void setup(){
pinMode(7, INPUT_PULLUP); // internal 20k pullup resistor
pinMode(8, OUTPUT);
}
void loop(){
digitalWrite(8, !digitalRead(7)); // when switch is pressed LED turns ON
// digitalWrite(8, digitalRead(7)); // when switch is pressed LED turns OFF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment