Arduino MKR WiFi 1010 - Rocker Switch Module

Many machines hide a service switch inside the case. Before a technician opens the box, they flip it, and the machine knows that someone is working on it. On this page, the DIYables ON/OFF rocker switch module becomes that service switch for an Arduino MKR WiFi 1010. In this tutorial, we are going to learn:

Arduino MKR WiFi 1010 rocker switch module - ON/OFF maintenance mode switch on pin D2

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×ON/OFF Rocker Switch Module
1×Jumper Wires

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 Rocker Switch Module

Look at a power strip or at the back of a PC power supply, and you will probably find this switch: a black key shaped like a small seesaw, with "I" on one half and "O" on the other. The DIYables module puts that KCD1 rocker on a small blue PCB of about 24 x 24mm, with four screw holes. A right-angle header with three pins comes out of one edge, so the jumper wires plug in from the side. One pack holds five modules.

Press the "I" half down and the switch is ON. Press the "O" half down and it is OFF. Beside the rocker, the board is printed with "ON (H)" and "OFF (L)". The letter in brackets tells you what the MKR will read: H for HIGH, L for LOW.

The rocker is a maintained switch. It keeps its position when you take your finger away, and it still has that position after the power is cut. A push button, like the one in the Arduino MKR WiFi 1010 - Button tutorial, is only down while you hold it. This changes what the code looks for. A button sketch waits for a press. A rocker sketch asks "ON or OFF?" and does something when the answer changes.

Rocker Switch Module
Switch KCD1 rocker, two positions (I = ON, O = OFF)
Action maintained, SPST (one contact, open or closed)
Supply Voltage (VCC) 3.3V to 5V
Signal (SIG) HIGH when ON, LOW when OFF
Pull-down Resistor 10 kOhm on the board (marked "103")
Header 3 pins, 2.54mm pitch, right-angle male
Size board about 24 x 24mm, about 24 x 29 x 22mm with the pins

Pinout

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

The pin labels are on the back of the board, beside the header. Seen from the top with the header on the right, the pins are SIG, VCC and GND, from top to bottom.

  • SIG pin: needs to be connected to a digital pin of Arduino MKR WiFi 1010 (D2 on this page). It goes HIGH, to the same voltage as VCC, while the "I" side is down. It goes LOW (0V) while the "O" side is down.
  • VCC pin: needs to be connected to the VCC pin of Arduino MKR WiFi 1010. On this board, the pin named VCC is the 3.3V output.
  • GND pin: needs to be connected to a GND pin of Arduino MKR WiFi 1010 (0V).

When the rocker is on "O", the switch is open, and the 10 kOhm resistor on the board pulls SIG down to GND. D2 always sees a clear level, so no extra resistor goes into the circuit. In the code, set the pin as plain INPUT. INPUT_PULLUP is not needed here.

※ NOTE THAT:

SIG carries the same voltage as VCC, so VCC decides how high the signal goes. The SAMD21 chip on the MKR WiFi 1010 takes 3.3V at most on its pins. The 5V and VIN pins are only one or two pins away from VCC on the same header, so read the labels twice before you plug in the red wire. 5V on SIG can damage pin D2.

You may also read "6A 250VAC / 10A 125VAC" on the side of the rocker. These numbers are for the bare rocker as a part. On this module, it only passes a small 3.3V signal into a pin of the MKR. Keep mains wires and motor currents away from the three header pins.

Have you used a bare 2-leg ON/OFF switch before? That one needs INPUT_PULLUP in the code, and it reads LOW when it is ON, which feels upside down. The module reads HIGH when it is ON, has its resistor on the board and has named pins. For another switch that stays where you put it, see Arduino MKR WiFi 1010 - Slide Switch.

Wiring Diagram

Three wires join the module to the MKR, with no resistor in between: SIG to D2, VCC to VCC (3.3V) and GND to GND.

The wiring diagram between Arduino MKR WiFi 1010 Rocker Switch Module

This image is created using Fritzing. Click to enlarge image

Rocker Switch ModuleArduino MKR WiFi 1010
SIGD2
VCCVCC (3.3V)
GNDGND

The Micro USB cable powers the MKR, and the MKR powers the module through its VCC pin. D2 is only a choice. Another free digital pin also works if you change the number at the top of the code. Skip pin 6, because the built-in LED of the board is on pin 6, and the second sketch blinks it.

