A DIP switch is a block of tiny slide switches you set by hand, handy for things like device addresses, operating modes or config flags that should survive a power cycle without touching your code. This tutorial wires a 4-position DIP switch to an ESP32 C3 Super Mini and reads it two ways: as individual ON/OFF states, and as a single encoded number.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 C3 Super Mini - DIP Switch
What you need
- ESP32 C3 Super Mini
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- DIP Switch
- Breadboard
- Jumper Wires
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect one side of each switch position to a GPIO pin — GPIO6, GPIO7, GPIO8 and GPIO9 for positions 1 to 4 — and the other side of every position to GND. Since the two pins of a position aren't polarized, you can swap them freely.
Stick to 3.3V logic, never feed 5V into a GPIO. GPIO8 and GPIO9 are boot strapping pins, so slide positions 3 and 4 to OFF before powering up or resetting the board, otherwise it won't boot normally. Avoid GPIO20 and GPIO21 too, since the Serial Monitor uses them.
How it works
Each position behaves like a wire with a switch in it: ON closes the circuit so current flows, OFF opens it and blocks current. You wire one side of a position to GND and the other to a GPIO configured as a pull-up input — INPUT_PULLUP keeps the pin reading HIGH by default through an internal resistor, so when the switch closes and connects the pin to GND, the reading drops to LOW. That's why in the code ON is defined as LOW and OFF as HIGH.
Code
The first sketch sets GPIO6, GPIO7, GPIO8 and GPIO9 as INPUT_PULLUP, then loops through them every half second, printing ON when digitalRead() returns LOW and OFF when it returns HIGH, one line per position.
The second sketch reads the same four pins but combines them into one decimal number: position 1 becomes the highest bit, position 4 the lowest, each ON position sets its bit to 1. The result ranges from 0 to 15 for a 4-position switch, printed once every half second.
esp32-c3-super-mini-dip-switch.ino
/*
* 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);
}
Things that go wrong
If the board refuses to boot or behaves oddly on power-up, check positions 3 and 4 — since they sit on GPIO8 and GPIO9, boot strapping pins, they need to be OFF when the board starts. If readings seem stuck or garbled over Serial, make sure you haven't wired a position to GPIO20 or GPIO21, which the Serial Monitor itself relies on. And never connect a 5V source to these pins; the ESP32 C3 Super Mini only handles 3.3V logic.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-dip-switch

Top comments (0)