按鈕




  • Arduino UNO
  • 按鈕
  • LED
  • 10KΩ電阻

按下按鈕時,點亮腳位13的LED燈

將腳位2設定為輸入,此時腳位2讀取到的是低電位(LOW),按下按鈕後,電流留入腳位2,產生高電位(HIGH

int pushButton = 2 ;
int ledPin = 13 ;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600) ;
  pinMode(pushButton,INPUT) ;
  pinMode(ledPin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  boolean buttonState = digitalRead(pushButton) ;
  if( buttonState) {
       digitalWrite(ledPin,HIGH) ;
    }else{
       digitalWrite(ledPin,LOW);
    }
      delay(1) ;
}