Example Code for Arduino-Button Test

Last revision 2026/01/24

This article offers a step-by-step guide to testing buttons with Arduino, including hardware and software preparation, wiring instructions, and sample code to control an LED with a button.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0350_Button_Test.png

Other Preparation Work

Open Arduino IDE, select Tools --> Board --> Intel Edison and Tools --> Serial Port --> COM xx.

Sample Code

Set D2 as input, D13 as output, button controls LED on/off.

int buttonPin = 2;     // Define Button Pin D2
int ledPin =  13;      // Define LED Pin D13

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);     // Define D13 Output Mode
  pinMode(buttonPin, INPUT);   // Define D2 Input Mode
}

void loop(){
  buttonState = digitalRead(buttonPin);  //Read D2 Status

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);   //Turn On LED
  }
  else {
    digitalWrite(ledPin, LOW);    //Turn Off LED
  }
}

Result

The LED turns on when the button is pressed and turns off when the button is released.

Was this article helpful?

TOP