Arduino Mega - Rocker Switch Module
An Arduino Mega often runs the long jobs: a weather station that writes a reading every few seconds, or a test bench that records data all night. Sooner or later, you want to stop the log for a moment - to move a sensor, change a battery, or just read the screen in peace - without pulling out the USB cable. A rocker switch on the front of the box is the classic answer. The DIYables ON/OFF Rocker Switch Module gives you one that comes ready-made on a little PCB, with printed pin names. This tutorial instructs you how to use the Arduino Mega to read the rocker switch module and let it start and stop a simple logger. In detail, we will learn:
- The parts of the rocker switch module, and the job of its SIG, VCC and GND pins
- How to wire SIG, VCC and GND to the Arduino Mega, with the signal on pin 7
- How to program the Arduino Mega to print the rocker position on the Serial Monitor, both in words and as a 1 or 0
- How to program the Arduino Mega to write an uptime log line every 2 seconds while the rocker is ON, and to pause and resume that log each time you flip it

We suggest:
- Following the Arduino Mega - Switch tutorial when you only have the loose 2-leg switch, without the blue board.
- Following the Arduino Mega - Button Module tutorial if you need a short press, like START or NEXT, instead of a position that lasts.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Rocker Switch Module and Its Pinout

If you have ever turned on a power strip, you have already used a rocker switch. In this DIYables module, that kind of switch - a black KCD1 rocker - is mounted on a square blue PCB, roughly 24mm on each side. Four corner holes let you bolt the module behind a panel. Along one edge, a 3-pin header with right-angle pins points sideways for the jumper wires.
Two symbols are printed on the rocker itself: "I" (ON) and "O" (OFF). The text "ON (H)" and "OFF (L)" next to it on the PCB adds the electrical meaning - in other words, the Mega sees HIGH when the switch is ON and LOW when it is OFF.
The most important thing about this switch is that it remembers. Whichever side you push down, that side holds - for a minute or for a year - until somebody pushes the other side. People call this a maintained (or latching) switch. Compare it with a push button, which gives ON just as long as your finger stays on it. For that reason, the rocker code never waits for a click. It looks at the present position, and it only acts when that position differs from the last one.
Electrically, it is an SPST switch (single pole, single throw): one contact, closed or open. Closed, it links SIG straight to VCC. Open, it leaves SIG to a 10 kOhm pull-down resistor on the module (the tiny part marked "103"), which drags the line to 0V. Pin 7 of the Mega therefore never floats, and no extra resistor goes into your wiring.
| ON/OFF Rocker Switch Module | |
|---|---|
| Rocker | KCD1 type, black, marked I and O |
| Contact Type | SPST (single pole, single throw) |
| Action | maintained (latching) - holds "I" or "O" |
| Power (VCC) | 3.3V to 5V |
| Output on SIG | VCC level when ON, 0V when OFF |
| Pull-down | 10 kOhm on SIG, printed "103" |
| Pins | 3-pin male header, right-angle, 2.54mm pitch |
| Board | about 24 x 24mm with 4 mounting holes |
| Size with Header | about 24 x 29 x 22mm |
| Package | 5 modules |
Do not let the "6A 250VAC / 10A 125VAC" stamp fool you. It belongs to the switch as a separate part. On this module, the job is much smaller: sending a logic level to an input pin. Keep mains wiring and motor current away from its header.
Flip the module over and you find the three pin names, printed beside the header:
- SIG pin: must be connected to one of the digital pins of the Arduino Mega - this tutorial uses pin 7. With "I" down, SIG is at the VCC voltage (HIGH). With "O" down, SIG is at 0V (LOW).
- VCC pin: must be connected to 3.3V or 5V on the Mega. Whichever you choose also becomes the HIGH voltage on SIG.
- GND pin: must be connected to one of the GND pins (0V).
※ NOTE THAT:
The pin order is not the same on every rocker module on the market. Before you plug in the jumper wires, turn the board over and match each wire to its printed name.
Wiring Diagram
Only three wires run between the module and the Mega: one for power, one for ground and one for the signal. The diagram feeds VCC from the 3.3V pin and sends SIG to digital pin 7.

