2
\$\begingroup\$

I have 8 TTP223 touch sensors for 8 relays. I want my microcontroller to know when anyone of the switches is high or low. For that I can take inputs from the same pins that switch relays. However, how would the microcontroller know that the state of the switches is changed in real-time?

I know Arduino has interrupts and they work fine with rising or falling edge. I could add interrupt for switch on/off on pin 2 and 3 respectively. But how do I do this for 8 pins? Is there some component that I could attach all 8 switches as input and it would generate a momentary pulse for Arduino to detect when any one of the switches is turned high or low?

Could I add this for 8 inputs?

\$\endgroup\$
7
  • \$\begingroup\$ Thing is you can't know which one of the switches was asserted if you don't use individual external interrupts. So if you MUXed the switches into a single GPIO then you need more circuitry in any case. \$\endgroup\$
    – Sorenp
    Jun 12, 2021 at 5:38
  • \$\begingroup\$ I could loop each of the inputs perhaps? With a 8x1 MUX. Read them individually against a stored state. But I need to know when to do that. If I keep it under constant monitoring, microcontroller loses precious time doing nothing. \$\endgroup\$ Jun 12, 2021 at 5:49
  • \$\begingroup\$ Related question: arduino.stackexchange.com/a/79691/13673 \$\endgroup\$
    – ErikR
    Jun 12, 2021 at 5:52
  • \$\begingroup\$ You need an interrupt controller to do something like that. Or go crude and inefficient with muxing and polling. \$\endgroup\$
    – Mitu Raj
    Jun 12, 2021 at 6:16
  • 1
    \$\begingroup\$ You can use a PCF8574 Remote 8-Bit I/O Expander for I2C Bus which will give you an interrupt when any any pin changes state. You have to keep track of the state in software to know which one tripped it and it only needs 2 pins. \$\endgroup\$
    – Gil
    Jun 14, 2021 at 3:07

2 Answers 2

2
\$\begingroup\$

If you are looking for making an interrupt on each input change and then search for the changed input, I usually use 'XOR' gates (or equivalent depending on rising/falling edge) for finding any change on inputs. You need to create 8 input 'XOR' gates (using multiple 2 or 4 input gates) and connect the output to the interrupt pin. If any pin change happens, the output of the 'XOR' gate will change and you can start to scan your input pins (via multiplex, or if there are enough input pins available, you can check them directly). More info can be found here.

However, for simplicity, if only one input is changed at a time, I always use diodes to create the interrupt. For example, if inputs are normally high and I'm looking for falling edge detection on each input, I connect every input directly to the processor, then I add a diode between each input and the interrupt pin in a way that the cathode is connected to the input and anode is connected to interrupt pin. I add a pullup resistor on the interrupt pin. This way, when inputs are high, all the diodes are off, and the interrupt pin is high too. When any of the inputs go low, the interrupt pin will go low too. In such events, you can check the inputs.

\$\endgroup\$
4
  • \$\begingroup\$ So, I would need 2 interrupts? One for when all switches are high or all are low, and another for everything else with XOR? But isn't XOR output HIGH once any of the switches is high? Does it generate a pulse if I turn on/off more than one switches? \$\endgroup\$ Jun 12, 2021 at 6:40
  • \$\begingroup\$ You will be needing one interrupt. Any change in inputs (going high/low), will change the output of the XOR gate. You need to listen to both rising/falling edges. This way, even if two inputs change almost simultaneously (even if input change overlaps), you will be able to detect that. Maybe this image helps: training.ti.com/sites/default/files/thumbnail_22.png \$\endgroup\$
    – Saadat
    Jun 12, 2021 at 7:27
  • \$\begingroup\$ What if one switch is on constantly and another is turned on meanwhile? Would that change be detected? Wouldn't the XOR give a constant high? The switches operate relay, like I said. It is on for long periods of time. And there are 8 of them. My apologies but I do not see how to make this work with these many inputs. There's no scenario as to how many can toggle or stay on/off at a time. Change is random. \$\endgroup\$ Jun 12, 2021 at 9:03
  • 1
    \$\begingroup\$ Yes. The output of XOR will change state on every change on any input. This means if for example, all inputs are high, the output will be low; If input 1 goes low, the output of XOR goes high; meanwhile, if another input goes low too, the output of XOR will go low again. This means you need to listen to both rising/falling edges on the interrupt pin. This way any change on any input pin will give you an interrupt. \$\endgroup\$
    – Saadat
    Jun 12, 2021 at 11:27
1
\$\begingroup\$

Since we're talking about touch switches it would be reasonable to just poll them every so often. Using an interrupt seems like overkill.

If you're low on pins, you could consider an I2C expander like the PCF8574 which can detect change-of-state on any pin and issue an interrupt. Your Arduino would then go read the state and update the button touch state.

Regardless, your button input code should also be doing some switch debounce.

\$\endgroup\$
13
  • \$\begingroup\$ I use a resistor, a capacitor and a diode with the switch output for relay. Debounce is handled; but I wasn't using it with the microcontroller. Thank you for pointing it out. Also, would polling work with me constantly waiting on WiFi Serial, and 8 pins? \$\endgroup\$ Jun 12, 2021 at 6:46
  • \$\begingroup\$ I can't answer that, not knowing how your code is structured. That said, if you have an RTOS of some kind you would use its scheduler to initiate button scanning from time to time. If you don't, you'd use a timer to generate a periodic interrupt that fires off a scan to capture button state. \$\endgroup\$ Jun 12, 2021 at 7:15
  • \$\begingroup\$ This doesn't answer the question, it tries to change the question, claiming it "would be reasonable to poll them" instead based on no knowledge of the system and therefore no reasoning at all, just wishing. Downvoting, I'm afraid. \$\endgroup\$
    – TonyM
    Jun 12, 2021 at 7:26
  • \$\begingroup\$ I've designed shipping products that poll buttons, including DVD players, and products that use interrupts to initiate scanning, like remote controls. And I've actually used the ATMega328 and written code for it. Seems like "knowledge of the system" to me and not "wishing". If you have something constructive to add then by all means, add it. \$\endgroup\$ Jun 12, 2021 at 8:00
  • \$\begingroup\$ @tonym, the second paragraph gives an interrupt based solution. \$\endgroup\$
    – tim
    Jun 12, 2021 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.