What is the Arduino Modulino Buttons module?
The Arduino Modulino Buttons module is an integral component of Arduino’s latest Plug and Make Kit. It provides a conventional user interface consisting of three pushbuttons and three software-controlled LEDs as shown in Figure 1.
The Modulino ecosystem is known for its consistent user experience from a hardware and a software perspective. This is facilitated by the 32-bit STMicroelectronics ARM microcontroller installed on each Modulino. This microcontroller offloads some of the responsibility from the Arduino UNO R4 WiFi to the Modulino. This allows the Arduino programmers to implement a standardized communications protocol over the Qwiic network.
Figure 1: Image of the Arduino Modulino board featuring three pushbuttons and three independently controlled LEDs. The 32-bit STM ARM microcontroller is visible in the lower-left corner. This Modulino is bolted to the Plug and Make Kit’s base.
Tech Tip: Qwiic is one of several competing formats used for easy connection of microcontroller-related prototyping breakout boards. You may also encounter the (conditionally cross-compatible) Stemma QT or even MikroElektronika Click modules. All are designed to provide the developer breadboard-free connectivity, which is beneficial especially for small-pitch surface mount devices.
Software description of the Arduino Modulino Buttons
Like all Arduino Modulino boards, the Buttons is implemented as an object in the C++ object-oriented programming environment. A simple code listing is shown below. Observe that a buttons object is constructed. Three methods are available including:
-
update( ); This method is a getter that initiates a data transfer from the Modulino to the WiFi. It returns a bool indicating that a button transition has occurred. The button data (status) is held inside the buttons object. And must be accessed using the is Pressed( ) method.
-
isPressed( ); This method allows the programmer to access the individual button data from within the buttons object. Button A is at index 0.
-
setLeds( ); This method is a setter that pushes a value to the Button module’s red LEDs. It takes three parameters corresponding to the A, B, and C LEDs respectively.
#include <Modulino.h>
ModulinoButtons buttons; // Constructor
void setup( ) {
Modulino.begin( );
buttons.begin( );
}
void loop( ) {
if (buttons.update( )) { // Get data from the Modulino and then check for a change
bool btn_A = buttons.isPressed(0); // Pull data from object
bool btn_B = buttons.isPressed(1);
bool btn_C = buttons.isPressed(2);
buttons.setLeds(btn_A, btn_B, btn_C); // Send data to the LEDs
}
}
Tech Tip: The update( ) method is a clever way to reduce traffic on the Qwiic network. Since it returns a bool indicating pushbutton activity, the programmer knows when to act. For example, in the previous code listing, the LEDs will only be updated when the buttons change. This simple fact becomes increasingly important as more modules are added to the I2C network. Remember that time is a precious resource. You may run out of bandwidth which is an essential resource for a responsive real-time system.
How to debounce the Modulino Buttons
Bouncing contacts are a challenging physical reality for microcontroller programmers, as switch and relay contacts can bounce like a ball before settling down. Electrically, this bouncing appears as a series of rapid transitions (spikes). Fast digital circuits will interpret the bouncing signal as multiple switch transitions. For example, a routine to count the number of times a button is pressed may record dozens of transitions for each press.
The Modulino Buzzer programmers have taken care of the debouncing for us. You can verify this statement using an oscilloscope to monitor an I/O pin that toggles each time a button change is detected. In this application, the oscilloscope shows a clean signal with no bounce. There is a 0.5 to 1 ms delay from when the user pressed the button (as measured at the pushbutton itself) to when the pin toggles.
#include <Modulino.h>
ModulinoButtons buttons; // Constructor
void setup( ) {
Modulino.begin();
buttons.begin();
pinMode(2, OUTPUT);
}
void loop( ) {
static bool X;
if (buttons.update( )) { // Get data from the Modulino and then check for a change
X = !X;
digitalWrite(2, X);
}
}
Tech Tip: Current limiting series resistors are required for each of the Modulino Buttons LEDs.
Pullup resistors are required for each switch. These pullup resistors may be external or internal to the microcontroller such as when the pullup-resistor peripheral is used. External resistors allow greater flexibility at the expense of added parts count. Consult the Arduino schematic for further study.
Communications protocol
Many of the Arduino Modulino products include an STM ARM processor as seen in Figure 1. With full control over both the Arduino Library and the Modulino’s ARM microcontroller, the Arduino programmers have produced a unified communication protocol. I leave it to you to reverse engineer the frame. The technique is similar to that used in the Modulino Buzzer tutorial.
Parting thoughts
This brief article scratches the surface when it comes to designing a responsive user interface. We have explored the mechanics of the Buttons module but not the fine details such as when and how to work around blocking code. For example, it’s not trivial to respond to a button press when a delay( ) function is active or while the microcontroller is busy performing an important task.
Let us know if you have any suggestions or questions by leaving a comment in the space below.
Best wishes,
APDahlen
Related Information
Please follow these links to related and useful information:
About this author
Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.