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
- Romeo for Intel® Edison Controller (Without Intel® Edison)(SKU: DFR0331) ×1
- Intel® Edison Module(SKU: DFR0335) ×1
- Digital Piranha LED light module(SKU: DFR0031-G) ×1
- Digital Push Button(SKU: DFR0029-G) ×1
- micro USB ×1
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE.
Wiring Diagram

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?
