Arduino Nano 33 IoT - Rocker Switch Module

This tutorial instructs you how to use Arduino Nano 33 IoT with the DIYables ON/OFF Rocker Switch Module, a panel-style switch that clicks to one side and then stays there. Because it holds its position, it is a natural "pause" control. Think of a work timer on your desk: rocker on "I" and the seconds tick, rocker on "O" during a coffee break and the number waits for you. That is exactly what the second sketch of this page builds. In detail, we will learn:

Arduino Nano 33 IoT rocker switch module as the pause switch of a seconds counter

We suggest:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×ON/OFF Rocker Switch Module
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 Rocker Switch Module and Its Pinout

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

The heart of the module is a KCD1 rocker, a small black switch with a tilting top. Its contact closes when the "I" end is pushed down, and it opens when the "O" end is pushed down. The rocker is soldered onto a blue board of about 24 x 24 mm. A mounting hole sits in each corner, and a 3-pin header bent at a right angle comes out of one edge. DIYables sells the module in a pack of five.

Next to the rocker, the board print says "ON (H)" and "OFF (L)". This is a hint for your code: ON gives a HIGH level on SIG, and OFF gives a LOW level.

The rocker remembers its position. In switch words, it is a maintained (latching) SPST switch: one contact that is either open or closed. This changes the way you write code for it. A push button module is only pressed for a moment, so its sketch waits for a press. The rocker module never has a "press": your sketch reads its state (ON or OFF), and it reacts when that state changes.

When the switch is open, a 10 kΩ pull-down resistor on the board (the one marked "103") holds SIG at 0V. So SIG never floats, and you do not add any resistor yourself.

ON/OFF Rocker Switch Module
Switch KCD1 rocker, SPST, maintained (latching)
Marks on the rocker I = ON, O = OFF
Supply voltage (VCC) 3.3V to 5V
SIG when ON HIGH, the same voltage as VCC
SIG when OFF LOW (0V), held by the on-board pull-down
On-board resistor 10 kΩ pull-down on SIG
Header 3 pins, 2.54 mm pitch, right-angle, male
Size with the pins about 24 x 29 x 22 mm

The text "6A 250VAC / 10A 125VAC" on the switch body is the rating of the bare switch. On this module, the switch only passes a weak logic signal to one input pin. Never connect mains power or a motor to the header.

The pin names are printed on the back of the board, right beside the header:

  • VCC pin: must be connected to the 3.3V pin of the Arduino Nano 33 IoT. The module itself accepts 3.3V to 5V, but this board needs 3.3V
  • SIG pin: must be connected to a digital pin of the Arduino Nano 33 IoT (D2 in this tutorial). It is HIGH while the rocker sits on "I" and LOW while it sits on "O"
  • GND pin: must be connected to GND (0V)

※ NOTE THAT:

Turn the module over and read the labels before you plug in any wire. A module from another maker may put SIG, VCC and GND in a different order.

Wiring Diagram

Three jumper wires do the whole job, with no resistor and no breadboard. Pay attention to the power wire. When the rocker is ON, SIG simply passes on the voltage of VCC. The SAMD21 chip of the Arduino Nano 33 IoT runs at 3.3V, and its pins do not accept 5V. So VCC goes to the 3.3V pin, and D2 then never sees more than 3.3V.

The wiring diagram between Arduino Nano and 33 IoT rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin Arduino Nano 33 IoT Pin
VCC 3.3V
SIG D2
GND GND

Want a stronger connection than jumper wires? Try a Screw Terminal Expansion Board for Arduino Nano: the Arduino Nano 33 IoT sits on it, and each pin gets its own screw terminal. Each wire goes to the screw terminal of the same pin, as in the image below.

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

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Do not use the pin marked 5V on the Arduino Nano 33 IoT for this module. That pin is VUSB: it stays unconnected until you close a solder jumper under the board, and even then it gives 5V only while the board runs on USB power. 5V on SIG could damage pin D2.

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do NOT use internal or external pull-down resistors for these pins

How To Program For Rocker Switch Module

Everything here uses the built-in Arduino functions. No library is needed.

  • The first step is to choose a name for D2, the pin where the SIG wire ends:
#define ROCKER_PIN 2
  • In setup(), make that pin a plain INPUT with pinMode(). INPUT_PULLUP is not wanted here: the pull-down resistor on the module already decides the level while the switch is open.
