Fork me on GitHub

Project Notes

#306 KeyboardMatrixModule/SwitchInterrupts

Demo interrupt-driven switch input on a simple pushbutton/LED matrix module and how to use the EnableInterrupt library.



See LEAP#303 KeyboardMatrixModule for details of the module I am testing here.

It features 4 independent Push Buttons (S1-4).

4 Independent Push Buttons

There are 4 independant (red) pushbuttons S1-4.

With VCC connected to power and GND grounded, pins S1-4 are normally pulled HIGH with a 10kΩ built-in resistor. When pressed, the corresponding button pin will pull LOW.

Test Program

The SwitchInterrupts.ino sketch demonstrates handling key presses with interrupts.

With four switches, that means we need at least 4 interrupts. There are only two external interrupt pins on the ATmega168/328 (ie, in the Arduino Uno/Nano/Duemilanove), INT0 and INT1, and they are mapped to Arduino pins 2 and 3.

To get more external interrupts, you could instead use a Mega2560 for example, which supports external interrupts on digital GPIO pins 2, 3, 18, 19, 20, 21.

Another approach (the one I’ll use here) is to use pin change interrupts that can be enabled on any or all 20 of the pins of an ATmega168/328-based Arduinos. The EnableInterrupt library is a very easy way to do this.

The test script simply:

  • sets up falling-edge interrupts on the four switch pins (normally pulled high)
  • the interrupt routine records which switch was pressed
  • the main loop looks for a switch press, prints to the serial console and clears the switch press flag

Test Run

Here’s an example of the console output, with me pressing all the switches from S1 to S4:






Credits and References

Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.