Arduino Nano - Rocker Switch Module

This tutorial instructs you how to use Arduino Nano to read the DIYables ON/OFF Rocker Switch Module. It is the same style of switch you see on extension leads and small appliances, but it sits on a small blue board with three labeled pins. Once you flip it, it stays there. So the Nano never waits for a press: it simply checks whether the switch is ON or OFF right now. In the last example, the switch becomes the arm switch of a tiny alarm box. In detail, we will learn:

Arduino Nano rocker switch module - arm and disarm an alarm box

We suggest:

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
1×ON/OFF Rocker Switch Module
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of ON/OFF Rocker Switch Module

Look at the module from the top and you see a black KCD1 rocker switch on a square blue board (about 24 x 24mm). Every corner has a hole, so you can screw the module to the lid of a box. The three header pins are bent at 90 degrees and point sideways. On the rocker, "I" stands for ON and "O" stands for OFF. The print beside it, "ON (H)" and "OFF (L)", tells the code what to expect: H for HIGH, L for LOW.

This is a maintained switch (also called latching). Tip it to "I" and it stays at "I" until you tip it back. A push button works the other way: it is only ON while your finger holds it. That is why there is no press to catch here. The Nano just asks for the current position, and it acts when the answer is not the same as last time.

Inside, it is a simple SPST switch (one contact that is open or closed). When the switch is closed, the SIG pin is joined to VCC. When it is open, a 10kΩ pull-down resistor on the board (marked "103") pulls SIG down to 0V. Thanks to that resistor, the input never floats, and you do not need any extra resistor.

ON/OFF Rocker Switch Module
Switch KCD1 rocker, SPST, maintained (stays in place)
Rocker marks I = ON, O = OFF
Operating voltage 3.3V to 5V
Output on SIG HIGH (the VCC voltage) when ON, LOW (0V) when OFF
Resistor on board 10kΩ pull-down on the SIG line
Header 3 pins, 2.54mm pitch, right-angle, male
Size about 24 x 29 x 22mm with the pins
Pack 5 modules

What about the "6A 250VAC / 10A 125VAC" text on the switch? It belongs to the switch part alone. The module uses that switch for one small job: giving a logic level to a Nano pin. Keep mains wires and motor currents far away from its header.

Rocker switch module pinout - SIG, VCC and GND pins next to the I/O rocker
image source: diyables.io

Turn the module over to find the pin labels, right beside the header:

  • SIG pin: must be connected to D2 (or another free digital pin) of Arduino Nano. It is HIGH while the rocker is on "I" and LOW while it is on "O"
  • VCC pin: must be connected to the 3V3 pin of Arduino Nano (the 5V pin also works). When the switch is ON, SIG takes this voltage
  • GND pin: must be connected to GND (0V)

※ NOTE THAT:

Other makers may put these three pins in a different order. Always follow the labels printed on your own module, not the order in a photo.

Wiring Diagram

In the diagram, the VCC wire of the module goes to the 3V3 pin of the Nano. The Nano reads 3.3V as HIGH, so 3V3 is enough. If that pin is already busy, use the 5V pin instead. SIG goes to D2, and both sketches of this tutorial read that pin.

The wiring diagram between Arduino Nano and rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Arduino Nano
SIG D2
VCC 3V3 (or 5V)
GND GND

A screw terminal keeps each wire tight, even when the board shakes. A Screw Terminal Expansion Board for Arduino Nano solves this. Plug the Arduino Nano into it and screw the wires in. This is how it looks with the screw terminal board:

The wiring diagram between Arduino Nano and rocker switch module Screw Terminal

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

How To Program For Rocker Switch Module

You do not need a library. Think of the switch as a state that you check, not as an event that you wait for.

  • The first step is to give a name to the Arduino Nano pin that receives the SIG signal:
#define ROCKER_SIG_PIN 2
  • Set that pin to INPUT mode in setup() with pinMode(). Skip INPUT_PULLUP: the module already has its pull-down resistor, and the internal pull-up would work against it.
pinMode(ROCKER_SIG_PIN, INPUT);
  • Ask for the rocker position with digitalRead(). It returns HIGH for "I" (ON) and LOW for "O" (OFF):
