4
\$\begingroup\$

I'm trying to design a circuit that will detect if the normally closed switch is opened. The internal pull-up resistors in the microcontroller will pull the signal high, so the signal pin will always detect a low GND signal when the switch is closed. When the switch is opened, the signal pin will detect a high 3.3V.

Does this configuration looks right? Is there any cons or better way to design this?

enter image description here

\$\endgroup\$

4 Answers 4

11
\$\begingroup\$

What you have is a standard way to connect a small switch to a nearby MCU.

However, in some situations, it is better to have an external pull-up, or pull-down, resistor:

  • Larger, higher-current switches need a higher switch current that's above the rated contact wetting current.

  • Further away switches need a higher switch current to reduce the connection's susceptibility to noise. Go further still and it may need filter capacitors or a different interface circuit altogether.

  • In testing and fault-finding, the switch voltage cannot be measured unless the MCU contains running software that enables the pull-up.

You will have to assess all these requirements for your own situation.

Sometimes switches, such as DIP switches, aren't changed during system operation. Those that are will need their input level debounced by your MCU software. This removes the multiple switchings caused by contacts chattering together and apart when the switch opens and closes.

\$\endgroup\$
4
  • \$\begingroup\$ External resistor as in like a 100 ohm resistor at the output? Or do you mean using an external pull up resistor instead of the internal one? Thanks! \$\endgroup\$
    – cy1125
    Commented Feb 21, 2022 at 21:12
  • \$\begingroup\$ @cy1125 External pull up but not 100Ω. Typical values are more like 1kΩ-10kΩ with 4.7kΩ being very commonly used in the PC industry. \$\endgroup\$
    – slebetman
    Commented Feb 22, 2022 at 4:07
  • \$\begingroup\$ If I use the external pull up set up, should I use a 3.3V or 5V? I have both 3.3V and 5V power on the board. The microcontroller is powered by 3.3V, and those GPIO pins are 5V tolerant. I think 5V will be a stronger pull up but I'm not sure if adding a resistor divider will be a good option. \$\endgroup\$
    – cy1125
    Commented Feb 23, 2022 at 17:22
  • \$\begingroup\$ @cy1125, it's impossible to answer because your question gives no details. Switch could be a tiny DIL on a board, could be up hundred foot of cable on a submarine. Please edit your question (not comments) and add decent details, lots of them. \$\endgroup\$
    – TonyM
    Commented Feb 23, 2022 at 19:03
10
\$\begingroup\$

It looks correct and should work.

There might be cons and better ways to do this but there is no context to what this should be compared. If done with a switch or button on a PCB it will work, but if the switch is connected with 100m of unshielded wiring in a factory filled with arc furnaces then in it is sure not to work.

\$\endgroup\$
5
\$\begingroup\$

As a quick circuit this will likely work, however if you are using a switch which lacks precious metal contacts then it may not work reliably in some environments unless you pass mA through the switch. It’s a design task to match the contact type to the application.

The switch is connected directly to the MCU, so if ESD (say from a finger operating the switch) finds its way to the MCU pin then it could cause damage or disrupt the program.

If the switch is some distance away, and particularly if the connections are open to mis-wiring by the user you may wish to add protection against damage and electrical noise. It’s part of the design process to anticipate sub-optimal possibilities and to decide whether to allow a failure (soft or hard failure) or to add protection. For example, you may specify that the switch wires can be connected to +/-15V continuous without damage, but the unit cannot be expected to survive a direct connection to the mains. Or maybe you’d prefer your product didn’t false operate whenever a nearby switch or relay operates. Some of that can be done in firmware but if the noise is too high amplitude it may cause improper operation.

If you look at professional consumer product designs such as automotive or garage door openers you’ll often find that even cheap products have a bunch of added components, because the cost of one field failure can eat up the profits from many, many units that don’t fail. Users will generally find all the wrong ways to wire things up that you can think of, and often a few more. Series resistance, TVS diodes, etc., can be very helpful.

\$\endgroup\$
5
\$\begingroup\$

Two things. First, the pull-up is somewhat weak if there is a trace running to it. It will be prone to noise pickup when the switch is opened.

You can reduce the sensitivity of the open node by adding some small capacitance in parallel with the switch. 30-100pF or so would do fine.

Second, if extreme low power is your goal, the pull-up will use power all the time, except when the switch is pressed. You could mitigate this by disabling the pull-up until you poll the pin.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.