The first question that comes up: where do I hook up the switch wires to the microcontroller?

Answer: We'll show you below!

The simplest switch has two terminals which are open when switched one way, and closed when switched the other way. Think of a light switch as an example but they come much smaller for our use.

Using wires, here is a vintage toggle switch (Radio Shack, 1970s) and how the wiring might look:

components_switch.png

components_toggle2.jpg

When the switch is in one position, the contacts are not connected ("off" or "open circuit") and in the other position the circuit is connected ("on" or "closed circuit").

This type of switch is called a normally open single pole single throw switch. More complicated switches are possible but let's understand the basic switch first.

Hookup

You connect both of the terminals to your microcontroller project. Here is the tip on which ones. Select a data pin (digital or analog are ok, it must be a pin that can be read as a digital data pin for input by code).

Connect the other side of the switch to the circuit ground (often marked GND).

Switches are symmetric, so it doesnt matter which switch-pin goes to ground and which switch-pin goes to data

components_hookup2.png

components_hookup1a.jpg

In the drawing, ignore the resistor between the microprocessor pin and the positive voltage - that resistor is internal to the microcontroller board, so you don't need to wire it up yourself. The resistor will connect from the data pin you're using to the internal logic power voltage which we label Vcc. Depending on your microcontroller Vcc may be connected to 5V (Arduino UNO) or 3.3V (which it is on the Circuit Playground) We'll get to that in a second.

Why connect the second connection made to ground? Let's look at the diagram.

Pull "high" or "up" Resistors

The voltage at the micro pins is normally "pulled high" by the resistor when the switch is open due to input pins being high resistance. When the switch closes, the micro pin doesn't see Vcc anymore because the resistor is between it and Vcc, it sees the direct short to ground. 

That circuit gives us unambiguous switching between Vcc voltage and zero/ground voltage depending on the switch.

Without the "phantom resistor", the pin would not be connected to anything with the switch open. With electronics, reading an unconnected pin may cause a reading that is undetermined, which is not good in digital on/off readings.

OK, so we know the hookup and know there should be some sort of resistor in there (what I called a phantom, still coming). 

How about those 4 legged breadboard switches. They work fine if you know what they do. See the next page.

This guide was first published on Aug 15, 2018. It was last updated on Aug 15, 2018.

This page (Hook Up Your Switch) was last updated on Nov 06, 2020.