Arduino Giga R1 WiFi Rocker Switch Module

Who turned the heater on, and when was it turned off again? A machine in a tutorial often answers this with an event log: a list of times, one for each start and stop. In this guide, we will learn how to build such a log with the Arduino Giga R1 WiFi and the DIYables ON/OFF Rocker Switch Module. This little board carries the wide, two-sided switch known from power strips, ready to wire with three jumper wires. The Giga writes a time-stamped line for every flip, and its blue LED shows at a glance that the switch is ON. In detail, we will learn:

Arduino Giga R1 WiFi with ON/OFF rocker switch module - time-stamped switch log and blue status LED

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×ON/OFF Rocker Switch Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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

Seen from the top, the module shows a black KCD1 rocker with two halves. Under it is a blue square PCB of close to 24 x 24 mm, with four holes near the corners for screws or spacers. From one edge, three header pins bent at a right angle (2.54 mm pitch) stick out sideways. Including those pins, the part takes up about 24 x 29 x 22 mm. DIYables sells it in packs of 5.

Rock the "I" half down: ON. Rock the "O" half down: OFF. The PCB print says the same thing another way, "ON (H)" next to "OFF (L)". Those two letters in brackets give away what the Giga will read: a HIGH level for ON, a LOW level for OFF.

Electricians call this kind of part a maintained (or latching) switch. Inside there is a single contact pair, open or shut, so it is also called SPST. Nothing pushes it back after you flip it. For the program, this means a different question than with a button. With a button, you watch for a press. With this rocker, you check its position and compare it with the one you saw before.

DIYables rocker switch module pinout - signal, power and ground pins
image source: diyables.io

The labels hide on the underside of the board, beside the pins:

  • SIG pin: connect to pin 7 of the Giga (another digital pin works too). When the "I" half is down, SIG carries the VCC voltage, which is HIGH. With "O" down, SIG drops to 0V, which is LOW.
  • VCC pin: connect it to 3.3V on the Giga. The module works on 3.3V or 5V, yet the Giga allows only 3.3V here (see the note).
  • GND pin: connect to GND. Any of the GND pins of the Giga will do.

※ NOTE THAT:

Keep the VCC wire away from the 5V and VIN pins of the Giga. With the rocker ON, the voltage on VCC passes straight through to SIG, and SIG goes into pin 7. The STM32H747 chip on the Giga runs its pins at 3.3V, and 5V on one of its inputs can damage it.

ON/OFF Rocker Switch Module
Switch KCD1 rocker, SPST, maintained
Marks on the rocker I = ON, O = OFF
Header SIG, VCC, GND (3 pins, right-angle, 2.54 mm)
Supply voltage 3.3V to 5V
SIG output HIGH when ON, LOW when OFF
Resistor on SIG 10 kOhm pull-down (marked 103)
Board about 24 x 24 mm, 4 mounting holes
Size with pins about 24 x 29 x 22 mm

A few details shape the code and keep the setup safe:

  • When "O" is down, the 10 kOhm pull-down drags SIG to 0V, so the input never floats. That is why the sketches use plain INPUT. Skip the extra resistor, and leave INPUT_PULLUP off, because it would only fight the pull-down.
  • The rocker body carries a rating, "6A 250VAC / 10A 125VAC". It describes the bare switch. In this circuit the switch carries about 0.33 mA (3.3V through 10 kOhm). Mains wires and motors have no place on the header pins.
  • Right after a flip, the metal contacts chatter open and shut for a few milliseconds. The first sketch reads too slowly to see this. The log sketch polls the pin nonstop, so it needs a 50 ms filter.

The Giga already has tutorials for three related parts. Here is how they compare:

Wiring Diagram

Nothing sits between the module and the Giga: no breadboard and no resistor. Three jumper wires finish the build.

The wiring diagram between Arduino Giga R1 WiFi rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Pin Arduino Giga R1 WiFi Pin
SIG Pin 7
VCC 3.3V
GND GND

Pin 7 is only our pick. If your project already uses it, move the SIG wire to any free digital pin and change ROCKER_SWITCH_PIN to the new number.

How To Program For Rocker Switch Module