int sig = digitalRead(ROCKER_SIG_PIN);
  • To do something only at the moment of a flip, store the last confirmed state and compare each new reading with it. The metal contacts bounce for a few milliseconds when you flip the rocker. So trust a new level only when it has not changed for about 50 milliseconds.

Arduino Nano Code for Rocker Switch Module - Read ON/OFF

This first sketch is a simple wiring test. It reads SIG every 400 milliseconds and prints the raw value (1 or 0) next to the word ON or OFF. A switch that stays in place does not need anything faster, and no debounce is needed at this slow speed.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rocker-switch-module */ #define ROCKER_SIG_PIN 2 // Arduino Nano pin D2, wired to the SIG pin of the rocker switch module // The setup function runs once on reset or power-up void setup() { Serial.begin(9600); // open the link to the Serial Monitor at 9600 baud pinMode(ROCKER_SIG_PIN, INPUT); // plain INPUT: the 10k pull-down on the module keeps SIG at 0V when OFF } // The loop function repeats indefinitely void loop() { int sig = digitalRead(ROCKER_SIG_PIN); // 1 (HIGH) = rocker on "I", 0 (LOW) = rocker on "O" Serial.print("SIG = "); Serial.print(sig); if (sig == HIGH) Serial.println(" -> rocker switch is ON"); else Serial.println(" -> rocker switch is OFF"); delay(400); // the switch stays where you put it, so a few readings per second are enough }

Detailed Instructions

  • If this is the first time you use Arduino Nano, see how to set up the environment for Arduino Nano in Arduino IDE.
  • Wire the components as shown in the diagram.
  • Connect the Arduino Nano board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • In the Tools menu, set the board to Arduino Nano and pick its COM port. Many Nano clones need ATmega328P (Old Bootloader) under Processor if the upload fails.
  • Copy the code above into a blank sketch.
  • Click Upload and wait for "Done uploading".
  • Open the Serial Monitor and pick 9600 baud in its speed list.
  • Start with the rocker on "O". After a moment, press the "I" side, wait a second or two, then press the "O" side again.
  • Each line shows the level on D2 and what it means:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
SIG = 0 -> rocker switch is OFF SIG = 0 -> rocker switch is OFF SIG = 0 -> rocker switch is OFF SIG = 1 -> rocker switch is ON SIG = 1 -> rocker switch is ON SIG = 1 -> rocker switch is ON SIG = 1 -> rocker switch is ON SIG = 1 -> rocker switch is ON SIG = 0 -> rocker switch is OFF SIG = 0 -> rocker switch is OFF
Ln 11, Col 1
Arduino Nano on COM15
2

Do you see only 0, even on "I"? Then the module gets no power: check that VCC goes to the 3V3 or 5V pin. If the value jumps around at random, the SIG wire is probably not in D2.

Arduino Nano Code for Rocker Switch Module - Arm and Disarm an Alarm Box

Many alarm boxes have one switch on the side. ON means "watch the room". OFF means "I am home, stay quiet". This sketch turns the rocker module into that arm switch:

  • Rocker on "I": the Serial Monitor prints "System ARMED", and the small "L" LED of the Nano (LED_BUILTIN, pin D13) blinks fast, five times per second.
  • Rocker on "O": it prints "System DISARMED" and the LED stays off. It also tells you how long the box was armed.

