title: "ez_switch_lib: Professional Switch Management Library for Arduino and ESP32" description: "Elevate Input management for all types of buttons and switches with a Library that efficiently handles Debounce, Circuit wiring, and Non-blocking operations."
Introduction: The Complexity Behind Simplicity
In the realm of Embedded Systems, a "switch" might appear to be the most fundamental component. However, for both novice and experienced developers, switches often lead to persistent, vexing problems, whether it's dealing with signal noise or unintended repeated presses.
"When you blend with wrong energy, an explosion is sure to occur. Be mindful and switch lanes when the signs change. What is true will ultimately be revealed..." — T.F. Hodge
"Darkness isn't an issue if you are in control of the switch..." — Aniekee Tochukwu Ezekiel
These quotes reflect a truth in hardware control programming: if we cannot manage the Input (switches) effectively, the entire system can malfunction. If you're looking for a way to manage a wide variety of switches, both in type and operational characteristics, or if you need a toolkit that helps make your code cleaner and more organized, <ez_switch_lib> is the answer you're looking for.
For those who wish to build a deeper understanding of switches, you can refer to these articles:
What is <ez_switch_lib> and What Problems Does It Solve?
At its core, <ez_switch_lib> is a software library that enhances the capability to read various types of switch values and automatically handles the "Switch Bouncing" problem (mechanical signal fluctuations).
In engineering, when a button is pressed, the metal contacts don't immediately connect perfectly. Instead, they "bounce" for a few milliseconds. The high speed of microcontrollers perceives this bounce as dozens of repeated button presses. <ez_switch_lib> acts as a software layer that filters out this noise, allowing developers to focus on the main program logic without worrying about managing input hardware.
Furthermore, this Library offers flexibility in Circuit Design, supporting both Internal Pull-up (no external resistor needed) and External Pull-down configurations. This ensures that hardware changes do not affect the core code structure.
Core Technical Features
This Library is designed based on Object-Oriented Programming principles and efficient memory management:
- Cross-Platform Support: Works with both Arduino (AVR) and ESP32 families, which have higher clock speeds.
- Dynamic Memory Allocation: Allocates memory based on the actual number of switches used, saving RAM in large projects.
- Concurrent Support for Multiple Switch Types: Allows free combination of both Button (momentary press) and Toggle (latching switch) types within the same project.
- Dual Wiring Support: Supports both Active Low (Pull-up) and Active High (Pull-down) circuits, eliminating the need for developers to write their own logic switching code.
- Automatic Pin Linking: Can directly "link" Digital Output pins to a switch, allowing LEDs or relays to operate automatically according to the button's status without needing explicit commands in the
loopfunction. - Non-blocking Execution: The read functions are Non-blocking (using
millis()instead ofdelay()), allowing the system to continue running other tasks smoothly without lag. - Interrupt Service Routine (ISR) Support: Supports operation with Interrupts, ensuring maximum responsiveness to button presses, even when the main program is heavily loaded.
- Error Trapping System: Includes functions for checking errors in pin linking or value reading, simplifying debugging.
- Serial Monitor Reporting: Supports reporting operational status via Serial Monitor to monitor the behavior of each switch.
In-depth Library Operation (Engineering Logic)
In depth, <ez_switch_lib> operates by creating a data structure (Structure/Class) for each switch added. When a read function is called, the Library performs the following:
- Reads the electrical value from the specified GPIO pin.
- Compares with the previous timestamp to check if the configured Debounce Time has elapsed.
- Processes the switch type: If it's a Button, it checks for a "Transition" (from High to Low or vice versa). If it's a Toggle, it focuses on State Change.
- Updates the output status: If linking has been configured, the Library automatically sends the value to the Output pin immediately.
Download and Installation
You can access all resources, including the detailed User Guide, a Crib Sheet for command summaries, or example code, on GitHub:
- Download All Files: GitHub - eazy-switch-library
- User Manual: User Guide (PDF)
- Quick Start Guide: Quick Start Guide (PDF)
- Command Summary Sheet: Crib Sheet (PDF)
Installation Method:
To ensure ez_switch_lib functions correctly, make sure to copy the main files (ez_switch_lib.h, ez_switch_lib.cpp, and keywords.txt) into a folder named ez_switch_lib within your computer's Arduino/libraries directory.
Note from the Engineer: Effective switch management is the foundation of a stable User Interface. <ez_switch_lib> is designed to minimize this complexity, allowing you to fully dedicate your time to innovating critical aspects of your project.