Arduino UNO Q - DIP Switch

DIP (Dual In-line Package) switches are compact configuration switches used to set addresses, enable features, or select modes. In this tutorial, you will learn how to wire a 4-position DIP switch to Arduino UNO Q, read each position's ON/OFF state, encode the positions into a number, and check values remotely via Telegram.

Arduino UNO Q - DIP Switch

Hardware Preparation

1×Arduino UNO Q
1×USB Cable for Arduino Uno Q
1×DIP Switch
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

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 DIP Switch

A DIP switch is a multi-position slide switch unit. Each individual position is an independent ON/OFF switch. Common variants have 2, 4, 5, 6, 8, or 10 positions.

Pinout

A DIP switch has two rows of pins — one row per side of each position. Each pair of opposite pins forms one slide switch. It does not matter which side connects to GND.

DIP Switch Pinout

How It Works

Connect one side of each position to GND and the other side to an Arduino UNO Q digital pin configured as INPUT_PULLUP. The table below shows the logic:

DIP switch position Circuit state Arduino UNO Q pin state Binary value
ON CLOSED LOW 1
OFF OPEN HIGH 0

This tutorial uses a 4-position DIP switch on pins 2, 3, 4, 5. The same approach works for 2, 5, 6, 8, or 10 positions — just adjust the pin count.

Wiring Diagram

The wiring diagram between Arduino UNO Q DIP Switch

This image is created using Fritzing. Click to enlarge image

MCU Code — DIP Switch

The Arduino UNO Q has two processors: the STM32 MCU (handles real-time hardware control) and the Qualcomm MPU (runs Debian Linux). In this section, only the STM32 MCU is programmed — the Linux side stays idle. A later section will show how both processors work together.

Two code examples are provided:

  • Reading each position as ON/OFF
  • Encoding all positions into a single integer

MCU Code — Reading each DIP position as ON/OFF

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-dip-switch */ #define POSITION_NUM 4 #define ON LOW #define OFF HIGH // define the pins connected to the DIP switch const int SWITCH_PINS[] = { 2, 3, 4, 5 }; void setup() { // set the DIP switch pins as inputs with pull-up resistors enabled for (int i = 0; i < POSITION_NUM; i++) pinMode(SWITCH_PINS[i], INPUT_PULLUP); } void loop() { // Read the state of each switch position for (int i = 0; i < POSITION_NUM; i++) { int state = digitalRead(SWITCH_PINS[i]); if (state == ON) { // position i+1 is ON // TO DO: add action for ON position here } else { // position i+1 is OFF // TO DO: add action for OFF position here } } delay(500); }

Detailed Instructions

  • First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial to get your development environment ready before proceeding.
  • Wire the DIP switch: Connect one side of each position to GND and the other side to pins 2–5 on the Arduino UNO Q.
  • Connect: Plug the Arduino UNO Q into your computer with a USB-C cable.
  • Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
  • Create a new App: Click the Create New App button.
Create New App in Arduino App Lab on Arduino UNO Q
  • Give the App a name, for example: DIYables_DIPSwitch
  • Click Create to confirm.
  • You will see a set of folders and files generated inside your new App.
