ESP32 C3 Super Mini - DIP Switch

A DIP (Dual In-line Package) switch is a small block of slide switches that you set by hand. It is used for device addresses, communication settings, security codes and operating modes. This tutorial shows you how to use a DIP switch with the ESP32 C3 Super Mini.

In this tutorial, you will learn:

ESP32 C3 Super Mini - DIP Switch

Hardware Preparation

1×ESP32 C3 Super Mini
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×DIP Switch
1×Breadboard
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 DIP Switch

A DIP switch is a group of tiny slide switches in one package, and each small switch is called a "position".

Key Features:

  • Many switches in one part: Each position is its own ON/OFF switch
  • Set by hand: You slide a position with a pen tip or a small screwdriver
  • Keeps its setting: The value stays after power off
  • No polarity: The two pins of a position can be swapped
  • Beginner friendly: Works with the 3.3V logic of the ESP32 C3 Super Mini

Technical Specifications:

  • Common sizes: 2, 4, 5, 6, 8 and 10 positions
  • Pin count: 2 pins per position, half on each row
  • Switch type: Mechanical SPST per position
  • Contact resistance: Near 0 ohm when ON, very high when OFF
  • Logic level used here: 3.3V from the ESP32 C3 Super Mini

Each position stands for one bit of a number. By sliding the positions between ON and OFF, you set the number you want.

Pinout

A DIP switch has two rows of pins, and each row has the same number of pins as positions.

DIP Switch Pinout
image source: diyables.io
  • Top row pins: One side of each slide switch
  • Bottom row pins: The other side of the same slide switch
  • Pin pairs: Two pins facing each other make one position
  • No fixed order: The two sides can be used interchangeably
  • Example: A 4-position DIP switch has 8 pins in total, 4 on each side

How It Works

Each position of a DIP switch works like a wire that you can open or close.

  • ON position: The switch is closed, so current can pass through
  • OFF position: The switch is open, so current is blocked

Wire one side of a position to GND and the other side to an ESP32 C3 Super Mini pin set as a pull-up input. The table below shows what you read:

DIP switch position Binary representation Circuit state ESP32 C3 Super Mini pin state
ON 1 CLOSED LOW
OFF 0 OPEN HIGH

※ NOTE THAT:

The ESP32 C3 Super Mini uses 3.3V logic only. Never connect a 5V signal to any GPIO pin.

The next parts use a 4-position DIP switch as an example. You can adapt the same idea for 2-position, 3-position, 5-position, 6-position, 8-position and 10-position DIP switches.

Wiring Diagram

Connect one side of each position to a GPIO pin, and the other side of every position to GND.

  • Note: Use 3.3V logic only. Do not feed 5V into a GPIO pin.
  • Note: GPIO8 and GPIO9 are boot strapping pins. Slide positions 3 and 4 to OFF before you power up or reset the board, or it will not start normally.
  • Note: Do not use GPIO20 or GPIO21 — the Serial Monitor uses them.
  • Note: The two pins of one position can be swapped.
The wiring diagram between ESP32 C3 Super Mini DIP switch

This image is created using Fritzing. Click to enlarge image

DIP Switch Pin ESP32 C3 Super Mini Pin
Position 1 (side A) GPIO6
Position 2 (side A) GPIO7
Position 3 (side A) GPIO8
Position 4 (side A) GPIO9
Position 1 to 4 (side B) GND

ESP32 C3 Super Mini Code - DIP Switch

We will learn through two pieces of code:

  • Reading the ON/OFF state of each position on the DIP switch.
  • Encoding the positions into a number.

ESP32 C3 Super Mini code - Reading the ON/OFF state of the DIP switch

This first sketch prints the state of every position, one line per position.

What This Code Does:

  • Sets GPIO6, GPIO7, GPIO8 and GPIO9 as inputs with the internal pull-up on
  • Reads each DIP switch position every half second
  • Prints ON when the pin reads LOW, which means the switch is closed
  • Prints OFF when the pin reads HIGH, which means the switch is open
  • Adds a small delay so the Serial Monitor stays easy to read
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-dip-switch */ #define POSITION_NUM 4 #define ON LOW #define OFF HIGH // define the ESP32 C3 SuperMini pins connected to the dip switch const int SWITCH_PINS[] = { 6, 7, 8, 9 }; void setup() { // initialize serial communication Serial.begin(115200); // 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++) { Serial.print("position "); Serial.print(i + 1); Serial.print(": "); int state = digitalRead(SWITCH_PINS[i]); if (state == ON) Serial.println("ON "); else Serial.println("OFF "); } Serial.println(); // add a delay to prevent rapid readings delay(500); }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire the parts: Follow the wiring diagram above to connect the DIP switch to the ESP32 C3 Super Mini.
  • Connect the board: Plug the ESP32 C3 Super Mini into your PC with a USB Type-C cable.
  • Open Arduino IDE: Launch the Arduino IDE software.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Pick the COM port of your ESP32 C3 Super Mini in Tools > Port.
  • Upload the code: Copy the code above, paste it into the Arduino IDE, then click the Upload button.
  • Open Serial Monitor: Set the baud rate to 115200.
  • Slide the positions: Move each position of the DIP switch to ON one by one.
  • See the result: Watch the Serial Monitor print the new states.
  • Pro Tip: If a position always reads ON, check that its GND wire does not touch the GPIO wire.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
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 position 1: ON position 2: ON position 3: ON position 4: OFF position 1: ON position 2: ON position 3: ON position 4: ON
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

