Arduino DIP Switch Binary Input: Wiring, Code and Simulator

To build this Arduino DIP Switch Binary Input project you need an Arduino Uno and an 8-way DIP switch. Eight DIP switches on pins 2–9 with internal pull-ups, read into one byte. Make the 16 connections in the wiring table, upload the Arduino C++ code, and flip switches and read the number in binary and decimal on the serial monitor.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
18-way DIP switchEight switches: switch n joins na to nb when on. Click a switch to flip it.

Wiring

#FromTo
1Arduino Uno pin 28-way DIP switch 1a
2Arduino Uno pin 38-way DIP switch 2a
3Arduino Uno pin 48-way DIP switch 3a
4Arduino Uno pin 58-way DIP switch 4a
5Arduino Uno pin 68-way DIP switch 5a
6Arduino Uno pin 78-way DIP switch 6a
7Arduino Uno pin 88-way DIP switch 7a
8Arduino Uno pin 98-way DIP switch 8a
9Arduino Uno GND8-way DIP switch 1b
10Arduino Uno GND8-way DIP switch 2b
11Arduino Uno GND8-way DIP switch 3b
12Arduino Uno GND8-way DIP switch 4b
13Arduino Uno GND8-way DIP switch 5b
14Arduino Uno GND8-way DIP switch 6b
15Arduino Uno GND8-way DIP switch 7b
16Arduino Uno GND8-way DIP switch 8b

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an 8-way DIP switch from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 2 to 8-way DIP switch 1a.
  4. Wire 2. Connect Arduino Uno pin 3 to 8-way DIP switch 2a.
  5. Wire 3. Connect Arduino Uno pin 4 to 8-way DIP switch 3a.
  6. Wire 4. Connect Arduino Uno pin 5 to 8-way DIP switch 4a.
  7. Wire 5. Connect Arduino Uno pin 6 to 8-way DIP switch 5a.
  8. Wire 6. Connect Arduino Uno pin 7 to 8-way DIP switch 6a.
  9. Wire 7. Connect Arduino Uno pin 8 to 8-way DIP switch 7a.
  10. Wire 8. Connect Arduino Uno pin 9 to 8-way DIP switch 8a.
  11. Wire 9. Connect Arduino Uno GND to 8-way DIP switch 1b.
  12. Wire 10. Connect Arduino Uno GND to 8-way DIP switch 2b.
  13. Wire 11. Connect Arduino Uno GND to 8-way DIP switch 3b.
  14. Wire 12. Connect Arduino Uno GND to 8-way DIP switch 4b.
  15. Wire 13. Connect Arduino Uno GND to 8-way DIP switch 5b.
  16. Wire 14. Connect Arduino Uno GND to 8-way DIP switch 6b.
  17. Wire 15. Connect Arduino Uno GND to 8-way DIP switch 7b.
  18. Wire 16. Connect Arduino Uno GND to 8-way DIP switch 8b.
  19. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  20. Run it. Press Run. Flip switches and read the number in binary and decimal on the serial monitor.

Arduino C++ code

// Binary input: eight DIP switches on pins 2-9 make one byte.
// Each switch joins its pin to GND, so ON reads LOW.
void setup() {
  for (int pin = 2; pin <= 9; pin++) pinMode(pin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  static int last = -1;
  int value = 0;
  for (int bit = 0; bit < 8; bit++) {
    if (digitalRead(2 + bit) == LOW) value |= 1 << bit;
  }
  if (value != last) {
    Serial.print("Switches: ");
    Serial.print(value);
    Serial.print(" = 0b");
    Serial.println(value, BIN);
    last = value;
  }
  delay(20);
}

What to try

Flip switches and read the number in binary and decimal on the serial monitor.

Frequently asked questions

How do I wire the DIP Switch Binary Input project on an Arduino Uno?

Make 16 connections: Arduino Uno pin 2 to 8-way DIP switch 1a; Arduino Uno pin 3 to 8-way DIP switch 2a; Arduino Uno pin 4 to 8-way DIP switch 3a; Arduino Uno pin 5 to 8-way DIP switch 4a; Arduino Uno pin 6 to 8-way DIP switch 5a; Arduino Uno pin 7 to 8-way DIP switch 6a; Arduino Uno pin 8 to 8-way DIP switch 7a; Arduino Uno pin 9 to 8-way DIP switch 8a; Arduino Uno GND to 8-way DIP switch 1b; Arduino Uno GND to 8-way DIP switch 2b; Arduino Uno GND to 8-way DIP switch 3b; Arduino Uno GND to 8-way DIP switch 4b; Arduino Uno GND to 8-way DIP switch 5b; Arduino Uno GND to 8-way DIP switch 6b; Arduino Uno GND to 8-way DIP switch 7b; Arduino Uno GND to 8-way DIP switch 8b.

What parts do I need for this Arduino project?

An Arduino Uno and an 8-way DIP switch.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Eight DIP switches on pins 2–9 with internal pull-ups, read into one byte.

More Arduino Uno projects