Arduino App Lab App folders and files on Arduino UNO Q
  • Find the sketch/sketch.ino file — this is where you will paste the MCU sketch.
  • Paste the sketch: Copy the MCU code above and paste it into the sketch file. Keep other files as default.
    • Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
    Add sketch library in Arduino App Lab on Arduino UNO Q
    • Search for Arduino_RouterBridge created by Arduino and click the Install button.
    My Apps / DIYables Apps
    Run
    Bricks
    No bricks added...
    Sketch Libraries
    No sketch libra...
    Files
    python
    sketch
    .gitignore
    README.md
    app.yaml
    sketch.ino
    Add sketch library
    Arduino_RouterBridge Arduino

    This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.

    0.4.1
    Install
    More Info
    • Upload: Click the Run button in Arduino App Lab to compile and upload to the STM32.
    Click Run button in Arduino App Lab on Arduino UNO Q
    • Flip the DIP positions one at a time. State is read every 500 ms and logged via Bridge Monitor in the next section.
    • Pro Tip: You can expand POSITION_NUM and SWITCH_PINS[] to support 6, 8, or 10-position DIP switches.

    MCU Code — Encoding DIP positions into a number

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-dip-switch */ #define POSITION_NUM 4 #define ON LOW #define OFF HIGH // define the pins connected to the DIP switch const int SWITCH_PINS[] = { 2, 3, 4, 5 }; void setup() { // set the DIP switch pins as inputs with pull-up resistors enabled for (int i = 0; i < POSITION_NUM; i++) pinMode(SWITCH_PINS[i], INPUT_PULLUP); } void loop() { int encoded_state = 0; for (int i = 0; i < POSITION_NUM; i++) { int state = digitalRead(SWITCH_PINS[i]); if (state == ON) encoded_state |= 1 << (POSITION_NUM - i - 1); } // TO DO: use encoded_state (0-15) in your logic here delay(500); }

    Detailed Instructions

    • Use the same App and wiring from the previous example.
    • Replace the sketch with the encoding code above and click Run.
    • Flip DIP positions to different combinations and observe the encoded value change in the Bridge Monitor.

    Linux + MCU Bridge Programming

    The Arduino UNO Q has two processors that work together: the MPU (Qualcomm, runs Debian Linux) and the MCU (STM32, runs Zephyr OS with your Arduino sketch). They communicate using RPC via the Arduino_RouterBridge library — never via raw serial ports.

    • The DIP switch is connected to the MCU (STM32) — wired to digital input pins on the STM32. The MCU reads each position using digitalRead() with INPUT_PULLUP.
    • The MPU cannot read the DIP switch directly — it must request the state from the MCU via Bridge.call(). The MCU responds immediately.
    • The MPU has Wi-Fi — because the MPU runs full Debian Linux with Wi-Fi, it can report the DIP state via Telegram on demand.
    • Communication: Bridge.call() on the Linux side invokes Bridge.provide() functions on the MCU side
    • ⚠️ Reserved: /dev/ttyHS1 (Linux) and Serial1 (MCU) are used by the Arduino Router — never open them directly

    In short: MPU requests DIP state → MCU reads pins → MCU reports positions and encoded value → MPU logs or forwards it.

    MCU sketch — DIP switch with Bridge and Monitor output:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-dip-switch */ #include "Arduino_RouterBridge.h" #define POSITION_NUM 4 #define ON LOW #define OFF HIGH const int SWITCH_PINS[] = { 2, 3, 4, 5 }; void get_dip_state() { for (int i = 0; i < POSITION_NUM; i++) { int state = digitalRead(SWITCH_PINS[i]); Monitor.print("position "); Monitor.print(i + 1); Monitor.print(": "); Monitor.println(state == ON ? "ON" : "OFF"); } } void get_encoded_state() { int encoded_state = 0; for (int i = 0; i < POSITION_NUM; i++) { int state = digitalRead(SWITCH_PINS[i]); if (state == ON) encoded_state |= 1 << (POSITION_NUM - i - 1); } Monitor.println(encoded_state); } void setup() { for (int i = 0; i < POSITION_NUM; i++) pinMode(SWITCH_PINS[i], INPUT_PULLUP); Bridge.begin(); Monitor.begin(); Bridge.provide("get_dip_state", get_dip_state); Bridge.provide("get_encoded_state", get_encoded_state); Monitor.println("DIP Switch Bridge ready"); } void loop() {}

    Python script (Arduino App Lab) — poll DIP state from Linux:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-dip-switch */ from arduino.app_utils import * import time def loop(): while True: Bridge.call("get_dip_state") encoded = Bridge.call("get_encoded_state") print(f"Encoded value: {encoded}") time.sleep(2) App.run(user_loop=loop)
    • Note: Make sure Bridge.begin() is called in the MCU sketch and the sketch is uploaded before running the Python script on the Linux side.
    • ⚠️ Warning: Never directly open /dev/ttyHS1 (on Linux) or use Serial1 (on MCU) in your code — these are reserved by the Arduino Router and accessing them will break the Bridge.

    Detailed Instructions

    • Upload the MCU sketch: Open Arduino App Lab, create a new App, paste the Bridge MCU sketch above into sketch/sketch.ino, install the Arduino_RouterBridge library, and click Run.
    • Add the Python script: Paste the Python code above into the Python tab of the same App.
    • Run the App: Click Run — the Python side polls the DIP switch every 2 seconds.
    • Flip DIP positions to different combinations.
    • Check the console: Open the Console tab → MCU Monitor subtab to see positions and encoded value logged in real time.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    Message (Enter to send a message to "Newbiely" on usb(2820070321))
    New Line
    9600 baud
    DIP Switch Bridge ready position 1: OFF position 2: OFF position 3: OFF position 4: OFF position 1: ON position 2: OFF position 3: OFF position 4: OFF position 1: ON position 2: ON position 3: OFF position 4: OFF

    Telegram Integration

    Read the current DIP switch state remotely from anywhere via Telegram.

    If you do not have a Telegram bot yet, see How to Create a Telegram Bot to get your bot token before continuing.

    MCU sketch: Keep the same MCU sketch from the previous Bridge section — no changes needed. Make sure it is already uploaded and running on the STM32 before proceeding.

    Python script (Arduino App Lab) — Telegram bot for DIP switch state:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-dip-switch */ from arduino.app_utils import * import requests import time BOT_TOKEN = "YOUR_BOT_TOKEN" API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" last_update_id = 0 def send_message(chat_id, text): requests.post(f"{API_URL}/sendMessage", json={"chat_id": chat_id, "text": text}) def get_updates(): global last_update_id resp = requests.get(f"{API_URL}/getUpdates", params={"offset": last_update_id + 1, "timeout": 5}) return resp.json().get("result", []) def loop(): global last_update_id updates = get_updates() for update in updates: last_update_id = update["update_id"] msg = update.get("message", {}) chat_id = msg.get("chat", {}).get("id") text = msg.get("text", "").strip() if text == "/state": state = Bridge.call("get_dip_state") send_message(chat_id, state) elif text == "/value": encoded = Bridge.call("get_encoded_state") send_message(chat_id, f"DIP switch encoded value: {encoded}") else: send_message(chat_id, "Commands:\n/state — read each DIP position (ON/OFF)\n/value — get encoded integer value") time.sleep(1) App.run(user_loop=loop)
    • Note: Replace YOUR_BOT_TOKEN with the token obtained from @BotFather on Telegram.
    • Send /state to see each position's ON/OFF state.
    • Send /value to get the encoded integer value (0–15 for a 4-position DIP switch).

    Detailed Instructions

    • Upload the MCU sketch: Use the Bridge MCU sketch from the previous section (upload it first if not already done).
    • Paste the Telegram script: Copy the Python code above into the Python tab of your App in Arduino App Lab.
    • Set your token: Replace YOUR_BOT_TOKEN in the script with your actual bot token.
    • Run the App: Click Run — the bot starts listening for Telegram messages.
    • Test it: Flip some DIP positions, send /value — confirm the integer matches your switch pattern.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    [2026-04-29 12:00:01] Telegram: /value [2026-04-29 12:00:01] DIP switch encoded value: 5 [2026-04-29 12:03:20] Telegram: /state [2026-04-29 12:03:20] position 1: ON position 2: OFF position 3: ON position 4: OFF
    Telegram
    Telegram 12:45
    Welcome to Telegram!
    ArduinoBot 10:19
    Chatting with Arduino...
    telegram-botfather
    BotFather Yesterday
    Your bot has been created.

    ArduinoBot

    bot
    Today
    /value
    10:15 AM ✓✓
    DIP switch encoded value: 5
    10:16 AM
    /state
    10:17 AM ✓✓
    position 1: ON position 2: OFF position 3: ON position 4: OFF
    10:18 AM

    OpenClaw Integration

    OpenClaw integration for Arduino UNO Q DIP switch reading is coming soon.

    • Coming Soon: OpenClaw support for DIP switch state reading on Arduino UNO Q will be covered in a future update.

    Application/Project Ideas

    • Address selector: Use a DIP switch to assign a unique device address in a multi-device network
    • Remote mode check: Send /value via Telegram to check what operational mode is configured
    • Feature flags: Map each DIP position to a feature toggle (debug mode, logging, etc.)
    • Security code entry: Use a 4-position DIP switch combination as a simple access code
    • Configuration reporting: Automatically send current DIP state via Telegram on boot

    Challenge Yourself

    • Easy: Add code to print position labels (e.g., "debug: ON", "verbose: OFF") based on each DIP position's meaning
    • Medium: Extend the Bridge sketch to expose all 4 positions individually as separate get_position_N() callbacks
    • Advanced: Build a Telegram bot that sends a notification whenever the encoded DIP value changes — track the previous value and compare on each poll

    Function References

    ※ 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!