Arduino - Rocker Switch Module

A rocker switch is the wide ON/OFF key you know from power strips: press the "I" side and it stays down until you press the "O" side. In this tutorial, we are going to learn:

Arduino Uno rocker switch module - print ON and OFF and detect each flip on Serial Monitor

About Rocker Switch Module

The module is a black KCD1 rocker switch soldered on a small blue board, about 24 x 24mm, with a mounting hole in each corner. A 3-pin right-angle header sticks out of one side, so the jumper wires plug in straight. The rocker has two marks: "I" means ON and "O" means OFF. Next to it, the board is printed with "ON (H)" and "OFF (L)", which tells you the level that Arduino will see.

This switch is latching (also called maintained). It does not spring back like a push button. Whatever side you press, it stays there, even with the power off. So the program does not wait for a "press". It reads a state, and the interesting moment is when that state changes.

Rocker Switch Module
Switch KCD1 rocker, ON/OFF, latching
Contact Type SPST (one circuit, open or closed)
Operating Voltage 3.3V to 5V
On-board Resistor 10 kOhm pull-down on SIG (marked "103")
Output Logic ON = HIGH, OFF = LOW
Header 3-pin, 2.54mm, right-angle
Board Size about 24 x 24mm, 4 mounting holes
Size with Pins about 24 x 29 x 22mm
Rocker switch module pinout - SIG, VCC and GND pins of the ON/OFF rocker switch board
image source: diyables.io

The pin names are printed on the back of the board, next to the header.

  • SIG pin: needs to be connected to a digital input pin of the Arduino Uno. It gives the VCC voltage (HIGH) when the rocker is on "I", and 0V (LOW) when it is on "O".
  • VCC pin: needs to be connected to the 3.3V or the 5V pin of the Arduino Uno.
  • GND pin: needs to be connected to GND (0V).

The switch body is printed with "6A 250VAC / 10A 125VAC". That is the rating of the bare switch alone. On this module, the switch only carries a tiny logic signal to Arduino. Never connect mains power or a motor to the header pins.

Maybe you have used the bare 2-leg switch from the Arduino - Switch tutorial. The module works in a simpler way:

Bare ON/OFF Switch Rocker Switch Module
Wires to Arduino 2 3
Resistor INPUT_PULLUP needed pull-down already on the board
Reading when ON LOW (inverted) HIGH (not inverted)
Pin Labels none SIG, VCC, GND

Wiring Diagram

The module plugs into Arduino with three jumper wires. No breadboard and no resistor are needed.

Arduino Uno Rocker Switch Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin Arduino Uno Pin
VCC 3.3V (or 5V)
SIG Pin 7
GND GND

Loose wires are a common cause of strange bugs. Put a Screw Shield V2 or a Proto Screw Terminal Block Shield on top of the Arduino Uno, then tighten each wire under a screw. This is how it looks with the Screw Shield V2:

Arduino Uno Rocker Switch Module Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The diagram uses the 3.3V pin for VCC. You can move that wire to 5V: SIG then goes up to 5V when ON, which is fine for the Arduino Uno. Pin 7 is also just a choice. Any free digital pin works if you change the number at the top of the code.

How To Program For Rocker Switch Module

  • Give the input pin a name, so you only change one line if you move the wire:
#define ROCKER_PIN 7
  • Make it a plain input with pinMode(). Do not use INPUT_PULLUP. The module already holds SIG at LOW when the switch is OFF.
pinMode(ROCKER_PIN, INPUT);
  • Read the state with digitalRead(). HIGH means the rocker is on "I":
