Arduino UNO R4 - Rocker Switch Module

In this guide, we will learn how to use the DIYables ON/OFF rocker switch module with the Arduino UNO R4, and turn it into the run/stop switch of a tiny machine. Fans, drills and power strips often have this kind of switch on the front: press the "I" side and the device works, press the "O" side and it rests. Our machine is the Arduino UNO R4 itself. While the rocker is on "I", it runs a job that blinks the built-in LED and counts the cycles. On "O", it stops. In detail, we will learn:

Arduino UNO R4 with an ON/OFF rocker switch module used as a run/stop switch

Overview of Rocker Switch Module

The module is a small blue board, about 24 x 24mm, with four mounting holes. A black KCD1 rocker switch sits on top. One half of the rocker shows "I", the other half shows "O". Push the "I" half down and the switch is ON. Push the "O" half down and it is OFF. The text printed next to the switch says the same: "ON (H)" and "OFF (L)". A 3-pin right-angle male header (2.54mm pitch) sticks out at one edge, so the wires come out from the side.

The rocker is a maintained (latching) switch. Once you flip it, it stays there, even when nobody touches it. That is the big difference from a push button. With a button, the code waits for a press and a release. With this switch, the code reads a position, ON or OFF, and does something when that position changes.

Rocker switch module pinout - SIG, VCC and GND pins of the DIYables ON/OFF rocker switch module
image source: diyables.io

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

  • SIG pin: connect to a digital input pin of the Arduino UNO R4. It is HIGH (the VCC voltage) when the rocker is on "I", and LOW (0V) when it is on "O".
  • VCC pin: connect to 3.3V or 5V.
  • GND pin: connect to GND (0V).
ON/OFF Rocker Switch Module
Switch KCD1 rocker, ON/OFF, SPST
Action maintained (latching), it stays where you put it
Marks I = ON, O = OFF
Header 3 pins, 2.54mm pitch, right-angle (SIG, VCC, GND)
Operating voltage 3.3V to 5V
SIG when ON HIGH (same as VCC)
SIG when OFF LOW (0V)
On-board resistor 10 kOhm pull-down on SIG
Size with pins about 24 x 29 x 22mm

The pull-down resistor (marked "103") holds SIG at LOW while the switch is open, so the pin never floats. For this reason, the code uses plain INPUT mode. Do not turn on INPUT_PULLUP, and do not add a resistor of your own.

SIG gives out the same voltage that you feed to VCC. The input pins of the Arduino UNO R4 accept 5V, so VCC can go to the 3.3V pin or to the 5V pin. The wiring diagram below uses 3.3V.

You may see "6A 250VAC / 10A 125VAC" on the side of the switch. That is the rating of the bare switch part. On this module, the switch only sends a small logic signal to an input pin. Never run mains power or motor current through the header pins of the module.

Like all mechanical switches, the contacts bounce for a few milliseconds at each flip. A sketch that reads the switch a few times per second does not notice it. A sketch that reacts to each change should wait until the new level is stable, for example 50 ms. The second example below does this.

How the module compares with the plain 2-leg switch of the Arduino UNO R4 - Switch tutorial:

  • The plain switch needs INPUT_PULLUP and reads LOW when it is ON. The module reads HIGH when it is ON, with plain INPUT.
  • The module has its resistor on board and named pins, so you just plug in three wires.

If your project needs a key that springs back after each press instead, see Arduino UNO R4 - Button Module.

Wiring Diagram

Only three jumper wires are needed, and no breadboard. Connect SIG to digital pin 7, VCC to the 3.3V pin, and GND to one of the GND pins of the Arduino UNO R4.

The wiring diagram between Arduino UNO R4 rocker switch module

This image is created using Fritzing. Click to enlarge image

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

If you plan to keep this circuit, think about screw terminals. The Arduino UNO R4 works with both the Proto Screw Terminal Block Shield and the Screw Shield V2. Both turn the header pins into screw terminals. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

