Reading Vespa Button
Introduction
Got your Vespa and want to know how to use it? Don't worry, we're here to help you with these first steps and to show how to use the basic functions of the library developed for it.
In this tutorial you will see how to read the built-in button on the Vespa and how it can be applied in practice.
List of Materials
Direct Activation
As a first basic example of using the button, this will make the Vespa's internal LED L turn on every time the button is pressed and turn off when the button is released.
Code
For this tutorial, we will not be using the Vespa library. However, follow the steps in the tutorial First Steps with the Vespa to learn how configure your IDE to program it.
Once done, upload the following code
Understanding the Code
In the global declarations at the beginning of the code, we only declarate the constants BUTTON_PIN
and LED_PIN
, which store the GPIO connected to the internal Vespa button and LED.
In the code configuration (function void setup()
), we just configure the pin connected to the button as an input with the internal "pull-up" resistor activated (INPUT_PULLUP
) and we configure the pin connected to the LED as an output of the board, initially with the initial low logic level ("LOW").
In the code loop (function void loop()
), we have the declaration of the variable button_read
that receives the digital reading of the pin connected to the button. Next, we check if the button was pressed by the if(button_read == LOW)
condition. When the button is pressed with a "pull-up" resistor in its circuit, the logic level read is low ("LOW"). Taking this into account, when the button is pressed, the condition created is true and LED L is then turned on. Otherwise, if the button has not been pressed, the previously created if
condition will be false, then the else
condition will be true and LED L will be off.
What Must Happen
After uploading the code to the board, see that LED L will light up when the button is pressed and will turn off after releasing the button, as in the GIF below.
Activation With Memory
In this second example, we will make the Vespa's LED L turn on and off when the button is pressed, maintaining its state until the button is pressed again.
Code
To do this, load the following code onto your Vespa.
Understanding the Code
In the global declarations at the beginning of this project's code, in addition to the variables that store the pins connected to the button and the LED, we also have the declaration of the auxiliary boolean variable light
, which will be responsible for turning the LED on or off when the button is pressed.
The code settings (function void setup()
) are exactly the same as the previous project, just configuring the pins as input and output.
When looping through the code, we check with the help of a debounce (delay()
of 30 milliseconds to filter noise) if the button was pressed through the condition if (digitalRead(BUTTON_PIN) == LOW)
. If the button is pressed, the conditions will be true by the debounce and the variable light
will have its logical state inverted. That is, as it is initially false, it will be true after the first button press, and so on. Once this is done, it is checked if the variable light
is true and, if so, LED L will be turned on. If it is false, LED L will be off. Finally, the empty while(digitalRead(BUTTON_PIN) == LOW)
condition ensures that the code does not execute anything while the button is pressed. This prevents the variable light
, and consequently the LED, from continuously changing state while the button is pressed.
What Must Happen
After uploading the code to the board, press the button and release it. You will see that LED L will stay turned on or off until the button is pressed and released again, as in the following GIF.
Going Beyond
The LED L controlled in the examples above is just a demonstration of a possible action when the Vespa button is pressed, but the possibilities are not limited to this. With this button, it is possible to control the sending of MQTT messages over Wi-Fi to save sensor readings to an IoT dashboard, for example, or even reverse the rotation of a DC motor that is being controlled by the Vespa. Anyway, the possibilities are endless with such a simple board functionality.
In addition, we also have several other tutorials demonstrating all the features of Vespa.
Conclusion
In this tutorial we saw how to configure and read the GPIO connected to the integrated Vespa button in simple examples of LED L control.