Only core Arduino functions are used, so there is nothing to install. Both sketches begin the same way:

  • Store the pin number in a constant:
const int ROCKER_SWITCH_PIN = 7;
  • Set it as an INPUT in setup(); the module's own pull-down keeps the level clean while the rocker is OFF:
pinMode(ROCKER_SWITCH_PIN, INPUT);
  • Read it. You get HIGH with the rocker on "I" and LOW with it on "O":
int sig = digitalRead(ROCKER_SWITCH_PIN);

The log sketch needs two more pieces that are special to the Giga:

  • The board package names the blue part of the on-board RGB LED LEDB. It works the other way round from most LEDs: LOW lights it, HIGH turns it off.
pinMode(LEDB, OUTPUT); digitalWrite(LEDB, LOW); // blue LED on
  • Millis() gives the milliseconds since the Giga started. One snprintf() call turns that number into minutes, seconds and milliseconds:
snprintf(text, sizeof(text), "[%02lu:%02lu.%03lu] ", ms / 60000UL, (ms / 1000UL) % 60UL, ms % 1000UL);

Arduino Giga R1 WiFi Code - Rocker Switch Module Position Check

Before building the log, confirm the wiring. This short sketch looks at pin 7 every 300 ms and writes the rocker position in words, with the raw 0 or 1 in brackets.

/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-rocker-switch-module */ const int ROCKER_SWITCH_PIN = 7; // Arduino Giga R1 WiFi pin 7 <- SIG pin of the rocker switch module void setup() { Serial.begin(9600); // The module has its own 10k pull-down resistor on SIG, so a plain INPUT is enough. // INPUT_PULLUP would work against that resistor, so do not use it. pinMode(ROCKER_SWITCH_PIN, INPUT); } void loop() { int sig = digitalRead(ROCKER_SWITCH_PIN); // 1 = "I" side down (ON), 0 = "O" side down (OFF) if (sig == HIGH) Serial.print("Rocker is ON (SIG "); else Serial.print("Rocker is OFF (SIG "); Serial.print(sig); Serial.println(")"); delay(300); // about three lines per second; this slow pace also hides the contact bounce }

Detailed Instructions

Follow these instructions step by step:

  • If your Arduino IDE has never worked with the Giga, add its board package first, as shown in Arduino Giga R1 WiFi - Getting Started.
  • Run the three wires from the diagram (pin 7, 3.3V, GND).
  • Use the USB Type-C cable to connect the Giga to your computer.
  • Open the Arduino IDE.
  • Pick the board Arduino Giga R1 WiFi and its COM port.
  • Copy the code, paste it into an empty sketch window and press Upload.
  • Start the Serial Monitor at 9600 baud.
  • Begin with the "O" half pressed. After a few lines, rock it to "I", count to two, and rock it back.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
Rocker is OFF (SIG 0) Rocker is OFF (SIG 0) Rocker is OFF (SIG 0) Rocker is ON (SIG 1) Rocker is ON (SIG 1) Rocker is ON (SIG 1) Rocker is ON (SIG 1) Rocker is ON (SIG 1) Rocker is ON (SIG 1) Rocker is OFF (SIG 0) Rocker is OFF (SIG 0)
Ln 11, Col 1
Arduino Giga R1 on COM15
2

Between flips, the screen keeps showing the same text: a latching switch sends the same level until someone moves it. Do you see OFF in both positions? Then pin 7 never gets a voltage. Check the VCC wire first, then the SIG wire.

For a test, a repeating list is fine. A real log should write one line per flip, together with the time it happened. That is the job of the next sketch.

Arduino Giga R1 WiFi Code - Rocker Switch Event Log with Timestamps

Think of a heater on your workbench, a small pump or a test setup, with this rocker as its power switch. Later, you want to know when it was turned on and how long it ran. Now the Giga keeps that record. It prints one line for each real flip:

  • "[mm:ss.mmm] switched ON" when the "I" side goes down. The blue LED of the Giga lights up.
  • "[mm:ss.mmm] switched OFF" when the "O" side goes down. The blue LED goes dark.

The time is minutes, seconds and milliseconds since the Giga started. There is no hour field: after 59 minutes, the minutes simply keep counting (60, 61, ...).

