Button on the microcontroller, what you need to know!
by Edi · 15/05/2023
This must be observed when using push-buttons
It sounds very easy to connect a push button to a microcontroller, but there are a few things to consider if you want to do it right. These improvements can be applied to any mechanical switch or button, such as a door or window contact.
Use pull-up or pull-down resistors to ensure that the input is set to a known state when the button is not pressed.
Button debouncing ensures that only a single stable pulse edge is generated.
Finally, we want to determine whether the button is short or long pressed and respond accordingly. Let's now go through the individual points in sequence.
If you want to find out more about pull-up and pull-down resistors, take a look here.
Structure of mechanical push-buttons
The basic operation of a mechanical pushbutton is based on a spring pressing a movable contact cap against a fixed contact to close the circuit. Mechanical pushbuttons come in many different designs and sizes, from tiny micro pushbuttons to large industrial switches. However, the basic design usually remains similar, regardless of the size or shape of the button.
For our microcontroller projects, we usually use the tactile buttons, which have four connections. This gives us four options for connecting the button, two of which are mirrored.
The button noise
The noise on a button is caused by electromagnetic interference (EMI), which can be caused by neighboring electronic components or environmental conditions.
Let's now do a practical example where we connect the button directly to an input of our Arduino UNO. This allows us to see the unwanted effect of the button noise. The serial monitor now outputs 0 but also 1 and every 1 is a false input that would trigger a function that is not desired.
const byte buttonPin = 5;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
}
void loop() {
Serial.println(digitalRead(buttonPin));
}
Hardware solution
The solution to this problem is a pull-up or pull-down resistor, which sets the input to HIGH for a pull-up resistor and to LOW for a pull-down resistor.
Software solution
const byte buttonPin = 5; //D1
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
Serial.println(digitalRead(buttonPin));
}
Debounce the button
Push-buttons do not switch directly to the next state, but tend to switch back and forth a few times, which is also known as bouncing. This bouncing usually only lasts a very short time, approx. 2-5 milliseconds, but this depends on many factors and can vary greatly. If the environment is dusty or the buttons are old and dirty, 30 milliseconds is still a practical value. Each bounce can be interpreted by the microcontroller as a button press due to its high speed.
#include //Bounce2 von Thomas O Fredericks V2.71
const byte buttonPin = 5; //D1
Bounce2::Button button = Bounce2::Button();
void setup() {
Serial.begin(115200);
button.attach(buttonPin, INPUT_PULLUP);
button.interval(30); // 30 ms zum Entprellen
button.setPressedState(LOW); // Input = LOW bei gedrückten Taster
}
void loop() {
button.update(); // Bounce Instance update
if ( button.pressed() ) {
Serial.println("Taster ist gedrückt");
}
}