This image is created using Fritzing. Click to enlarge image
| Rocker Switch Module | Arduino Mega |
|---|---|
| GND | GND |
| VCC | 3.3V (or 5V) |
| SIG | Pin 7 |
※ NOTE THAT:
- The Arduino Mega works with 5V logic, so you may also move the VCC wire to the 5V pin. SIG then gives 5V instead of 3.3V when the switch is ON. The Mega reads both as HIGH.
- Pin 7 is only an example. The long double header of the Mega (pins 22 to 53) has plenty of free inputs. If you move the SIG wire, update ROCKER_SIG_PIN in the sketch as well.
How To Program For Rocker Switch Module
Only built-in calls are used, so no library has to be installed.
- First, give pin 7 a name that says what is wired to it. Should the SIG wire move to another pin later, this is the only line to edit:
- Inside setup(), set that pin up with pinMode(). Choose INPUT, not INPUT_PULLUP. While the rocker rests on "O", the 10 kOhm resistor keeps SIG at a clean 0V by itself:
- The logger must also catch the moment of a flip. A moving rocker does not give one clean edge: for a few milliseconds the contacts bounce, and one flip may look like three or four. The cure is a short waiting rule. Each time the raw level moves, note the time. Only when the level has not moved for 50 milliseconds does it count as the new state. millis() keeps the time, and nothing in the loop has to pause:
Arduino Mega Code for Rocker Switch Module - Wiring Test with the Raw Pin Value
Before you build the logger, check the wiring with a short test. Every 300 milliseconds, this sketch reads pin 7 and writes one line: the position in words (ON or OFF) next to the number from digitalRead() (1 or 0). At this slow pace, no debounce is needed, because any contact bounce has died away long before the next reading.
Detailed Instructions
- New to the Arduino Mega? Install the Arduino IDE on your computer first. Support for the Mega is built in, so you do not need an extra board package.
- Plug the three jumper wires in as shown in the wiring diagram.
- Use the USB cable to connect the Arduino Mega to your computer.
- Start the Arduino IDE.
- In the Tools menu, choose Arduino Mega or Mega 2560 as the board, then choose the COM port of the Mega.
- Paste the code above into an empty sketch window.
- Click the Upload button and wait for the upload to finish.
- Open the Serial Monitor and choose 9600 baud in its baud list.
- Start with "O" pressed down. After about a second, rock the switch to "I", hold on for two seconds, then go back to "O".
Nothing changes until you rock the switch to the other side. Until then, the same line keeps coming back - the rocker simply stays put.
Does the line say OFF no matter which side is down? Then the VCC wire is usually missing or on the wrong pin. If ON and OFF swap by themselves while nobody touches the switch, pin 7 is floating: push the SIG wire firmly into pin 7 and onto the SIG pin of the module.
Arduino Mega Code for Rocker Switch Module - Logging Switch with Uptime Stamps
Here the rocker earns its place on the front panel. In this sketch, it is the logging switch of a small logger:
- Rocker on "I": the Mega prints a log line every 2 seconds. Each line starts with its uptime, for example [00:01:24]. The uptime is the time since the board started. The sketch works it out from millis() and shows it as hours, minutes and seconds. The "L" LED on the board (LED_BUILTIN, pin 13) stays lit, like the REC lamp of a recorder.
- Flip to "O": the Mega prints "Logging paused" one time, turns the LED off and stays quiet.
- Flip back to "I": it prints "Logging resumed" and goes on with the next entry number.
In this demo, each log line only carries an entry number. In a real project, you would put a sensor value there. The flip is checked with the 50 ms millis() rule from above, so one flip never prints two messages. And because the loop never calls delay(), a flip is noticed within a few milliseconds - the Mega does not have to finish a 2-second wait first. The Arduino Mega - LED - Blink Without Delay tutorial explains this millis() style of timing in more detail.
Detailed Instructions
- No new wires are needed: the "L" LED is part of the Mega board.
- Paste this sketch over the old one in the Arduino IDE, then click Upload.
- Set the rocker to "I", then bring up the Serial Monitor at 9600 baud. Missing the first lines? Tap RESET on the Mega once.
- Let it log for about ten seconds. Flip the rocker to "O" for a while, then flip it back to "I".
Look at the time after the pause: it jumps from 00:00:12 to 00:00:33. The clock shows how long the Mega has been running, so it keeps counting while the log is paused. The entry number only grows while logging is ON.
If the rocker is on "O" when the Mega starts, the second line says that the log is paused, and the first flip to "I" prints "Logging started" instead of "Logging resumed".
Additional Knowledge
Which ON/OFF Input Fits Your Mega Project?
The Arduino Mega tutorials cover three inputs that look alike. This table helps you pick the right one:
| Bare 2-Leg ON/OFF Switch | Rocker Switch Module | Button Module | |
|---|---|---|---|
| Memory of position | yes, latching | yes, latching | no, momentary |
| How it connects | 2 soldered legs | 3-pin labeled header | 3-pin labeled header |
| Input mode in the code | INPUT_PULLUP | INPUT | INPUT |
| Reading in the ON position | LOW (upside down) | HIGH | HIGH while held |
| Where the resistor comes from | inside the ATmega2560 chip | 10 kOhm on the module | on the module |
| Best job | a power switch in the wall of a case | a mode that lasts, like log or pause | a short command, like start or next |
It is evident that a rocker module wins whenever a setting must last: no soldering, no resistor to add, and ON simply reads as HIGH. Pick the bare ON/OFF switch when it has to sit in the wall of a case, and the button module when the input is a quick press.
Why Only the Logger Debounces
The first sketch reads the pin once every 300 ms. Contact bounce dies away within a few milliseconds, so a reading almost never lands in the middle of it. And if one does, the next line, 300 ms later, is correct again. The logger is different: it prints a message at each change. Without the 50 ms rule, one flip could print "Logging paused", "Logging resumed" and "Logging paused" in a row.
Ideas for the Logger
- Save the lines on a memory card instead of the Serial Monitor - see Arduino Mega - Micro SD Card. Flip the rocker to "O" before you take the card out, so no line is being written at that moment.
- Replace the entry number with a real reading, for example a temperature, and change LOG_INTERVAL_MS to log more or less often.
- Use more than one rocker module on the same Mega: one for logging, one to mute a buzzer, one to choose between auto and manual mode. Each module only needs its own input pin.