How the sketch works:

  • In setup(), it gives the Serial Monitor at most 3 seconds to connect. The USB port of the Giga reconnects after each upload, and the first line could get lost without this pause. With no computer attached, the sketch goes on by itself.
  • The first line records the position at start, and the blue LED is set to match it.
  • The debounce uses no delay(). Each time the level on pin 7 changes, the sketch notes the time. A new position counts only when pin 7 has kept it for 50 ms without a single change. So a chattering flip gives one line, not five.
  • The time in each line is the moment of the last level change, not the end of the 50 ms wait. So the log shows when the contacts came to rest.
/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-rocker-switch-module */ const int ROCKER_SWITCH_PIN = 7; // Arduino Giga R1 WiFi pin 7 <- SIG pin of the rocker switch module const unsigned long SETTLE_MS = 50; // a new level must stay the same this long before it counts int rawLevel; // last level read from the pin (it may still be bouncing) int switchState; // accepted position: HIGH = ON, LOW = OFF unsigned long rawChangedAt = 0; // millis() value when rawLevel last changed // Print a millis() value as [mm:ss.mmm] void printTimestamp(unsigned long ms) { char text[24]; snprintf(text, sizeof(text), "[%02lu:%02lu.%03lu] ", ms / 60000UL, (ms / 1000UL) % 60UL, ms % 1000UL); Serial.print(text); } // The blue LED of the Giga is active LOW: LOW = lit, HIGH = dark void showOnBlueLed(int state) { if (state == HIGH) digitalWrite(LEDB, LOW); // switch ON -> blue LED on else digitalWrite(LEDB, HIGH); // switch OFF -> blue LED off } void setup() { Serial.begin(9600); // Give the Serial Monitor up to 3 seconds to connect, so the first line is not lost. // Without a computer, the sketch simply goes on after 3 seconds. while (!Serial && millis() < 3000) { } pinMode(ROCKER_SWITCH_PIN, INPUT); // the module has its own pull-down resistor pinMode(LEDB, OUTPUT); // Start the log with the position the switch has right now rawLevel = digitalRead(ROCKER_SWITCH_PIN); switchState = rawLevel; showOnBlueLed(switchState); printTimestamp(millis()); Serial.print("log started, switch is "); if (switchState == HIGH) Serial.println("ON"); else Serial.println("OFF"); } void loop() { int level = digitalRead(ROCKER_SWITCH_PIN); if (level != rawLevel) { // the pin changed: a real flip or just a bounce rawLevel = level; rawChangedAt = millis(); // start the settle time again } // Accept the new level only after it stayed the same for SETTLE_MS if (rawLevel != switchState && millis() - rawChangedAt >= SETTLE_MS) { switchState = rawLevel; showOnBlueLed(switchState); printTimestamp(rawChangedAt); // time of the flip, not the end of the 50 ms wait if (switchState == HIGH) Serial.println("switched ON"); else Serial.println("switched OFF"); } }

Detailed Instructions

  • Keep the same three wires.
  • Upload this sketch, and keep the Serial Monitor open at 9600 baud.
  • Rock the switch between "I" and "O" several times. Make some ON periods long and one very short. Look at the RGB LED of the Giga while you do it: blue means ON, dark means OFF.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
[00:01.284] log started, switch is OFF [00:05.917] switched ON [00:13.402] switched OFF [00:15.660] switched ON [00:16.531] switched OFF [00:21.048] switched ON [01:04.775] switched OFF
Ln 11, Col 1
Arduino Giga R1 on COM15
2

Reading the log is simple subtraction. The first ON period starts at 00:05.917 and ends at 00:13.402, so the switch was ON for 7.485 seconds. The short one in the middle lasted less than one second. The last one runs from 00:21.048 to 01:04.775, which is 43.727 seconds.

Two things to know about this clock:

  • Millis() starts from zero after every reset or upload. So the times show how long the Giga has been running, not the time of day. If you need real clock times, a later version could get the time from the internet over the WiFi of the Giga.
  • A line appears only when the position changes. If the switch stays ON for an hour, the Serial Monitor stays quiet for an hour. The blue LED is your sign that it is still ON.

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!