How to connect and use tact switches with an Arduino
All my content is free.
If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.
Select amount then click the “Donate” button.
At some point you will have to connect and use switches to control stuff in your Arduino projects.
Here are some basics to successfully use switches and make sure they respond correctly.
Most switches are simple mechanical devices that basically make a connection between two inputs.
In this tutorial we will be using a standard tact switch.
A tact switch is a momentary mechanical switch that makes a connection when the switch is pressed.
There are two things we can detect with an Arduino:
- Detect when the switch is pressed or not pressed
- Detect if the switch is HIGH (5V) or LOW (Ground) in either of those 2 states
Why do we need a resistor when using a switch?
A typical tact switch has 4 pins, but has you can see from the picture below some are connected together which means that they basically have 2 pins.
You connect one of those either to Ground (LOW) or 5V (HIGH) and connect the other one to an Arduino Digital pin.
When the switch is pressed it makes the connection between those two pins and connects the Arduino Digital pin to either Ground or 5V which we can detect in code and do the appropriate action.
But when the switch is not being pressed it enters a state of “Floating”.
Floating is when the Arduino Digital pin is neither High or Low and results in noise.
So to fix this we connect the tact switch pin that is connected to the Arduino Digital pin to either Ground or 5V depending on what we are trying to read when the switch is pressed.
So if we want to read LOW when the switch is pressed we would connect the 5V, which will results in the switch being “Pulled UP” when the switch is not being pressed and the Arduino would read the Digital pin HIGH without any noise.
Now we can’t just go ahead and connect that pin directly to ground or 5V since that would create a short circuit when the switch is pressed, this is why we need to use a Resistor.
The Resistor prevents the short circuit by limiting the amount of current to flow when the switch is pressed.
Resistor value and Arduino integrated PullUp resistors
In your projects you can either use external resistors or the ones available inside the Arduino.
The Arduino UNO has PullUp resistors available on each one of the Digital Pins. These are activated inside your code and have a value between 20k and 50k (average of around 34k).
Notice that the Arduino UNO only has PullUp resistors and no PullDown resistors, which mean that using the Arduino resistors only enables you to make the state of the switch when it’s not pressed to HIGH (PullUp), which is not a big problem since most of the time you want to read HIGH when not pressed and LOW when the switch is pressed.
To my knowledge only the Arduino ZERO has both PullUp and PullDown resistors.
If you project need to read LOW when the switch is not pressed then you will need to use external resistors, and a value between 20k and 50k will provide the same results as if using the Arduino resistors.
In this tutorial we will look at 4 different connections scenarios:
- Demonstrate the Floating state
- Using a resistor to PullUp
- Using the Arduino integrated resistor to PullUp
- Using a resistor to PullDown
To visualize what happens when I press the tact switch, I’m using a MAX7219 LED Matrix.
The Matrix will display a ZERO or a FIVE (Ground or 5V) when the switch is pressed.
If the scenario is a PULLUP then the matrix will display a FIVE when the switch is not pressed and a ZERO when it is pressed.
If the scenario is a PULLDOWN then it will do the opposite.
You can find links to the library used to control the LED Matrix at the bottom of this page.
As always don’t forget to watch the tutorial video for more information.
Copy and Paste the above code/sketch in your Arduino IDE software.
Download the Parola library here: