Article count:1750 Read by:2410977

Featured Content
Account Entry

It is crucial to master these knowledge points when connecting MCU to DIP switch!

Latest update time:2024-01-03
    Reads:
Q
A
&

Q: What is the basic principle of DIP switch and MCU interface?

Connecting a microcontroller to a microcontroller using a dual-in-line package (DIP) switch is a common application. These switches, commonly known as "DIPs," are used in a variety of designs, from traditional DIPs suitable for breadboard prototyping to surface-mount " piano " types to rotary switches for easy hexadecimal reading .

In this article, we will take a closer look at rotary switches and explore how to integrate them into our microcontroller designs. The techniques presented in this article are generally applicable to all microcontroller designs.

Start with the rules

Let's start with a simple rule: no floating inputs allowed . A floating input occurs when a microcontroller pin is configured as an input but is otherwise 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.

This is highly undesirable because a floating pin could be interpreted as a logic high, or at other times, a logic low. From a troubleshooting perspective, the microcontroller's response will have no rhyme or reason. The pin is susceptible to noise and will often loosely follow the values ​​of adjacent microcontroller pins.
The solution is to add a pull-down resistor, as shown in the diagram 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 the switch bounce for the moment, the microcontroller will have a clean input.

Optimization solutions

Modern microcontrollers are designed for this type of interface. Almost all microcontrollers have an I/O section with internal resistors that can pull the I/O pins up or down. This is desirable because the switch can be connected directly to the microcontroller, eliminating the need for external resistors.

Technical Tips:


Some microcontrollers have 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 referred to as "weak pull-ups" and consume tens to hundreds of uA. This is equivalent to connecting an external pull-up resistor with a value between 15 kΩ and 150 kΩ.

An example for Arduino is:
pinMode (SW_PIN_D0 INPUT_PULLUP);
This schematic shows one way to interface a microcontroller with a switch. Although this example uses a rotary DIP switch, the design is applicable to all switches. Observations:
  • The internal pull-up resistor is enabled using the microcontroller's special function registers
  • The common element of the switch is connected to ground

Technical tips:


The schematic shows an optional series resistor and an optional multiplexing section. This allows an I/O pin to perform double duty. For example, this narrow-width interface can be used to read a switch and also drive D3 to D0 on an LCD display. This can be desirable, as it potentially reduces microcontroller pin count and overall PCB size at the expense of circuit and code complexity.

Before we conclude, let's look at the physical switch codes associated with rotary DIP switches. Representative HEX codes are taken from an Omron datasheet . Observe the "BCD hexadecimal" and "BCD hexadecimal complement" switch configurations for the A6A-16R and A6A-16C models.

Looking back at the microcontroller schematic and its associated pull-up resistors, we see an inversion. For example, when the switch is in the 1 position, there are three positive logic inputs at positions 2, 4, and 8. Meanwhile, if the complementary switch is installed, there is a positive logic (active-high) input corresponding to a 1 signal.
From a programming perspective, this difference in the physical switch code is insignificant. A simple bit-invert instruction will make them equal. From a troubleshooting or educational perspective, the complementary version may be easier to understand because it results in a positive logic value at the microcontroller pin.

This month's lucky draw password: Circuit Design



Finally, if you like this article, please share it with more friends ! Remember to give it a like !


Tips Click the menu Design Support: Engineer Tips for more engineer tips

Click "Read More" below to see more


Let me know you 're watching .


Latest articles about

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号