Summary of MOTOR SWITCH LED Using Arduino
This article outlines a step-by-step method to connect and test an LED, switch, and motor with an Arduino. It emphasizes verifying each component individually before writing the main code. The guide provides specific wiring instructions for a membrane switch on Pin 3 and details how to drive a motor using a TIP120 transistor and an external battery rather than the Arduino's power pins.
Parts used in the Motor Switch LED Project:
- Arduino board
- LED
- Membrane switch
- Digital Multimeter (DMM)
- TIP120 transistor
- Separate 9V or 12V battery
- Gear motor or small motor
- 2N3904 transistor (for small motor)
- Solderless breadboard
When a project has a motor, a switch and an LED, wire and test each part on its own before writing the main program. Then, if something fails later, you know the hardware is fine. This page gives a tiny test sketch you drive from the Serial Monitor, the wiring for each part and a checklist for when a test fails.
- Send l for the LED test, s for the switch test and m for the motor test, at 9600 baud.
- The switch uses INPUT_PULLUP: the pin reads 1 when open and 0 when closed; a meter on the pin should show 5 V open and 0 V closed.
- The motor goes through a transistor, with a diode across it, never directly from a pin.
- The sketch compiles for the Uno (2798 bytes); we did not run it on hardware.

Wiring
| Part | Connection |
|---|---|
| LED | Pin 2 through 220 ohm to ground |
| Switch | Pin 3 to ground (internal pull-up used) |
| Motor | Pin 9 to 1 kilohm to an NPN transistor base; motor between the supply and the collector; diode across the motor; emitter to ground |
The test sketch
// Staged test of an LED, a switch and a motor, one at a time, from the Serial Monitor (9600 baud).
// Send l: LED on pin 2 for one second. s: print the switch on pin 3 for five seconds (1 open, 0 closed).
// m: run a motor on pin 9 through a transistor for two seconds at half speed.
// Motor circuit: pin 9 -> 1 kilohm -> base of an NPN transistor (or a logic level MOSFET gate), motor between supply and
// the collector, a diode across the motor, emitter to ground. Compiled only, not run on hardware.
const uint8_t LED_PIN = 2, SWITCH_PIN = 3, MOTOR_PIN = 9;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(MOTOR_PIN, OUTPUT);
Serial.println(F("Send l, s or m"));
}
void loop() {
if (!Serial.available()) return;
char c = Serial.read();
if (c == 'l') {
digitalWrite(LED_PIN, HIGH); delay(1000); digitalWrite(LED_PIN, LOW);
Serial.println(F("LED test done"));
} else if (c == 's') {
unsigned long start = millis();
while (millis() - start < 5000) {
Serial.println(digitalRead(SWITCH_PIN));
delay(250);
}
} else if (c == 'm') {
analogWrite(MOTOR_PIN, 128); delay(2000); analogWrite(MOTOR_PIN, 0);
Serial.println(F("Motor test done"));
}
}
How to run the tests
- Upload the sketch and open the Serial Monitor at 9600 baud.
- Send l: the LED should light for one second. If not, check polarity and the resistor.
- Send s: five seconds of 1s should change to 0s when you close the switch. If not, measure the pin voltage and the switch continuity.
- Send m: the motor should run at half speed for two seconds. If not, check the transistor pins, the supply and the diode direction.
If a test fails
| What you see | What to check |
|---|---|
| LED never lights | Reversed LED, wrong pin or a missing common ground. |
| Switch always reads 1 | The switch is not connected to ground, or a bad contact; try wiggling it. |
| Motor twitches | Supply too weak: use a separate supply with a shared ground. |
| Arduino resets when the motor starts | Supply sag: add a capacitor or a separate motor supply. |
Quick answers
More from this site
- TB6612FNG motor driver: a proper motor driver
- Switch with serial input: serial commands
- Quiz buzzer: buttons and LEDs
Downloads and references
- 💾 Arduino code (arduino-staged-test-code.zip)
staged_test.ino.
- How do I confirm the LED works?
Use the provided program to set pin 2 to HIGH for one second and then LOW. - What should happen when I close the switch?
The stream of 1s displayed on the monitor window should change to 0s. - Does the voltage at Pin 3 change based on the switch state?
Yes, it should read 5 V when the switch is open and 0 V when closed. - Can I use any pin for the switch connection?
Yes, although the code assumes Pin 3, any pin can be used. - Why should I not connect the motor directly to the Arduino?
You must use a transistor and a separate battery to control the motor safely. - Which transistor should I use for the small motor?
You can use the 2N3904 transistor as the switch for the small motor. - Where should the connections be made?
Connections should be made on a solderless breadboard rather than by soldering. - What is the best way to check if a switch contact is dirty?
Try wiggling the switch button while it is closed to see if continuity is restored.
