In this tutorial you will learn how to use a DIP switch with an Arduino to control a set of LEDs.
A DIP switch is a group of switches packaged in a single housing. DIP switches are very helpful in setting the modes of operation or providing specific settings as input.
In this article, we go through the basics of DIP switches, look at several applications where DIP switches are essential, and study a datasheet of a DIP switch to understand the parameters.
I will present the hardware connection guide, a few examples of Arduino code, and a compilation of the most frequently answered questions about the DIP switches.
Let’s get started!
Components Needed To Build Arduino DIP Switch Project
Hardware Components
- Arduino Uno Rev3 x 1
- DIP Switch – Arduino Breadboard compatible x 1
- Dupont wire x 1 set
- Arduino USB cable (for powering Arduino and programming) x 1
Software
Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.
Basics of A DIP Switch
In this section, I will brief about DIP switches in detail. We will see the types of DIP switches available, the specifications of a DIP switch, and a few applications.
You will find DIP switches in many applications. The DIP switches are in Modems, Control circuit boards, HMI boards, etc.
DIP Switch Actuators
The DIP switch consists of several tiny switches. To activate the switch, you will have to slide the actuator. Several types of actuators are available. You can choose the right one for your application.
Some DIP switches will have the actuators on the top, and some will have actuators on the edge. The side actuators are also known as Piano style DIP switches due to their resemblance to piano keys.
DIP Switch internals
The DIP switch contains several tiny switches. Each tiny switch can be operated independently. A simplistic representation of a DIP switch with eight positions is shown below. When you move the actuator to the ON position, it will close the switch and vice-versa.
DIP Switch Types
The DIP switches come in various options. There are NC, NO, multiple-stage switches, etc.
An NC switch is one where the tiny switches on the DIP module will be ON in a normal state.
An NO switch is one where the tiny switches on the DIP will be OFF when you put the switch in a normal state.
There is also an ON-FF-ON DIP switch where there can be three stages of the actuators.
You will also find SPDT and DPDT configurations of DIP switches. You should choose the one which is more suitable for your application.
DIP Switch Specifications
Let us see a DIP switch’s most important specifications, which will help you choose the right DIP switch reliably for your applications.
- Circuit type – Most of the DIP switches you find are SPST types (Single Pole, Single Throw). Many other configurations, such as single pole double throw, and double pole double throw, are also available.
- Number of Positions – The number of positions refers to the number of tiny switches on the module. The usual ones I use have either 4 or 8 positions. You will find multiple options in the stores.
- Current rating – The amount of current each tiny switch on the DIP can handle. Always use not more than 60% of the maximum rating. You shouldn’t operate the DIP switch close to the maximum current rating since you bring down the expected lifetime of the switch.
- Voltage rating – The voltage rating mentions the maximum voltage you can apply across the DIP switch. If you are using an Arduino UNO, the maximum voltage you may apply is 5 V. So, you should choose at least 5 V as the minimum rating of the Arduino UNO.
- Mechanical Life / Electrical Life – The number of operations supported by the DIP switch before the switching operations become unreliable.
- The On state resistance – The on-state resistance will be significantly less ( in a few milli Ohms range).
Below is a screenshot from a DIP switch datasheet, where you can see most of the parameters listed for our reference.
Applications Of DIP Switches
There are several applications for the DIP switch. I have listed a few below. Let me know what you are using the DIP switch for!
- You can generate two to the power N distinct values using the DIP switch to set the price. N represents the number of tiny switches present on the DIP switch. Hence, by using a DIP switch with four positions, you can create 16 different values.
- To Set timer options for automatic lighting.
- Assign different IDs to multiple remote controller nodes.
- Set the operation mode of the room controller (Vacation mode, hating mode, party mode, eco mode, etc.)
- Configuring the control boards to either act like a repeater, a hub, or a monitor.
- Use it as a security passcode to enter the garage.
- Set different communication frequency channels for wireless radios.
Connect The DIP Switch To An Arduino
In this section, we will build a project using Arduino UNO and a DIP switch. You will control the LEDs connected to the Arduino using the DIP switch.
Wiring of the DIP switch and the LEDs
The following picture shows the complete wiring of the DIP switch and the LEDs. To keep things simple, we will be using only 4 of the 8 switches of the DIP switch. But you can easily use the remaining ones in the same manner, if you want to.
Start the wiring by connecting the switches 1 to 4 of the DIP to the GPIO pins 7 to 4 of the Arduino (blue wires). Next connect all other pins of the DIP switch to ground (black wire).
Now, connect all the short pins of the LEDs (Cathode) to ground (GND) using a black wire. Finally, we need to connect al the long pins (individually) to the GPIO pins 13 to 10 of the Arduino. Each connection needs a current limiting resistor of 220Ω.
In the next section we will write the code to control the LEDs via the DIP switch.
Code to control the LEDs via DIP switch
In the code below, we control 4 LEDs using 4 of the 8 switches of the DIP switch. The state of each switch determines whether the corresponding LED is on or off. The loop continuously checks the state of each switch and updates the LEDs accordingly.
Have a look at the complete code first, before we discuss its details.
// Control 4 LEDs with a DIP switch const int LED1 = 13; const int LED2 = 12; const int LED3 = 11; const int LED4 = 10; const int S1 = 7; const int S2 = 6; const int S3 = 5; const int S4 = 4; void switch_led(int switchPin, int ledPin) { int state = digitalRead(switchPin); digitalWrite(ledPin, state == HIGH ? LOW : HIGH); } void setup() { pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(S1, INPUT_PULLUP); pinMode(S2, INPUT_PULLUP); pinMode(S3, INPUT_PULLUP); pinMode(S4, INPUT_PULLUP); } void loop() { switch_led(S1, LED1); switch_led(S2, LED2); switch_led(S3, LED3); switch_led(S4, LED4); delay(100); }
Constants and Variables
We start by defining the constants and variables. Here, we define the pin numbers for the LEDs and the DIP switches.
const int LED1 = 13; const int LED2 = 12; const int LED3 = 11; const int LED4 = 10; const int S1 = 7; const int S2 = 6; const int S3 = 5; const int S4 = 4;
Switch LED function
The switch_led()
function is responsible for controlling an LED based on the state of a DIP switch. It takes two parameters: the pin number of the switch and the pin number of the LED.
Inside the function, we read the state of the switch using digitalRead()
and then set the state of the LED using digitalWrite()
. If the switch is HIGH, the LED is set to LOW (off), and if the switch is LOW, the LED is set to HIGH (on).
void switch_led(int switchPin, int ledPin) { int state = digitalRead(switchPin); digitalWrite(ledPin, state == HIGH ? LOW : HIGH); }
Note that the logic is reversed, since we are using the internal pullups of the GPIO pins connected to the switch. This means, when the switch is open it is pulled to HIGH and when the switch is closed it is read as LOW. There for we switch the LEDs in reversed logic. If the switch is HIGH, it is in its off state and therefore we set the LED to LOW (off).
If you want to learn more about how to use switches and buttons with an Arduino, have a look at our tutorial Push-Button And Arduino. Similarly, if you want to know more about LEDs have a look at How To Blink An LED Using Arduino.
Setup function
In the setup()
function, we set the pin modes for the LEDs as OUTPUT and the DIP switches as INPUT_PULLUP. The INPUT_PULLUP mode enables the internal pull-up resistors for the switches, so they read HIGH when the switches are open and LOW when the switches are closed.
void setup() { pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(S1, INPUT_PULLUP); pinMode(S2, INPUT_PULLUP); pinMode(S3, INPUT_PULLUP); pinMode(S4, INPUT_PULLUP); }
You should avoid connecting switches directly to GPIO pins without using pull-up or pull-down resistors. Those can be external resistors (typically 1kΩ or 10kΩ), or you can use the internal pull-up resistors, as we have done here.
Loop function
The loop()
function is where the main logic of the program resides. It calls the switch_led()
function for each switch and LED pair, updating the LEDs based on the switch states. We also include a small delay of 100 milliseconds to debounce the switches and prevent rapid toggling.
void loop() { switch_led(S1, LED1); switch_led(S2, LED2); switch_led(S3, LED3); switch_led(S4, LED4); delay(100); }
This code allows us to control the state of 4 LEDs using a DIP switch. The LEDs will mirror the state of the corresponding switches, turning on when the switch is closed and turning off when the switch is open.
Refined Code Example
The code presented above works fine but will be cumbersome to use if we use all 8 switches or more to control more LEDs or other devices. We would need to define individual constants for each of them, which is tedious.
The following code uses arrays and loops instead, which will make it much easier to extend the code to more switches and LEDs. It works exactly the same but is much shorter
// Control 4 LEDs with a DIP switch const int LEDS[] = { 13, 12, 11, 10 }; const int SWITCHES[] = { 7, 6, 5, 4 }; const int N = 4; void switch_led(int switchPin, int ledPin) { int state = digitalRead(switchPin); digitalWrite(ledPin, state == HIGH ? LOW : HIGH); } void setup() { for (int i = 0; i < N; i++) { pinMode(LEDS[i], OUTPUT); pinMode(SWITCHES[i], INPUT_PULLUP); } } void loop() { for (int i = 0; i < N; i++) { switch_led(SWITCHES[i], LEDS[i]); } delay(100); }
Constants and Variables
We start by defining two arrays, LEDS
and SWITCHES
, which hold the pin numbers for the LEDs and switches, respectively. The constant N
is set to 4, representing the number of LEDs and switches.
const int LEDS[] = { 13, 12, 11, 10 }; const int SWITCHES[] = { 7, 6, 5, 4 }; const int N = 4;
Switch LED function
The switch_led()
function is the same as before. It takes two parameters: switchPin
and ledPin
and sets the state of the LED connected depending on the state of the switch.
void switch_led(int switchPin, int ledPin) { int state = digitalRead(switchPin); digitalWrite(ledPin, state == HIGH ? LOW : HIGH); }
Setup function
In the setup()
function, we iterate over the LEDS
and SWITCHES
arrays to set the pin modes. The LED pins are set as OUTPUT, as we will be writing to them, and the switch pins are set as INPUT_PULLUP, enabling the internal pull-up resistors.
void setup() { for (int i = 0; i < N; i++) { pinMode(LEDS[i], OUTPUT); pinMode(SWITCHES[i], INPUT_PULLUP); } }
Loop function
In the loop()
function, we iterate over the LEDS
and SWITCHES
arrays again. For each iteration, we call the switch_led()
function to update the state of the corresponding LED based on the state of the switch. We then introduce a delay of 100ms before the next iteration.
void loop() { for (int i = 0; i < N; i++) { switch_led(SWITCHES[i], LEDS[i]); } delay(100); }
And that’s it. Now you know how to use a DIP switch.
Frequently Asked Questions
I have compiled a list of the most frequently asked questions about projects using Arduino and DIP switches. If you have any other questions, please post them in the comments section.
1) What are DIP Switches?
DIP stands for Dual-In-Line Package. A DIP switch contains a set of switches arranged and packed in a single package. The switches help to handle 8 GPIO inputs easily.
You can use DIP switches to program Arduino UNO to set different modes. You can also control the LEDs or relays using DIP switches without any MCUs.
The DIP switch in the image above consists of 8 buttons.
2) What is a 4-pin DIP switch?
A 4-Pin DIP switch consists of 4 independent switches in one package. Here is one such example of a 4-pin DIP switch.
3) How much current can a DIP switch handle?
The DIP switch can handle currents in the mA range. Here is one example datasheet of a DIP switch, which can handle up to 100 mA of continuous current. The current rating will be higher for a continuous current. For a switching current, the ratings will be slightly lower.
4) How do you tell whether a DIP switch is ON or OFF?
The DIP switch is ON when the slider is near the ON marker on the switch. In the image below, the DIP switch has eight switches in it. Switches 1 and 2 are ON. The remaining eight switches are OFF.
Conclusion
In this article, I explained the basic concepts of a DIP switch. I am hoping that the article was easy to follow.
We have covered different types of DIP switches available. We also reviewed the specifications one should look for while deciding the right choice of the DIP switch.
I hope you enjoyed the article. If you have any suggestions to improve the article, I will be glad to hear them.
I am Puneeth. I love tinkering with open-source projects, Arduino, ESP32, Pi and more. I have worked with many different Arduino boards and currently I am exploring, Arduino powered LoRa, Power line communication and IoT.