The wiring diagram between Arduino UNO R4 rocker switch module Screw Shield

This image is created using Fritzing. Click to enlarge image

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

How To Program For Rocker Switch Module

No library is used. A few built-in functions are enough.

  • Name the pin that SIG is wired to:
const int ROCKER_SIG_PIN = 7;
  • Make it a plain input, because the pull-down is already on the module:
pinMode(ROCKER_SIG_PIN, INPUT);
  • Read the position. HIGH means ON ("I"), LOW means OFF ("O"):
int sig = digitalRead(ROCKER_SIG_PIN);
  • To react to a flip, keep the last accepted state. When the reading is different, note the time with millis(), and accept the new state only after it has stayed the same for 50 ms. Nothing has to wait, so the rest of the code keeps running.

Arduino UNO R4 Code - Read the Rocker Switch Position

The first sketch is a quick test of the wiring. About two and a half times per second, it reads SIG and prints the raw value next to its meaning.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-rocker-switch-module */ const int ROCKER_SIG_PIN = 7; // Arduino UNO R4 pin connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); pinMode(ROCKER_SIG_PIN, INPUT); // plain INPUT: the 10k pull-down resistor is already on the module } void loop() { int sig = digitalRead(ROCKER_SIG_PIN); // HIGH when the rocker is pressed to "I", LOW when it is on "O" Serial.print("SIG = "); Serial.print(sig); if (sig == HIGH) Serial.println(" | rocker on I -> ON"); else Serial.println(" | rocker on O -> OFF"); delay(400); // a switch flipped by hand does not need more than a few readings per second }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Make the three connections shown in the wiring diagram.
  • Connect the Arduino UNO R4 to your computer with a USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino UNO R4 board (e.g., Arduino UNO R4 WiFi or Arduino UNO R4 Minima) and COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button.
  • Open the Serial Monitor and set it to 9600 baud.
  • Begin with the rocker on "O". After a few lines, press the "I" side down, wait about two seconds, and then go back to "O".
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
SIG = 0 | rocker on O -> OFF SIG = 0 | rocker on O -> OFF SIG = 0 | rocker on O -> OFF SIG = 0 | rocker on O -> OFF SIG = 1 | rocker on I -> ON SIG = 1 | rocker on I -> ON SIG = 1 | rocker on I -> ON SIG = 1 | rocker on I -> ON SIG = 1 | rocker on I -> ON SIG = 0 | rocker on O -> OFF SIG = 0 | rocker on O -> OFF
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

The same line keeps coming as long as the rocker stays put, because the switch holds its position by itself. The 400 ms pause also hides the contact bounce. If the value stays at 0 in both positions, check the VCC wire first: without power on VCC, the pull-down keeps SIG at LOW all the time.

This is good for a test, but a machine should act when the switch changes, not print the same line again and again. That is what the next sketch does.

Arduino UNO R4 Code - Run/Stop Switch for a Machine

Now the Arduino UNO R4 plays a small machine, and the rocker is its run/stop switch.

  • Rocker on "I": the job runs in cycles of one second. In each cycle, the built-in "L" LED is on for half a second and off for half a second, and the Serial Monitor prints "Running - cycle" with the cycle number.
  • Rocker on "O": the job stops at once. The LED goes off, and "Stopped" is printed one time.