How To Program For Rocker Switch Module

  • Name the pin of the SIG wire: const int ROCKER_PIN = 2;
  • Make it a plain input in setup(): pinMode(ROCKER_PIN, INPUT);. The pull-down on the module holds the OFF level, so the internal pull-up stays off.
  • Read the rocker: int level = digitalRead(ROCKER_PIN);. HIGH means the "I" side is down (ON). LOW means the "O" side is down (OFF).
  • Catch a flip: keep the raw reading and the accepted state in two variables. Each time the raw reading changes, save millis(). When the raw reading has not moved for 50 ms and it is not the same as the accepted state, it becomes the new state.

Arduino Code - Read the Rocker Switch Module

Start with a simple test of the wiring. Every 300ms, the sketch reads D2 and prints the switch position next to the raw value.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-rocker-switch-module */ const int ROCKER_PIN = 2; // Arduino MKR WiFi 1010 pin D2 connected to the SIG pin of the rocker switch module void setup() { Serial.begin(9600); pinMode(ROCKER_PIN, INPUT); // plain INPUT: the 10k pull-down on the module keeps SIG LOW while the switch is OFF } void loop() { int level = digitalRead(ROCKER_PIN); // 1 = "I" side pressed down (ON), 0 = "O" side pressed down (OFF) if (level == HIGH) Serial.print("Switch ON | D2 = "); else Serial.print("Switch OFF | D2 = "); Serial.println(level); // the raw value: 1 or 0 delay(300); // about three lines per second; the bounce of a flip is over long before the next reading }

Detailed Instructions

  • Plug the Arduino MKR WiFi 1010 into your PC with the Micro USB cable. If this is your first MKR project, follow Getting Started with Arduino MKR WiFi 1010 to add the board to the Arduino IDE.
  • Wire the module as the diagram shows. Look at the VCC wire once more: it must end on VCC, not on 5V or VIN.
  • Navigate to the Arduino IDE. In the Tools menu, pick the Arduino MKR WiFi 1010 board and the port it uses.
  • Copy the above code into a new, empty sketch.
  • Click the Upload button and wait until the IDE says the upload is done.
How to upload Arduino MKR WiFi 1010 code on Arduino IDE
  • Open the Serial Monitor and set the baud rate to 9600.
  • With the rocker on "O", wait a second. Then press the "I" side, wait about two seconds, and press the "O" side again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
Switch OFF | D2 = 0 Switch OFF | D2 = 0 Switch OFF | D2 = 0 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch ON | D2 = 1 Switch OFF | D2 = 0 Switch OFF | D2 = 0
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

The line repeats for as long as the rocker stays where it is. This sketch does not care about contact bounce: the bounce lasts a few milliseconds, and the next reading comes 300ms later.

If the value stays at 0 even on "I", check that the D2 wire is on SIG and that the VCC wire is on the VCC pin of the MKR.

Arduino Code - Maintenance Mode Switch

Now the rocker gets a real job. Think of an MKR WiFi 1010 inside a weather station on a roof, or inside a vending machine. When a technician comes to work on it, they flip the rocker to "I". The board stops its normal work and shows that it is being serviced. When the work is done, they flip it back to "O". The sketch does this:

  • Switch ON: the built-in LED blinks slowly (1 second on, 1 second off), and the Serial Monitor shows "MAINTENANCE MODE" at once, then again every 3 seconds, with the seconds since the switch went ON.
  • Switch OFF: the LED goes dark, and "NORMAL MODE" is printed one time.
  • At power-up, the sketch reads where the rocker already is. A maintained switch can be left ON while the board is unplugged.
  • Debounce: a new level counts only after it stays the same for 50 ms, measured with millis(). Nothing in the sketch uses delay(), so the loop is free for the normal job of your project, for example the WiFi code.

The built-in LED is the small LED on the MKR board, wired to pin 6. The code uses its name, LED_BUILTIN, so no extra part is needed.