pinMode(ROCKER_PIN, INPUT);
  • Ask digitalRead() for the position. HIGH means the rocker is on "I" (ON), and LOW means it is on "O" (OFF):
int level = digitalRead(ROCKER_PIN);
  • To act only at the moment of a flip, keep the last accepted state in a variable. The metal contacts bounce for a few milliseconds after each flip, so a new level is accepted only after it has stayed the same for 50 milliseconds. millis() measures those 50 milliseconds, so the loop never has to stop and wait.

Arduino Nano 33 IoT Code for Rocker Switch Module - Print the Switch State

The first sketch is a plain check of the wiring. Every 300 milliseconds, it reads D2 and prints the level (1 or 0) and the matching word. The rocker stays where you put it, so this slow, repeated reading is all you need here, and bounce does not matter.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-rocker-switch-module */ #define ROCKER_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the SIG pin of the rocker switch module #define READ_PERIOD 300 // time between two readings (milliseconds) void setup() { Serial.begin(9600); // plain INPUT: the 10k pull-down resistor on the module keeps SIG at LOW while the switch is OFF, // so no internal pull-up is needed pinMode(ROCKER_PIN, INPUT); } void loop() { int level = digitalRead(ROCKER_PIN); // HIGH = rocker pressed on "I", LOW = rocker pressed on "O" Serial.print("D2 level "); Serial.print(level); if (level == HIGH) Serial.println(" => rocker module ON"); else Serial.println(" => rocker module OFF"); delay(READ_PERIOD); // the rocker stays where you put it, so a few readings per second are enough }

Detailed Instructions

Before you start, make sure the Arduino IDE can program your board: the Getting Started with Arduino Nano 33 IoT tutorial shows how to add it to the IDE.

  • Wire the module as shown in the diagram. Check one more time that VCC is on 3.3V.
  • Connect the Arduino Nano 33 IoT board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Pick the Arduino Nano 33 IoT board and its COM port.
  • Copy the code above into a new sketch.
  • Click the Upload button. When the IDE says "Done uploading", the sketch is running.
  • Open the Serial Monitor and set it to 9600 baud.
  • Leave the rocker on "O" for a moment, press the "I" side and count to two, then press the "O" side again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
D2 level 0 => rocker module OFF D2 level 0 => rocker module OFF D2 level 0 => rocker module OFF D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 1 => rocker module ON D2 level 0 => rocker module OFF D2 level 0 => rocker module OFF
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

About seven ON lines for two seconds: that fits the 300 ms reading period. If every line shows 0, the module has no power: look at the VCC and GND wires. If the upload fails because the IDE cannot find the board, press the reset button of the Arduino Nano 33 IoT twice quickly and upload again.

Arduino Nano 33 IoT Code for Rocker Switch Module - Seconds Counter With a Pause Switch

Now the rocker becomes the pause switch of a counter. While it is on "I", the count goes up by one every second, a new line appears, and the built-in LED of the board (LED_BUILTIN, on pin D13) is lit. Flip it to "O" at, say, 37 seconds: the sketch prints "Paused at 37 s", the LED goes dark, and the number waits. Flip it back to "I" and the count goes on from 37, not from 0.

