0
\$\begingroup\$

I have a "normally closed" magnetic door sensor, that I'd like to use with an ATtiny microcontroller.

Does a "normally closed" switch use more power than a "normally open" switch?

i.e. since the switch will be closed 99.9% of the time, will some current flow through the switch, thus wasting battery?

Or is there a way to make a "normally closed" switch not use more than a "normally open" one?


Note: This is a low-power project (goal: 1 year with a set of batteries):

  • ATtiny powered with 2 or 3 AA batteries, I'm thinking about using ATtiny85 "V" for powering with as low as 1.8V

  • I'll use "deep sleep" features to use very few power (see https://jeelabs.org)

\$\endgroup\$
  • \$\begingroup\$ Look at electronics.stackexchange.com/questions/344574/… \$\endgroup\$ – TonyM Nov 14 at 10:50
  • \$\begingroup\$ You have already accepted an answer, but check the datasheet for your processor and look for the maximum input leakage current. For reliable operation the current through your pullup/pulldown resistor should be at least 5X that value. For the ATtiny you should plan to draw at least 5\$\mu\$A through your pullup/pulldown. That's why you never see really large pullup/pulldown resistor values in well-designed systems. \$\endgroup\$ – Elliot Alderson Nov 14 at 13:07
  • \$\begingroup\$ @ElliotAlderson given this information, which max resistor value would you use? (just to be sure) \$\endgroup\$ – Basj Nov 14 at 13:39
  • \$\begingroup\$ That 1uA is a maximum value, I challenge anyone to find a working chip that actually has such a high input current. OK, if you're going to produce millions of your gadget then this is an issue to consider. If this is a one-off for a hobby project, try 3.3 Mohm and see how that works. If that appears unreliable, go lower to 1 Mohm or less. \$\endgroup\$ – Bimpelrekkie Nov 14 at 14:33
  • \$\begingroup\$ @Bimpelrekkie So what is the actual maximum current? Do you have data to support a design decision based on a lower value? Over temperature? Over voltage? If you have data that is useful here, provide it and back up your claim. \$\endgroup\$ – Elliot Alderson Nov 14 at 15:27
3
\$\begingroup\$

Indeed a normally closed switch means that a current must flow otherwise you cannot detect when the switch opens (and there is no more current flow).

Fortunately there are several ways to minimize the current consumption.

The simplest way is to use a very high value pull-up (or pull down if you prefer) resistor, a value of a couple of Mega-ohms will reduce the current to a few uA which should not affect battery life much as a microcontroller will also consume a few uA on average when it is in deep sleep mode and only wakes up occasionally to check if it needs to do something.

This is an example schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

It could be that you're using long wires to the switch and then using Mega-ohm pull up/down resistors might not work reliably.

Then another option might be better if it is OK to "poll" the state of the switch, check it every second for example instead of continuously. For that, configure an output of the micro to become high for a short time every second, connect the switch + pull up resistor to that output. Monitor the voltage across the switch as you would normally do. Then when the output is high, monitor the input.

Here's an example schematic:

schematic

simulate this circuit

If this circuit would run on 3.3 V, only 1uA would flow during the polling of the switch. Suppose I poll the switch for 10 ms every second then my duty cycle is only 1% so that would make the average current only 0.01 uA !

Obviously this only works if you do not need to know the state of the switch immediately.

In conclusion I do not think that there is a way to make a normally closed switch use less current than a normally open switch!

However, that isn't really an issue as we can make the design such that the impact of the additional current is negligible.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for your answer! PS: can you add a schematic for the first case too (just the simple pull up): "The simplest way is to use a very high value pull-up" to make sure I'm doing the same way. \$\endgroup\$ – Basj Nov 14 at 8:49
  • 1
    \$\begingroup\$ Done but note how similar both schematics are! The only difference is that the resistor connects to Vdd or to an output. In the second schematic you do need to add code to toggle that output though. \$\endgroup\$ – Bimpelrekkie Nov 14 at 8:54
  • \$\begingroup\$ Thanks a lot for your update! PS: of course, I'll disable the internal PULL_UP of the ATtiny, is that right? \$\endgroup\$ – Basj Nov 14 at 8:55
  • \$\begingroup\$ I suggest that you read some tutorial on using switches with a microcontroller. In the program you first make the output high, wait 5 ms, then use digitalRead on the input, wait 5 ms, make the output low. Repeat that every second. Yes you must disable the internal pull up/down as that would overrule the high value resistor. \$\endgroup\$ – Bimpelrekkie Nov 14 at 8:57
  • \$\begingroup\$ Last thing @Bimpelrekkie: would a megaohm resistor work with an ILS magnetic door switch? \$\endgroup\$ – Basj Nov 14 at 10:12
0
\$\begingroup\$

Yes, but the signal will be primarily voltage on a MOSFET gate, not current flowing. Each pin on the microcontroller also has some protection diodes. All in all, you will have some leakage here, but the impact will be absolutely minimal.

In the datasheet of the ATtiny85, in section "Electrical Characteristics" you can find all relevant values. Input leakage at 5.5V in high-state is <50nA. This is around ~0.438mAh per year!

If you need a pull-down resistor, you have to calculate this cost as well. The value depends on the length of the cable, insulation or shielding and the amount of inference you get.

Let us assume a 5MΩ resistor is working, I = U/R, so at 5V there is 1µA flowing through this resistor. This is a cost of ~1mAh per year.

If you use two 2850mAh batteries, you can ignore this cost completely. The microcontroller itself will use more current.

Polling using just one pin

As mentioned in my comment, there is also the possibility to implement polling with just a single pin, using a capacitor.

schematic

simulate this circuit – Schematic created using CircuitLab

The 100Ω and 1µF are just examples. Also, the parts in the dashed rectangle are just for the simulation.

The idea is, first set the pin as output and set it to high for e.g. 1ms, switch it back to input, wait 1ms and read the value.

If the switch is closed, a short time (depending on the values of the resistor and capacitor) are back to GND levels.

closed

If the switch is open, the voltage stays high for a longer time.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer!! PS: it's mAh (electric charge) and not mA/h, can you edit (I can't because it asks to update more than 6 characters ;))? \$\endgroup\$ – Basj Nov 14 at 9:10
  • \$\begingroup\$ Indeed, thank you! I fixed it. \$\endgroup\$ – Flovdis Nov 14 at 9:12
  • \$\begingroup\$ Do you think a megaohm resistor work with an ILS magnetic door switch? \$\endgroup\$ – Basj Nov 14 at 10:12
  • \$\begingroup\$ It looks like a mechanical switch. The length of the cable and inferences are more critical. Your question was if you should use this switch because of the current loss. The answer was, don't worry. So I suggest, buy the switch and experiment with a prototype and different resistor values. Also, the polling method is a great way to save energy if you have an output left. Using a capacitor, you can implement polling even with the same pin you use as input. \$\endgroup\$ – Flovdis Nov 14 at 12:12
  • \$\begingroup\$ No, the datasheet for the ATtiny specifies a maximum leakage current of 1\$\mu\$A. Any pullup/pulldown resistor over 1M\$\Omega\$ will be unreliable. \$\endgroup\$ – Elliot Alderson Nov 14 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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