Fundamentals of DIP Switch to Microcontroller Interfacing

Interfacing a microcontroller to a Dual Inline Package (DIP) switch is a common application. Switches with the colloquial name of “DIP” are available in a wide range of design from the traditional DIP suitable for breadboard prototyping to a surface mount “piano” type, to rotary switches with easy-to-read hex values.

|image| image |image

In this post we will take a closer look at the rotary switch and explore how it may be integrated into our microcontroller designs. The techniques presented in this post are generally applicable to all microcontroller designs.

Start with the Rules

Let’s begin with a simple rule: no floating inputs are allowed. A floating input occurs when a microcontroller pin is configured as an input, but otherwise left unconnected. An example is shown here. When the switch is closed, the associated pin is tied to the positive rail. When the switch is open, the pin is floating.

image

This is highly undesirable as a floating pin may be interpreted as a logic high or, at other times, a logic low. From a troubleshooting standpoint, there will be no rhyme or reason to the microcontroller’s response. The pin is susceptible to noise and will often loosely follow the value of adjacent microcontroller pins.

The solution is to add a pull-down resistor as shown below. With this small change, the microcontroller’s pin will be pulled up to the rail when the switch is closed, or it will be pulled down to ground when the switch is open. Ignoring switch bounce for the moment, the microcontroller will have a clean input.

image

Optimized Solution

The modern microcontroller was built for this type of interfacing. Nearly all microcontrollers feature I/O sections with internal resistors to either pull up or pull down against an I/O pin. This is desirable as the switch may be connected directly to the microcontroller thereby eliminating the need for external resistors.

Tech Tip: Some microcontrollers feature both pull-up and pull-down resistors. Others will have only one type, with the pull-up configuration being more common. These peripherals are often called “weak pull-up” and consume tens to hundreds of uA. This is equivalent to connecting an external pull resistor with a value somewhere between 15 kΩ and 150 kΩ.

An Arduino example is:

pinMode(SW_PIN_D0, INPUT_PULLUP);

This schematic presents one way to interface a microcontroller to a switch. While this example features a rotary DIP switch, the design is applicable to all switches. Observe:

  • internal pull up resistors are enabled using the microcontroller’s special function register(s)

  • the common element for the switch is connected to ground

Schematic of selector switch with internal microcontroller weak pull-up resistors.

Tech Tip: An optional set of series resistors are shown in the schematic along with an optional multiplexed section. This allows the I/O pins to perform double duty. For example, this nibble-wide interface could be used to read the switch as well as drive D3 to D0 of an LCD display. This may be desirable as it has the potential to reduce the microcontroller pin count and overall size of the PCB at the expense of circuit and code complexity.

Before we conclude, let’s look at the physical switch code associated with the rotary DIP switch. A representative HEX code is taken from this Omron datasheet. Observe that there are two switch configurations including the “BCD Hexadecimal code” and “BCD Hexadecimal complement code” corresponding to models A6A-16R and A6A-16C respectively.

Chart showing connections for an Omron Hex rotary selector switch.

Looking back to the microcontroller schematic with its associated pull-up resistors, we see an inversion. For example, when the switch is in the 1 position, there will be three positive-logic inputs on positions 2 + 4 + 8. Meanwhile, if a complementary switch were installed, there would be a single positive-logic (active high) input corresponding to the 1 signal.

From a programming perspective, this difference in this physical switch code is inconsequential. A simple bit inversion instruction will make them equal. From a troubleshooting or education perspective, the complementary version may be easier to understand as it results in positive logic values being present on the pins of the microcontroller.

Best Wishes,

APDahlen