Example Code for Arduino-LED Switch Control
Last revision 2025/12/08
Explore how to control an LED light using Arduino and a switch module, complete with a wiring diagram and sample code for beginners.
Hardware Preparation
- Arduino UNO × 1
- LED Switch Module × 1
- Digital Cable × 1
Software Preparation
Wiring Diagram

Sample Code
/*
Description:
When you press the button for the first time, its inner LED will be lighten up. At the same time, the LED on pin 13 on the board will be lighten up, too.
When you press the button again, its inner LED will be off, and the other one on pin 13 will be off as well.
*/
int ledPin = 13; // Select the pin of light
int inputPin = 2; // Sensor connect pin 2
void setup() {
pinMode(ledPin, OUTPUT); // Define the pin of light as output pin
pinMode(inputPin, INPUT); // Define the pin of button as input pin
}
void loop(){
int val = digitalRead(inputPin); //Read input value
if (val == HIGH) { // Check if the input is high, high means the button is pressed down.
digitalWrite(ledPin, HIGH); // LED light is on
} else {
digitalWrite(ledPin, LOW); // LED light is off
}
}
Result

Was this article helpful?
