2

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?

  • 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. – Sorenp Jun 12 '21 at 05:38
  • 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. – Mashal Rashid Jun 12 '21 at 05:49
  • Related question: https://arduino.stackexchange.com/a/79691/13673 – ErikR Jun 12 '21 at 05:52
  • You need an interrupt controller to do something like that. Or go crude and inefficient with muxing and polling. – Mitu Raj Jun 12 '21 at 06:16
  • Which Arduino? Does it have pin change interrupts? You could also read the 8 switches in a timer interrupt to poll them perodically. – Justme Jun 12 '21 at 08:35
  • Arduino Uno. Two interrupts, pin 2,3. No, it doesn't have pin change interrupts. That's for Arduino Mega which I do not wish to use. I'll look into timer interrupt with polling. – Mashal Rashid Jun 12 '21 at 08:44
  • 1
    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. – Gil Jun 14 '21 at 03:07

2 Answers2

2

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.

Saadat
  • 572
  • 3
  • 8
  • 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? – Mashal Rashid Jun 12 '21 at 06:40
  • 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: http://training.ti.com/sites/default/files/thumbnail_22.png – Saadat Jun 12 '21 at 07:27
  • 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. – Mashal Rashid Jun 12 '21 at 09:03
  • 1
    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. – Saadat Jun 12 '21 at 11:27
1

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.

hacktastical
  • 49,832
  • 2
  • 47
  • 138
  • 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? – Mashal Rashid Jun 12 '21 at 06:46
  • 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. – hacktastical Jun 12 '21 at 07:15
  • 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. – TonyM Jun 12 '21 at 07:26
  • 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. – hacktastical Jun 12 '21 at 08:00
  • @tonym, the second paragraph gives an interrupt based solution. – tim Jun 12 '21 at 08:53
  • And a great many of us here have done the same or much much more, but that's all irrelevant. Lack of "knowledge of the system" clearly refers to the OP's specific system, application and requirements, not just the single microcontroller within all that. This wishes the requirements were different rather than addressing the question. That observation is reasonable. – TonyM Jun 12 '21 at 14:09
  • @tonym you seem to be falling into the 'xy problem' trap. OP is asking for some kind of *hardware* change-of-state detect across a lot of inputs. It's of course possible with some kind of gates-and-latches kludge. However, **no one really builds systems that way**. Contemporary MCUs (including the Arduino's ATMega328) have change-of-state detect built in to their GPIOs. Systems short on pins can use an I2C expander as I and others suggested. Polling is also a valid solution, which can be made very efficient with the right row/column setup. I've done all of these, and more. – hacktastical Jun 18 '21 at 05:44
  • I'm clearly not. And yet again, having a CV doesn't elevated one's status beyond anyone else's here, so bringing that up is utterly irrelevant. The 'xy' thing just defends changing a question, so we'll ditch that. I see your argument, which needs no shouty bold, and disagree. The point is to answer the question as written. Why? People learn and develop in design by exploring things that are harder to do and up against restrictions. This breeds creative thinking and understanding. Churning out industry typicals teaches one-dimensional thinking. (cont'd) – TonyM Jun 18 '21 at 13:49
  • So exploring ideas, looking into what you might never actually make is very, very good for one's skills. Within sensible bounds, naturally, but we're miles away from any boundaries with this one. The answers are to teach - not to just provide solutions. It's great watching people here take that and try out their own ideas, learning and progressing. That's the point of the site. Second-guessing the OP's situation and instructing them how to go about their task is not the point of it. So we answer the question as asked. (cont'd) – TonyM Jun 18 '21 at 13:49
  • Yes, I can do fully costed, tested and proven designs suitable. I can choose one suitable for college learning or the home enthusiast or high-volume mass production or critical safety systems or all sorts of applications and I'll use different approaches for each. I learned all of that by exploring the possibilities and learning how to pragmatically assess and select the better/best ones. That's why, for instance, I found the 'huh just use an MCU' answers (we used to get more of) are so useless and dull. (cont'd) – TonyM Jun 18 '21 at 13:52
  • Not least because they almost never consider actual development/support costs in pro' organisations, only 'me at my desk spending freely' set-ups. That promotes narrow thinking. Finally, this could be done with an 80p CPLD and a few days of firmware to make the 'gates and latches' thing that apparently "no-one does any more" - but gets done tons. Understanding the building blocks of such a design is far more important - people can then see how they could do their own and weight that up against other solution that suit their circumstances. (If only I had time to post such an answer.) Done. – TonyM Jun 18 '21 at 13:53
  • Getting an ISR set up to process GPIO is very simple, and there’s lots of reference code available, given that OP is using Arduino. We’re talking maybe an hour’s effort. Polling is even simpler as a first step - half an hour, tops. No external hardware solution could compete with that in either hardware or software cost. It would be an absurd waste of time and money to use a CPLD - that’s like the least cost-effective solution except perhaps for SSI TTL. Like I said, no one designs systems that way anymore. – hacktastical Jun 19 '21 at 16:23
  • Well, one can change one's views to fit the facts or try to change the facts to fit one's views. Meanwhile, I've designed for a lot of clients, for high/low volume commercial, avionics, industrial, automotive, medical, naval and satellite over a few decades, and seen this stuff for real. You'll understand me taking that instead. The CPLD bit made me chuckle, there's no grasp of what's done in wider industry. I'll leave you to it. – TonyM Jun 19 '21 at 19:42