7
\$\begingroup\$

I'm trying to build a latching switch connected to a door and would appreciate ideas.

The switch is connected to a microcontroller input pin, and I'd like it to operate as follows: When a person opens and closes the door, it latches on. It remains on until it is reset in software.

The basic idea is to time how long since the door was opened and closed again, and do something based on that. Once I have done this, I can reset the switch again. It does not matter if the door is opened or closed several times.

I'm not sure where to start, so any ideas welcome.

\$\endgroup\$
  • \$\begingroup\$ So do you have an idea about how you want to sense when the door is open or closed, or is that part of the question? Also, would you say that you are primarily interested in when the door opens and closes, not in a "latch"? \$\endgroup\$ – W5VO May 19 '10 at 3:22
  • \$\begingroup\$ Good point. That is part of the question :) I don't mind when the door opens and closes, just that it has done so. \$\endgroup\$ – Dirk May 19 '10 at 11:03
9
\$\begingroup\$

Most modern MCUs like the Atmel AVR (used in Arduino) have the ability to sleep in a very low power state (< 1 uA) and wake up from a variety of sources including low-power timer driven by 32 kHz crystal or pin change interrupts. Using a pin change interrupt or a low level triggered interrupt can wake up the AVR microcontroller from deep power down. Then the state can be saved in a program variable. See http://www.atmel.com/avr

An alternative hardware-only approach to a latching switch is the RS latch built from two 2 input NOR or NAND gates with output fed back to one of the 2 inputs. Whenever the output goes high, the feedback holds it in that state until the other input is pulled high to reset the latch.

Atmelfaebrian

\$\endgroup\$
  • \$\begingroup\$ If you have a controller, it probably makes sense to use it. It may also have power down modes that allow a timer or rtc to keep running so you can still keep time while the cpu sleeps. Alternatively, you could use an external RTC. \$\endgroup\$ – XTL Jun 26 '10 at 22:32
4
\$\begingroup\$

Use a Hall Effect sensor and a magnet on the door. If the sensor picks up a magnetic field, the door is shut. No magnetic field - not shut.

Now you can periodically poll the sensor at whatever rate you want (something long...like 250 ms?). Once you detect no magnetic field, you can start tracking time.

\$\endgroup\$
3
\$\begingroup\$

To clarify, you're not talking about latching the door shut, right? You just mean that the switch latches?

Assuming yes: I'd start with an optical slot sensor and a piece of cardboard attached to the door, and then do the latching in software. Try something like the Rohm RPI-131 from Digikey.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yep - not latching the door shut. By "latching" I mean that the switch stays in some state until reset. I know that it is possible to have the MCU listen continuously, but would rather only do that once every few minutes. (I'm really trying to conserve power, so mcu will hopefully spend most of its time asleep) \$\endgroup\$ – Dirk May 19 '10 at 2:20
  • \$\begingroup\$ This is kind of a cool idea, so you'd mount this sensor on the frame and mount a piece of card on the door that breaks the beam when the door is closed? The drawback of this approach is that this type of sensor is always on, and looking at the datasheet it draws between 50-100 mA. \$\endgroup\$ – vicatcu May 19 '10 at 15:55
  • \$\begingroup\$ I bet you could solve that mechanically. Maybe the cardboard could actually be a scrap of plastic that's bent so it fits into the slot sensor. When the door opens, it pulls out of the slot and un-bends, but when the door closes, it doesn't fit back into the slot. (It would need to be reset by hand, but from the question, I think that's OK.) Then you could turn the sensor off almost all the time. \$\endgroup\$ – pingswept May 19 '10 at 19:22
  • \$\begingroup\$ If polling, the sensor only needs to be on when it's polled. Arrange it so the controller can switch it on/off. \$\endgroup\$ – XTL Jun 26 '10 at 22:34
3
\$\begingroup\$

I would do something super simple, like a contact "switch" and do the "latching" logic in software. That is to say, just use two pieces of metal, one on the door and one on the frame that "touch" when the door is closed and separate when the door is open. Make the input pin have the internal pullup enabled. Attach the metal that is on the frame to your MCU input pin. Attach the metal that is on the door to GND (common to your MCU GND). When the door is closed that input will read "0", when the door is open that input will read "1". I think you can then even wake up on a logic level transition on that pin (I think that's how it works anyway). You can also couple this wakeup with a "periodic" wakeup from a timer interrupt as suggested by others.

\$\endgroup\$
  • \$\begingroup\$ I agree. It's simplest to have the hardware only sense "door closed" or "door open", and have the software keep track of whether the door has ever been opened, how long the door has been open, how long the door has been closed, etc. The above "2 pieces of metal" is fine for prototyping. Later you may want to switch to some other door sensor less vulnerable to static charge. -- a magnet and a hall effect sensor, a spot painted white and an optical reflective sensor, etc. Anything that gives you a "0" when the door is open and a "1" when the door is closed. \$\endgroup\$ – davidcary May 31 '10 at 23:34

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.