How does it work? The sketch adds up only the time that passes while the rocker is ON. It also keeps the milliseconds, so a pause in the middle of a second does not lose that part of the second. Each flip prints one line, because a new level must stay the same for 50 milliseconds before the sketch believes it. Both the counting and the debounce use millis(), as in Arduino Nano 33 IoT - LED - Blink Without Delay. One more detail: the Arduino Nano 33 IoT talks to the PC through its own native USB port, so setup() waits up to 3 seconds for the Serial Monitor. Without a PC, the sketch simply goes on.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-rocker-switch-module */ // Pause / resume counter with the rocker switch module: // rocker on "I" (ON) -> a seconds counter runs, prints its value once per second, and the built-in LED is on // rocker on "O" (OFF) -> the counter pauses and keeps its value, and the built-in LED is off // Flip the rocker ON again and the counter goes on from where it stopped. #define ROCKER_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the SIG pin of the rocker switch module #define STEADY_TIME 50 // a new rocker level must stay the same this long before it is accepted (milliseconds) int rocker_state; // accepted rocker level: HIGH = ON (counting), LOW = OFF (paused) int raw_previous; // raw level read in the previous pass of loop() unsigned long raw_changed_at; // millis() value when the raw level changed the last time unsigned long counted_ms = 0; // total time the rocker has been ON, in milliseconds unsigned long previous_ms; // millis() value of the previous pass of loop() unsigned long shown_seconds = 0; // the last whole second that was printed void setup() { Serial.begin(9600); // The Nano 33 IoT uses its own native USB port. Give the Serial Monitor // up to 3 seconds to connect, so the first lines are not lost. unsigned long wait_start = millis(); while (!Serial && millis() - wait_start < 3000) { delay(10); } pinMode(ROCKER_PIN, INPUT); // the module has its own pull-down resistor pinMode(LED_BUILTIN, OUTPUT); // the built-in LED (D13) shows when the counter runs rocker_state = digitalRead(ROCKER_PIN); // where is the rocker at power-up? raw_previous = rocker_state; raw_changed_at = millis(); previous_ms = millis(); digitalWrite(LED_BUILTIN, rocker_state); Serial.println("Seconds counter on D2: I = count, O = pause"); if (rocker_state == HIGH) Serial.println("Started at 0 s"); else Serial.println("Paused at 0 s"); } void loop() { unsigned long now = millis(); // 1. Debounce: a new rocker level counts only after it stayed the same for STEADY_TIME int raw = digitalRead(ROCKER_PIN); if (raw != raw_previous) { // the level moved: a real flip or a contact bounce raw_previous = raw; raw_changed_at = now; // start the steady-time check again } if (raw != rocker_state && now - raw_changed_at >= STEADY_TIME) { rocker_state = raw; // the flip is real digitalWrite(LED_BUILTIN, rocker_state); if (rocker_state == LOW) Serial.print("Paused at "); else if (counted_ms == 0) Serial.print("Started at "); else Serial.print("Resumed at "); Serial.print(shown_seconds); Serial.println(" s"); } // 2. Count: add the time of this pass only while the rocker is ON if (rocker_state == HIGH) { counted_ms += now - previous_ms; unsigned long seconds = counted_ms / 1000; if (seconds != shown_seconds) { // one more whole second is complete shown_seconds = seconds; Serial.print("Count: "); Serial.print(shown_seconds); Serial.println(" s"); } } previous_ms = now; // no delay() anywhere, so the rocker is checked thousands of times per second }

Detailed Instructions

  • Do not change the wiring. The LED is already on the board.
  • Put the rocker on "O".
  • Paste the code above into the Arduino IDE in place of the first sketch, and click Upload.
  • Open the Serial Monitor (9600 baud) right after the upload.
  • Flip the rocker to "I", let it count for a few seconds, then flip it to "O". Wait a moment, and flip it to "I" again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
Seconds counter on D2: I = count, O = pause Paused at 0 s Started at 0 s Count: 1 s Count: 2 s Count: 3 s Count: 4 s Count: 5 s Paused at 5 s Resumed at 5 s Count: 6 s Count: 7 s Count: 8 s Paused at 8 s
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

While the counter is paused, no new lines appear, however long you wait. The counter starts from 0 again only when the board restarts, for example after you press its reset button or unplug it.

Additional Knowledge

Rocker Module, Button Module or Bare Switch?

All three are inputs for the Arduino Nano 33 IoT, but they do not behave the same:

Rocker Switch Module Button Module Bare ON/OFF Switch
After you let go stays ON or OFF springs back (released) stays ON or OFF
Pins SIG, VCC, GND (labeled) VCC, OUT, GND (labeled) 2 legs, no labels
Extra resistor none (10 kΩ pull-down on the board) none (pull-down on the board) none, but use INPUT_PULLUP
pinMode() INPUT INPUT INPUT_PULLUP
Level when ON or pressed HIGH HIGH LOW (inverted)
What the code looks for the state and its changes a short press the state and its changes

It is evident that the rocker module is the easiest way to give a Nano 33 IoT project a setting that stays put: the pins are labeled, nothing needs soldering, and ON reads as HIGH. The bare switch from the Arduino Nano 33 IoT - Switch tutorial behaves the same way but reads upside down. The button module is the right pick for a key that you press and release.

Why Not Count With delay(1000)?

It looks simpler to write delay(1000) and add one to the counter. But during that delay, the Arduino Nano 33 IoT does nothing else. A flip to "O" could wait up to one second before the sketch sees it, and the 50 ms bounce filter could not work at all. Each step would also take a little more than one second, because printing takes time too, so the count would slowly fall behind the clock. With millis(), loop() runs thousands of times per second: the sketch sees every flip at once, and each second is counted from the real clock of the board.

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!