How to wire a rocker switch to an Arduino?

Wiring a rocker switch to an Arduino is a fundamental skill for controlling your projects. There are two main ways to do it, depending on your goal:



  1. As a Digital Input (Read the Switch State): This is the most common use. You read whether the switch is ON or OFF to trigger actions in your code (e.g., turn on an LED, start a motor, change a mode).

  2. As a Power Switch (Control Arduino's Power): This is less common and physically cuts power to the entire Arduino board.

This guide will focus on the first and most useful method: using the switch as a digital input.


Method 1: Using the Switch as a Digital Input (Recommended)

In this setup, the switch doesn't power the Arduino on/off. Instead, the Arduino is always powered, and it constantly checks the state of the switch pin to decide what to do in its code.

What You'll Need

Step 1: Identify Your Switch Type

Rocker switches come in different types. The most common are:

  • SPST (Single-Pole, Single-Throw): Simple ON/OFF switch. It has two terminals. This is the easiest to use.

  • SPDT (Single-Pole, Double-Throw): ON-ON or ON-OFF-ON switch. It has three terminals. You can use it like an SPST.

This guide will cover wiring an SPST switch.

Step 2: The Circuit (Wiring Diagram)

The key concept here is to use a pull-down resistor. This resistor holds the Arduino's input pin at LOW (0V) when the switch is open (OFF). When you close the switch (ON), it connects the pin to HIGH (5V).

Wiring Instructions:

  1. Connect 5V to Switch: Run a wire from the 5V pin on the Arduino to one terminal of the switch.

  2. Connect Switch to Arduino Pin: Run a wire from the other terminal of the switch to the Arduino digital pin you want to use (e.g., pin 2).

  3. Add the Pull-Down Resistor: Connect a 10k Ohm resistor from that same digital pin (pin 2) down to GND.

  4. Complete the Circuit: Ensure your Arduino has a common ground.

How it works:

  • When the switch is OFF, the input pin (2) is connected to GND through the 10k resistor. The Arduino reads LOW.

  • When the switch is ON, the input pin (2) is connected directly to 5V. The Arduino reads HIGH. The 10k resistor prevents a short circuit between 5V and GND.

Step 3: The Arduino Code

Here is a simple sketch that reads the switch and turns the Arduino's built-in LED (on pin 13) on and off.

cpp
// Define the pin numbers for clarity
const int switchPin = 2;   // The pin connected to the switch
const int ledPin = 13;     // The built-in LED pin

// Variable to store the switch's state
int switchState = 0;

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
  
  // Initialize the switch pin as an input
  pinMode(switchPin, INPUT);
  
  // Start the serial communication (for debugging)
  Serial.begin(9600);
}

void loop() {
  // Read the value from the switch pin
  switchState = digitalRead(switchPin);
  
  // Print the state to the Serial Monitor
  Serial.println(switchState);
  
  // Check if the switch is ON (HIGH)
  if (switchState == HIGH) {
    // Turn the LED ON
    digitalWrite(ledPin, HIGH);
  } else {
    // Otherwise, turn the LED OFF
    digitalWrite(ledPin, LOW);
  }
  
  // A small delay to stabilize reading
  delay(10);
}

Upload this code to your Arduino. Open the Serial Monitor (Tools > Serial Monitor) to see the values change from 0 (OFF) to 1 (ON) as you flip the switch. The built-in LED should mirror the switch's state.


Method 2: Using a SPDT Switch (Optional)

If you have a 3-terminal SPDT switch, you can still wire it as a digital input. The center terminal is usually the common pin.

Wire it exactly like the SPST switch:

  • Connect 5V to one outer terminal.

  • Connect the center terminal to Arduino pin 2.

  • Connect a 10k resistor from the center terminal to GND.

  • You can leave the other outer terminal disconnected.

This configuration will only use one of the two "ON" positions.


Method 3: Using the Internal Pull-Up Resistor (Alternative Wiring)

Arduino microcontrollers have built-in pull-up resistors you can activate in code. This allows for a simpler wiring setup with no external resistor.

Wiring for Internal Pull-Up:

  • Connect one switch terminal to the Arduino digital pin (e.g., pin 2).

  • Connect the other switch terminal directly to GND.

Code for Internal Pull-Up:
The code logic is inverted! When the switch is OPEN, the pin reads HIGH. When the switch is CLOSED (pressed), it connects the pin to GND, reading LOW.

cpp
const int switchPin = 2;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP); // Activate the internal pull-up resistor
}

void loop() {
  // Note the logic is now inverted
  if (digitalRead(switchPin) == LOW) { // Switch is ON (pressed to GND)
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}

Summary Table

MethodWiringResistorSwitch ON reads as...Pros
External Pull-DownSwitch between 5V and Pin10k to GNDHIGHVery stable, easy to understand
Internal Pull-UpSwitch between GND and PinNone (internal)LOWFewer components, simpler wiring

For beginners, Method 1 (External Pull-Down) is highly recommended as it makes the code logic (HIGH = ON) very intuitive.

评论

此博客中的热门博文

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?

What is a Look-Up Table (LUT) in an FPGA, and how does it work?