Each flip prints one line only. A new level must stay steady for 50 milliseconds before the sketch accepts it, so contact bounce cannot print extra lines. Both the debounce and the blinking use millis() instead of delay(), like in Arduino Nano - LED - Blink Without Delay. The loop never stops, so the Nano sees a flip at once, even in the middle of a blink.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rocker-switch-module */ #define ROCKER_SIG_PIN 2 // Arduino Nano pin D2, wired to the SIG pin of the rocker switch module #define STATUS_LED LED_BUILTIN // the small "L" LED on the Nano board (pin D13) #define DEBOUNCE_TIME 50 // a new switch level must stay the same this long (ms) before we trust it #define BLINK_INTERVAL 100 // LED on/off time while ARMED (ms): 5 fast blinks per second int armedState; // confirmed switch level: HIGH = ARMED, LOW = DISARMED int lastReading; // raw level seen in the previous loop unsigned long lastChangeTime = 0; // when the raw level last changed (for the debounce) unsigned long lastBlinkTime = 0; // when the LED was last toggled unsigned long armedSince = 0; // when the system was armed bool wasArmed = false; // true after the box was armed at least once int ledState = LOW; // Print the alarm state; called at start-up and on every real flip of the switch void printState() { if (armedState == HIGH) { Serial.println("System ARMED"); } else { Serial.print("System DISARMED"); if (wasArmed) { // tell how long the box was armed Serial.print(" - it was armed for "); Serial.print((millis() - armedSince) / 1000); Serial.print(" seconds"); } Serial.println(); } } // The setup function runs once on reset or power-up void setup() { Serial.begin(9600); pinMode(ROCKER_SIG_PIN, INPUT); // plain INPUT: the module has its own pull-down resistor pinMode(STATUS_LED, OUTPUT); armedState = digitalRead(ROCKER_SIG_PIN); // the switch may already be ON at power-up lastReading = armedState; if (armedState == HIGH) { armedSince = millis(); wasArmed = true; } Serial.println("Alarm box ready"); printState(); } // The loop function repeats indefinitely void loop() { int reading = digitalRead(ROCKER_SIG_PIN); // 1. Restart the debounce timer every time the raw level moves (contact bounce included) if (reading != lastReading) { lastChangeTime = millis(); lastReading = reading; } // 2. The level stayed the same for DEBOUNCE_TIME and differs from the confirmed state: a real flip if ((millis() - lastChangeTime) >= DEBOUNCE_TIME && reading != armedState) { armedState = reading; if (armedState == HIGH) { armedSince = millis(); wasArmed = true; } printState(); // one line per flip, never a flood } // 3. Drive the LED with millis(), so the loop never stops if (armedState == HIGH) { if (millis() - lastBlinkTime >= BLINK_INTERVAL) { lastBlinkTime = millis(); ledState = (ledState == LOW) ? HIGH : LOW; // toggle the LED digitalWrite(STATUS_LED, ledState); } } else { ledState = LOW; digitalWrite(STATUS_LED, LOW); // disarmed: the LED stays dark } }

Detailed Instructions

  • Leave the wiring as it is. The LED is already on the Nano board.
  • Paste this sketch into Arduino IDE in place of the first one, then click Upload.
  • Set the rocker to "O" and open the Serial Monitor (9600 baud).
  • Flip the rocker to "I": the "L" LED starts to flash quickly. Wait a few seconds and flip it back to "O": the LED goes dark.
  • Do it once more and keep it armed a bit longer. The output looks like this:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
Alarm box ready System DISARMED System ARMED System DISARMED - it was armed for 9 seconds System ARMED System DISARMED - it was armed for 31 seconds
Ln 11, Col 1
Arduino Nano on COM15
2

If the rocker is already on "I" when the Nano starts, the first state line says "System ARMED" and the LED blinks right away.

Additional Knowledge

Rocker Switch Module vs Bare ON/OFF Switch

Rocker Switch Module Bare ON/OFF Switch
Pins 3, labeled SIG, VCC, GND 2 legs, no labels
Resistor 10kΩ pull-down already on the board none, so use the internal pull-up
pinMode() setting INPUT INPUT_PULLUP
Level when the switch is ON HIGH LOW (upside down)
How it is connected jumper wires on a 2.54mm header wires soldered to the legs
How it is mounted screws through the 4 corner holes pressed into a cut-out in a panel

It is evident that the module is the faster way to get started: no soldering, labeled pins, and ON reads as HIGH. The bare ON/OFF switch is still the better pick when the switch must sit in the wall of a box.

Project Ideas

  • A door alarm: add a door sensor and a piezo buzzer to the arm/disarm sketch, and let the buzzer sound only while the system is armed
  • A service switch that pauses a pump or a fan while you work on it
  • A unit switch that shows a temperature in °C on one side and in °F on the other
  • A quiet-hours switch that turns off all the beeps of a clock project at night

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!