Current draw from Mega IO Pin to NAND logic gate inputs

Hi there,

I am using a NAND logic gate to indicate if any of the limit switches (connected to the three NAND inputs and the 6x1 header pins 1, 2, and 3) have been triggered. I am using an NAND gate because the limit switches use pullup inputs at the MCU. The limit switches are also normally closed (NC). Therefore (correct me if I'm wrong), the NC limit switches give a LOW signal to the logic gate when they are NOT engaged, and they give a HIGH signal when they are engaged. I've attached a photo of my schematic. Now, I have a few other peripherals on my ATMega so I'm concerned I'm going to go over the 200mA current limit on the MCU with the logic gate. Does anyone know how much current will be drawn from the output pins of the MCU (pins 40, 42, 44) when the switch is closed/open? Not sure if I'm thinking about this correctly. Do I need a current limiting resistor at the MCU pins?

Thanks in advance,

For a NAND gate, all inputs must be HIGH for the output to be LOW.

Umm, you meant input pins, right? You'll damage these pins if you configure them as outputs and set them high while wiring them to a switch that is closed and connected to ground.
And yea, for a NAND, you want to have both pins HIGH to give you a LOW signal out. In order for you to detect if EITHER limit is broached, with NC to ground switches that open when actuated, you want either a NOR or an OR gate.
C

Thanks for the responses everyone.

My mistake, I meant input pins. They're set as pull-up inputs. What I want is for every time any input signal to the NAND gate is LOW (the limit switch is NOT depressed) for the output signal from the NAND gate to be HIGH. The main question I have is how much current will be drawn from the MCU when the limit switch is depressed or not depressed? ie. when not depressed, how much current flows into the limit switch, and when depressed, how much current flows into the NAND gate?

The current into the limit switch depends on the pullup/dn resistor you supply. Your circuit description is so imprecise that I cannot tell more. The current into the gate depends on the chip technology.

I'm still lost, but...

There is virtually no current into (or out-of) an Arduino input pin unless you are using the internal pull-up and that's 10 or 20K so less than a milliamp when forced-low.

The current into the NAND gate input will depend on the logic family so you'd have to check the datasheet. But it will be insignificant relative to that 200mA spec. If it's CMOS it's probably also nearly zero.

I don't have the value at my fingertips, but if you configure the input with pullup, I believe the pullup is around 30kohm; I'm sure someone who knows will correct me if I'm wrong. With 5V across 30 k, you're only seeing around 1/6 of a mA into the switch. When the switch is open, then your NAND input and the MCU input will both be drawing microamps. So I'd not worry about this.
Let's look at your logic. To have a low output from the NAND, you have to have both NAND inputs high. To have them high, the switches must be open. So you'll have a low only when both switches are not operated. However, switch nomenclature is usually considered with the switch in it's "natural" state - for a pushbutton, NC means when your not pressing it, it's closed, whereas NO means you're pressing it. For a switch depicting both states (i.e. the switch body has terminals (NC, C, NO), it is normal to have conduction from NC to C when the switch is NOT actuated. Hence our confusion. In the use case you've described, the switch should be wired using NO and C, so that when the switch is actuated, you close the switch, dropping the NAND input and changing the output state to HIGH.
C

Thank you all for your responses! I now understand the ~microamp current draw from the pullup resistor.

I am using the internal pullup resistor in the Arduino Mega 2560 which I believe is 20-30kOhm.

I actually am wanting for an "action" when the limit switch is NOT engaged. This is because I want to know when the limit switch ISN'T engaged. Seems convoluted but stick with me... The limit switch will be normally closed so that when it is NOT engaged, it will make the signal to the gate LOW, so when any of the limit switches are NOT engaged I will get a HIGH signal from the gate output to trigger an event. If all of the switches are OPEN (and all therefore giving HIGH signals), then I do not want any event triggered and the gate output will be LOW.

image

Now to be clear, whether the switch is open or closed, will I always be drawing ~microamps from the input pin? Wether its flowing into ground from the switch or into the gate inputs? The NAND gate that I am using is https://www.onsemi.com/pdf/datasheet/nl17sz10-d.pdf . I don't know when the DC input/output diode currents mean, so I'm not sure if this particular chip will work for this application. Can anyone help me out with deciding that?

Thanks again!

Input current I_IN is <= 1µA.

Great, thanks!

Why an external NAND gate. Did you run out of MCU pins?
Leo..

So just change the code to accommodate these logic levels. There is no need to use external gates at all.

This is an x-y problem.

const byte limitPin[] {5, 6, 7};
boolean limit;

void setup() {
  for (byte i = 0; i < 3; i++) pinMode(limitPin[i], INPUT_PULLUP); // switches between pin and ground
}

void loop() {
  if (digitalRead(limitPin[0]) && digitalRead(limitPin[1]) && digitalRead(limitPin[2])) {
    limit = false;
  } else limit = true;
}

Untested.
Leo..

Yes I see the NAND gate as redundant - in fact contact bounce will cause glitches on its output, which might be an issue .

You can debounce within the processor and produce the output you want .
You really need something like a 10k pull-up anyway ( and possibly a capacitor ), as the internal pull-up still gives a high impedance input , susceptible to noise - if your limit switches are any distance away ( 1m +) or better still optical Isolation , relays .

I second that. A critical shortage of pins is the only reason to add such a gate. :face_with_raised_eyebrow:

A simple logic gate is safer than a MCU if it comes to shutting down some machinery on a limit violation.

Not if it is (only) connected to the MCU after all. :roll_eyes:

I'm using the logic gate on an interrupt pin because I don't have enough interrupt pins left for each of the limit switches. I need to use an interrupt because the limit switches are a safety that could be triggered at any point in time during use of the machine. Does that sounds reasonable?

Good point, I have made sure to debounce the interrupt pin in the ISR function.

Fair enough. Let me explain:

I have a machine that uses air cylinders to actuate force. If those air cylinders are not retracted because the system looses pressure, but the MCU thinks they have retracted because its sent an output signal to retract them, so I need limit switches to check if the cylinders are actually retracted or not. Now, I don't have enough interrupt pins to connect each of the limit switches to an interrupt, so I am trying to use an NAND gate to interrupt pin to be triggered when any of the limit switches are engaged. I am using the internal pullups for each of the limit switch pullups to limit current for safety (does that sound correct?) and the limit switch in a normally closed state which is triggered (ie. opened) when the limit switches are retracted (=safe to proceed with the function). Does that sound too convoluted? I'm very new to electronics to any ideas on how to better organize this are appreciated. I also have about 150mA of current being drawn by peripherals, and I think that the ATMega2560 maxes out at 200mA of output current, correct?

Thanks in advance.

Why interrupts? If the system looses pressure then the limit switches never are activated, and if the switches are activated the system works well. What the heck should cause an interrupt then?

If there are hard limits then the limit switches should prevent the cylinders from moving into just that direction and that's possible without a controller.

The cylinders are raised up and down during operation. I need to check to see if the cylinders are ever down when the MCU thinks that they are up. ie:

if (lmt_switch == LOW && retract_cylinder ==HIGH) //then stop the system