ESP32 C3 Super Mini code - Encoding the states of DIP switch into a number

This second sketch turns the four positions into one decimal number.

What This Code Does:

  • Reads the same four pins: GPIO6, GPIO7, GPIO8 and GPIO9
  • Uses position 1 as the highest bit and position 4 as the lowest bit
  • Sets a bit to 1 when its position is ON
  • Prints the final number, from 0 to 15 for a 4-position DIP switch
  • Repeats the reading every half second
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-dip-switch */ #define POSITION_NUM 4 #define ON LOW #define OFF HIGH // define the ESP32 C3 SuperMini pins connected to the dip switch const int SWITCH_PINS[] = { 6, 7, 8, 9 }; void setup() { // Initialize serial communication Serial.begin(115200); // 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); } Serial.print("encoded state: "); Serial.println(encoded_state); // add a delay to prevent rapid readings delay(500); }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Keep the same wiring: The wiring diagram above does not change.
  • Connect the board: Plug the ESP32 C3 Super Mini into your PC with a USB Type-C cable.
  • Open Arduino IDE: Launch the Arduino IDE software.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Pick the COM port of your ESP32 C3 Super Mini in Tools > Port.
  • Upload the code: Copy the second code above, paste it into the Arduino IDE, then click the Upload button.
  • Open Serial Monitor: Set the baud rate to 115200.
  • Slide the positions: Move each position of the DIP switch to ON one by one.
  • See the result: The printed number changes as you slide the positions.
  • Pro Tip: An 8-position DIP switch gives you 0 to 255. Just change POSITION_NUM and add more pins.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
encoded state: 0 encoded state: 1 encoded state: 2 encoded state: 3 encoded state: 4 encoded state: 5 encoded state: 6 encoded state: 7 encoded state: 8 encoded state: 9 encoded state: 10 encoded state: 11 encoded state: 12 encoded state: 13 encoded state: 14 encoded state: 15
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

The value depends on the position of each slide switch. The table below shows the mapping between the ON/OFF positions and the integer value for a 4-position DIP switch:

Position-1 Position-2 Position-3 Position-4 Binary Value Decimal Value
OFF OFF OFF OFF 0000 0
OFF OFF OFF ON 0001 1
OFF OFF ON OFF 0010 2
OFF OFF ON ON 0011 3
OFF ON OFF OFF 0100 4
OFF ON OFF ON 0101 5
OFF ON ON OFF 0110 6
OFF ON ON ON 0111 7
ON OFF OFF OFF 1000 8
ON OFF OFF ON 1001 9
ON OFF ON OFF 1010 10
ON OFF ON ON 1011 11
ON ON OFF OFF 1100 12
ON ON OFF ON 1101 13
ON ON ON OFF 1110 14
ON ON ON ON 1111 15

Line-by-line Code Explanation

The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!

Application/Project Ideas

Here are simple ways to use a DIP switch with the ESP32 C3 Super Mini.

  • Device Address: Give each board a number so many boards can share one bus.
  • Mode Selector: Pick one of 16 program modes without changing the code.
  • Secret Code Lock: Open a relay only when the right pattern is set.
  • Sensor Calibration: Choose a threshold step for a sensor reading.
  • Wi-Fi Profile Picker: Select which saved Wi-Fi network the board joins.
  • LED Pattern Chooser: Change a light animation by sliding one position.

Challenge Yourself

Try these steps to go further with the ESP32 C3 Super Mini and a DIP switch.

  • Easy: Turn an external LED on GPIO3 ON when position 1 is ON.
  • Easy: Print the value in binary with Serial.println(encoded_state, BIN).
  • Medium: Print a message only when the encoded number changes.
  • Medium: Use the number to set a blink speed, from fast to slow.
  • Advanced: Add a 5th and 6th position on GPIO2 and GPIO4 for values 0 to 63.
  • Advanced: Send the DIP switch value over Wi-Fi to a web page.

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