How to connect a switch with Arduino
Circuit setup
Example Code for Arduino
/* switch and LED should be connected with pin 3 and 6 respectively. When switch is pressed, LED glows and when switch is released LED is switched off.
*/
const int buttonPin = 3; // Pin connected to the button
const int ledPin = 6; // Pin connected to LED
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set the button as output
}
void loop() {
if (digitalRead(buttonPin)) {
digitalWrite(ledPin,LOW);
} else {
digitalWrite(ledPin,HIGH);
}
delay(100);
}
No comments:
Post a Comment