There is one more line to know: while (!Serial && millis() < 3000); in setup(). The MKR talks to the PC through the native USB port of its own chip. After a reset, the Serial Monitor needs a short time to connect again. This line waits up to 3 seconds for it, so the first mode line is not lost. Without a PC, the board simply goes on after 3 seconds.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-rocker-switch-module */ const int ROCKER_PIN = 2; // Arduino MKR WiFi 1010 pin D2 connected to the SIG pin of the rocker switch module const int SERVICE_LED_PIN = LED_BUILTIN; // the built-in LED of the MKR WiFi 1010 (pin 6) const unsigned long DEBOUNCE_TIME = 50; // a new switch level must stay the same this long (ms) const unsigned long BLINK_TIME = 1000; // slow blink: LED on for 1 s, off for 1 s const unsigned long REMINDER_TIME = 3000; // print "MAINTENANCE MODE" every 3 s int raw_level; // the last raw reading of D2 (may still bounce) int switch_state; // the accepted (debounced) state: HIGH = maintenance ON unsigned long raw_change_time = 0; // when the raw reading changed for the last time int led_state = LOW; unsigned long last_blink_time = 0; unsigned long last_reminder_time = 0; unsigned long maintenance_start = 0; // when the switch was turned ON void print_maintenance() { Serial.print("MAINTENANCE MODE ("); Serial.print((millis() - maintenance_start) / 1000); // seconds since the switch went ON Serial.println(" s)"); } void start_maintenance() { maintenance_start = millis(); last_blink_time = maintenance_start; last_reminder_time = maintenance_start; led_state = HIGH; // start the blink with the LED on digitalWrite(SERVICE_LED_PIN, led_state); print_maintenance(); // first message at once } void start_normal() { led_state = LOW; digitalWrite(SERVICE_LED_PIN, LOW); // LED off in normal mode Serial.println("NORMAL MODE"); // printed only once per flip } void setup() { Serial.begin(9600); while (!Serial && millis() < 3000); // native USB: give the Serial Monitor up to 3 seconds to connect pinMode(ROCKER_PIN, INPUT); // plain INPUT: the module has its own pull-down resistor pinMode(SERVICE_LED_PIN, OUTPUT); // the rocker keeps its position, so it may already be ON at power-up raw_level = digitalRead(ROCKER_PIN); switch_state = raw_level; if (switch_state == HIGH) start_maintenance(); else start_normal(); } void loop() { unsigned long now = millis(); // 1) debounce the switch without delay() int reading = digitalRead(ROCKER_PIN); if (reading != raw_level) { // a flip or a bounce raw_level = reading; raw_change_time = now; // start the 50 ms check again } if (raw_level != switch_state && now - raw_change_time >= DEBOUNCE_TIME) { switch_state = raw_level; // the level stayed the same for 50 ms: it is a real flip if (switch_state == HIGH) start_maintenance(); else start_normal(); } // 2) do the work of the current mode if (switch_state == HIGH) { // MAINTENANCE MODE: slow blink + a reminder line every 3 seconds if (now - last_blink_time >= BLINK_TIME) { last_blink_time = now; led_state = !led_state; digitalWrite(SERVICE_LED_PIN, led_state); } if (now - last_reminder_time >= REMINDER_TIME) { last_reminder_time = now; print_maintenance(); } } else { // NORMAL MODE: put the normal job of your project here (for example, sending data over WiFi) } }

Detailed Instructions

  • Keep the same wiring, and put the rocker on "O".
  • Replace the first sketch in the Arduino IDE with the code above.
  • Click the Upload button.
  • Open the Serial Monitor at 9600 baud.
  • Flip the rocker to "I" and watch the LED for about 7 seconds. Flip it to "O". Then do a short second round of about 4 seconds.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
NORMAL MODE MAINTENANCE MODE (0 s) MAINTENANCE MODE (3 s) MAINTENANCE MODE (6 s) NORMAL MODE MAINTENANCE MODE (0 s) MAINTENANCE MODE (3 s) NORMAL MODE
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

Each flip gives exactly one mode line, even though the contacts bounce. While the switch stays ON, only the reminder repeats, and the LED keeps its slow blink. If the first "NORMAL MODE" line is missing, the Serial Monitor connected after the 3 seconds. Flip the rocker, or press the RESET button of the MKR one time.

To use it in your own project, put the normal job in the empty else part at the end of loop(). It runs only while the switch is OFF, so it pauses by itself during maintenance. To learn more about contact bounce, see the Arduino MKR WiFi 1010 - Button - Debounce tutorial.

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!