if (digitalRead(ROCKER_PIN) == HIGH) { // the switch is ON }
  • To react to a flip, keep the last known state in a variable and compare each new reading with it. Use millis() to check that a new level stays the same for 50 ms before you accept it.

Arduino Code - Rocker Switch Module ON/OFF State

This first sketch is a wiring test. It prints the raw level of SIG (1 or 0) and the matching word, about two and a half times per second.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rocker-switch-module */ #define ROCKER_PIN 7 // Arduino pin connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); // start the Serial Monitor connection pinMode(ROCKER_PIN, INPUT); // plain INPUT: the module already has a pull-down resistor on SIG } void loop() { int level = digitalRead(ROCKER_PIN); // 1 (HIGH) = rocker on the "I" side, 0 (LOW) = on the "O" side Serial.print("SIG = "); Serial.print(level); if (level == HIGH) Serial.println(" -> switch is ON"); else Serial.println(" -> switch is OFF"); delay(400); // about 2.5 lines per second, easy to follow by eye }

Quick Steps

  • Connect the module to Arduino as in the wiring diagram.
  • Connect Arduino to PC via USB cable.
  • Open Arduino IDE, then choose the Arduino Uno board and its COM port.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor and set the baud rate to 9600.
  • Press the "I" side of the rocker, wait a moment, then press the "O" side.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
SIG = 0 -> switch is OFF SIG = 0 -> switch is OFF SIG = 0 -> switch is OFF SIG = 1 -> switch is ON SIG = 1 -> switch is ON SIG = 1 -> switch is ON SIG = 1 -> switch is ON SIG = 0 -> switch is OFF SIG = 0 -> switch is OFF
Ln 11, Col 1
Arduino Uno on COM15
2

The line repeats as long as the switch stays in one place. That is fine for a test, but most projects do not want it.

If you only ever see 0, turn the module over and read the labels: the wire on pin 7 must sit on SIG, and VCC must go to 3.3V or 5V.

Arduino Code - Detect When the Rocker Switch Is Flipped

Think of a rocker switch on a machine. It may stay ON for an hour. During that hour, the loop() runs many thousands of times. If your code starts a motor, sends a message or counts something every time it sees ON, it does that job again and again. In real projects, you usually want to act once: start when the switch turns ON, stop when it turns OFF. So the normal way to use a latching switch is to watch for the change, not the level.

This sketch does that:

  • In setup(), it reads where the rocker already is. A latching switch can be ON before Arduino even starts.
  • In loop(), it compares each reading with the last known position.
  • When the level changes, it does not believe it at once. The contacts bounce for a few milliseconds. The level must stay the same for 50 ms, measured with millis().
  • Only then it prints "Switch turned ON" or "Switch turned OFF", with the time in milliseconds.

Unlike delay(), millis() never stops the loop. So you can add other work to the same sketch later.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rocker-switch-module */ #define ROCKER_PIN 7 // Arduino pin connected to the SIG pin of the rocker switch module #define DEBOUNCE_TIME 50 // a new level must stay the same this long (ms) before we trust it int stableState; // the last confirmed (debounced) position of the switch int lastReading; // the raw level seen in the previous loop unsigned long lastChangeTime = 0; // when (in ms) the raw level last changed void setup() { Serial.begin(9600); pinMode(ROCKER_PIN, INPUT); // no INPUT_PULLUP: SIG is already pulled down on the module // a latching switch may already be ON at power-up, so read where it is now stableState = digitalRead(ROCKER_PIN); lastReading = stableState; Serial.print("Start position: "); Serial.println(stableState == HIGH ? "ON" : "OFF"); } void loop() { int reading = digitalRead(ROCKER_PIN); if (reading != lastReading) { // the raw level moved: a real flip or just a bounce lastChangeTime = millis(); // restart the quiet-time timer lastReading = reading; } // the level has been quiet for DEBOUNCE_TIME ms and differs from the known position if ((millis() - lastChangeTime) >= DEBOUNCE_TIME && reading != stableState) { stableState = reading; // accept the new position Serial.print("["); Serial.print(millis()); Serial.print(" ms] "); if (stableState == HIGH) Serial.println("Switch turned ON"); // put your "start" action here else Serial.println("Switch turned OFF"); // put your "stop" action here } // nothing above waits, so other code can run here at the same time }

Quick Steps

  • Keep the same wiring.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor (baud rate 9600).
  • Flip the switch ON, wait a few seconds, then OFF. Do it twice.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Start position: OFF [2871 ms] Switch turned ON [6034 ms] Switch turned OFF [9518 ms] Switch turned ON [11247 ms] Switch turned OFF
Ln 11, Col 1
Arduino Uno on COM15
2

The Serial Monitor now stays quiet while you leave the switch alone, no matter how long. Each flip gives exactly one line. To make the switch do something, put your own code in the place of the two Serial.println() lines.

For more about contact bounce, see Arduino - Button - Debounce. Need a key that springs back after each press? See Arduino - Button Module.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES