How to interface multiple switches using one interrupt

Keywords : Arduino Interrupts, Multiple buttons for one interrupt



Problem 

If you have an application where you need more interrupts than available there are couple of options for you.
ex:
  • Pin Change Interrupts
  • A Hardware design using diodes (Suitable for some of the applications)
  • go for a board/micro-controller with higher no of interrupts

In this post we will discuss how to utilize a one interrupt to check multiple inputs using diodes. (here we will interface multiple switches using one interrupt)

Apart from this you may even utilize Analog inputs along with using resistor dividers to obtain the same function. We will be discussing that in another example.


Interfacing multiple switches single Interrupt pin
Interfacing multiple switches to a single Interrupt pin - Schematic

Interfacing multiple switches single Interrupt pin connections
Interfacing multiple switches to a single Interrupt pin - Connections

As per the schematic All the switches and the interrupt pin are pulled down. When a switch is pressed It will bring up the respective pin to +5V. Also it will bring the interrupt pin voltage to the high state. This will happen for each and every switch. When Coding you can check the respective pin attached to the button inside the interrupt.  (ex: Diodes 1N4001-1N4007, 1N4148 etc) Once interrupt pin receive the signal, program will be diverted to the interrupt function.

Note : If your application is good enough with Internal Pull-up resistors you can do it that way. In that case you the diodes other way round and you will need to change the interrupt mode. (LOW, FALLING etc) 

Simulation




<Arduino Code>
const byte BlinkLED = A0;   //To represent main task

const byte LED_Red = A1;      //Each LED to represent push buttons
const byte LED_Green = A2;
const byte LED_Blue = A3;

const byte Button_Red = 8;      //Initialize pins connected to P/Bs
const byte Button_Green = 9;
const byte Button_Blue = 10;

const byte interruptPin = 2;      //Interrupt pin

void setup() {
  pinMode(LED_Red, OUTPUT); //Declaring Outputs, No need to declare inputs
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Blue, OUTPUT);

  pinMode(interruptPin, INPUT);   //Define Interrupt Pin as input, since pulled down, do not use INPUT_PULLUP
  attachInterrupt(digitalPinToInterrupt(interruptPin), ButtonPress, HIGH);
}

void loop() {
  digitalWrite(BlinkLED, HIGH);       //What ever the task of the Arduino, in this example its Blinking LED on pin A0
  delay(1000);
  digitalWrite(BlinkLED, LOW);
  delay(1000);
}

void ButtonPress() {
  if (digitalRead(Button_Red) == HIGH) //Check the button Status
  {
    digitalWrite(LED_Red, HIGH);      //Make the respective LED pin HIGH
    while (digitalRead(Button_Red) == HIGH);    //And hold until Button is pressed, may not need such step if your application is different
  }
  if (digitalRead(Button_Green) == HIGH)
  {
    digitalWrite(LED_Green, HIGH);
    while (digitalRead(Button_Green) == HIGH);
  }
  if (digitalRead(Button_Blue) == HIGH)
  {
    digitalWrite(LED_Blue, HIGH);
    while (digitalRead(Button_Blue) == HIGH);
  }

  digitalWrite(LED_Red, LOW); //Make the inputs low again
  digitalWrite(LED_Green, LOW);
  digitalWrite(LED_Blue, LOW);
}

Note : Select the interrupt mode depending on your application. Since I selected to be inside the interrupt during the button press, I used "HIGH". Also it depends on how you bias the pins also. (ex: Pull-up, Pull-Down and diode polarity etc)