How the sketch works:

  • Debounce: every pass of loop() reads SIG. If the reading changes, the sketch saves the time. The reading becomes the new switch state only after it has stayed the same for 50 ms. So the bounce of one flip cannot start and stop the job several times.
  • The job is timed with millis(), not delay(). The sketch checks the switch thousands of times per second, even while the LED blinks. This is why the job stops right away and not at the end of a cycle. If millis() is new to you, see Arduino UNO R4 - Blink LED Without Delay.
  • The cycle counter is not reset when the job stops. Like the counter of a real machine, it shows the total number of cycles since power-up.
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-rocker-switch-module */ // Run/stop switch for a small "machine": // rocker on "I" -> the job runs: the built-in LED blinks once per second and each cycle is counted // rocker on "O" -> the job stops: the LED is off and "Stopped" is printed one time const int ROCKER_SIG_PIN = 7; // Arduino UNO R4 pin connected to the SIG pin of the module const unsigned long DEBOUNCE_MS = 50; // a new switch level must stay this long before we accept it const unsigned long CYCLE_MS = 1000; // one job cycle = one blink = 1 second int switchState; // the debounced switch state: HIGH = ON, LOW = OFF int lastReading; // the raw reading of the previous pass of loop() unsigned long lastChangeTime = 0; // when the raw reading last changed unsigned long cycleStart = 0; // when the current cycle began unsigned long cycleCount = 0; // all cycles since power-up, like the counter of a real machine void beginCycle() { cycleStart = millis(); cycleCount++; digitalWrite(LED_BUILTIN, HIGH); // first half of the cycle: LED on Serial.print("Running - cycle "); Serial.println(cycleCount); } void stopJob() { digitalWrite(LED_BUILTIN, LOW); // the machine rests: LED off Serial.println("Stopped"); // printed only once per stop } void setup() { Serial.begin(9600); pinMode(ROCKER_SIG_PIN, INPUT); // plain INPUT: the module has its own pull-down resistor pinMode(LED_BUILTIN, OUTPUT); // the "L" LED on the board shows that the job is running switchState = digitalRead(ROCKER_SIG_PIN); // where is the rocker at power-up? lastReading = switchState; if (switchState == HIGH) beginCycle(); else stopJob(); } void loop() { // 1. Debounce the switch without delay() int reading = digitalRead(ROCKER_SIG_PIN); if (reading != lastReading) { // the level moved: a real flip or a contact bounce lastChangeTime = millis(); // start the 50 ms check again lastReading = reading; } if (reading != switchState && millis() - lastChangeTime >= DEBOUNCE_MS) { switchState = reading; // the new level held for 50 ms, so the flip is real if (switchState == HIGH) beginCycle(); // switch turned ON: start the job at once else stopJob(); // switch turned OFF: stop the job at once } // 2. Run the job while the switch is ON (no delay(), so the switch is checked all the time) if (switchState == HIGH) { unsigned long elapsed = millis() - cycleStart; if (elapsed >= CYCLE_MS) beginCycle(); // one second is over: next cycle else if (elapsed >= CYCLE_MS / 2) digitalWrite(LED_BUILTIN, LOW); // second half of the cycle: LED off } }

Detailed Instructions

  • Keep the same wiring.
  • Paste this code into a new sketch and click the Upload button.
  • Open the Serial Monitor at 9600 baud, and keep an eye on the "L" LED of the board. If you do not see the first "Stopped" line, press the RESET button of the board once.
  • With the rocker on "O", flip it to "I" and let the job run for about four seconds. Flip it back to "O". Then run it again for about two seconds and stop it.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Stopped Running - cycle 1 Running - cycle 2 Running - cycle 3 Running - cycle 4 Stopped Running - cycle 5 Running - cycle 6 Stopped
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

The second run continues from cycle 5, because the counter keeps the total. If you want every run to start again from cycle 1, add the line cycleCount = 0; inside the stopJob() function.

To give the job real work, put your own code in beginCycle(). For example, take one sensor reading per cycle, or move a motor one step.

Project Ideas

More ways to use the rocker switch module with the Arduino UNO R4:

  • A master switch for a small robot: the motors may move only while the rocker is on "I".
  • A data logger switch: the Arduino UNO R4 WiFi sends sensor values to the cloud only while the switch is ON.
  • A "machine status" display: show RUN or STOP on the built-in 12x8 LED matrix of the Arduino UNO R4 WiFi.
  • An enable switch for a timer: a countdown runs only while the switch is ON and pauses when it is OFF.

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!