Introduction: Open Fly Detection System
This instructable was a collaboration with OHoilett. Check him out, he's awesome!
We've all been there. You used the restroom hours ago, look down and...
Your zipper has been open this whole time?! How? You wonder if anyone noticed as you stealthily correct your situation.
No worries - we have a solution for you today. Two solutions actually, developed in one of the weirder brainstorming sessions we've had. This instructable presents two designs - from conception they were a symbiotic collaboration, and so they will remain one.
If you like our Instructable, please vote for us in the Zip It Good! Contest, Wear It! Contest, and/or the Microcontroller Contest. The "VOTE" button is also located at the top right of this page. Thanks! And enjoy the Instructable!
Step 1: Table of Contents (really)
We'll first show each of the two devices in action. The photodiode detection write-up is focused on easy implementation from common parts. The magnetic detection write-up is focused on a walk-through design example with some side lessons in reading schematics and datasheets. Feel free to read either or both of them!
Steps 2-6: Photodiode Detection Method by OHoilett
Steps 7-17: Magnetic Detection Method by Moxon
We'll show you how to construct each of the devices, their operating principles, and hiccups along the way.
And finally we'll conclude with good resources, prior art, bonus information, and future steps
Step 2: Open Fly Detector: Photodiode Detection Method
This method employs a few basic components and, of course, an Arduino:
2 x photodiodes
1 x infrared (IR) LED
1 x red LED
5 x resistors
1 x Arduino Uno
I used 2, 220 kOhm resistors with the photodiodes, one 100 ohm resistor with the IR LED, and one 200 ohm resistor with the red LED)
Summary of operation:
A photodiode is a device that produces a current proportional to the amount of light that hits it. When we put a photodiode in series with a resistor, the current flows through the resistor and produces a voltage in accordance with Ohm's Law. The voltage is sensed by the analog in pins on the Arduino. In this set up, there are two photodiodes positioned at the top and bottom of the fabric behind the slider of the zipper. The IR LED is held at the handle of the zipper slider. When the zipper slides down, the top diode is triggered and then the bottom diode. The program registers this as a "zipDown" event. When the zipper slides up, the bottom diode is triggered and then the top diode. The program registers this as a "zipUp" event. If a zipDown event is registered, and as a corresponding zipUp event does not occur within a given amount of time (e.g. the time it would take for you to use the bathroom), a haptic response is triggered. In this example, I used a red LED because it was too early in the morning to use a buzzer.
Step 3: Gather Your Parts
Step 4: Preparing the Various Connections
1. Solder some hook up wire to the photodiode. The photodiode has two leads, one which is longer than the other. The longer lead is the anode. The shorter lead is the cathode. The orientation of the photodiode is important. But for right now, solder the red wire to the longer lead, and solder the black wire to the shorter lead. I also soldered some header pins to the other ends of the wire. This is optional.
2. Solder some wire to the IR LED. Similar to the photodiode, the LED has two leads, one longer than the other. Solder the red wire to the longer of the two wires and the black wire to the shorter of the two.
3. Hot glue the photodiodes to the pants, right behind the slider. Do not hot glue the zipper chain, or the zipper will not slide up.
4. The anodes of the photodiodes should be connected to 5 V. The cathodes should be connected to the 220 kOhm resistors. The other end of the resistors are grounded.
5. Connect the IR LED in series with the 100 Ohm resistor. I attached the IR LED to pin 9, but you could also simply attach it to 5 volts. We are writing HIGH to pin 9 anyway so it is 5 volts.
6. The red LED should be placed in series with the 200 Ohm resistor and with one of the connections going to digital pin 8 on the Arduino.
Step 5: Upload the Code
Please plug in your Arduino and upload the code provided.
Step 6: A Few Suggestions
I did not glue the IR LED to the zipper tab. Instead, I held it to the tab with my fingers so the light shining from the LED would point towards the photodiodes when I was zipping up. This was because I could not find a good place to fasten the LED. For a better design, you may want to use flat surface mount LEDs and photodiodes. The flat LED could be pasted behind the zipper slider. I plan on doing this, but this was a good start.
Step 7: Open Fly Detector - Magnetic Detection Method
In contrast to the first method, this method uses more parts and is a bit more involved. A picture of the finished system is shown above, and we'll go through each portion of the system in detail.
Due to the number of parts in the system, I will try to guide you in general of how to build up a circuit from a given circuit schematic, rather than give a step by step wiring diagram. (It'll be fun, I promise*). I hope to get across a feel for the design process and tradeoffs considered, and to give a starting base for you to develop your own.
*Depends on if your idea of fun aligns with mine. Read on!
Step 8: Gather Your Parts
I chose to go a little bit more custom (read: time-consuming) route, and you might not have all these parts on hand. There are many ways this design could be improved, and I'll point them out along the way. The parts shown in the pictures above are only a subset of what is needed.
I will take a little bit more technical approach to my design - there will be schematics, datasheets, c-code, and bit fiddling. Reader beware! But I will do my best to explain everything as I go - if you aren't familiar, don't worry, I hope it will be a good learning experience.* In fact, this build will read more like a design and thought process, and that's on purpose (and I hope you'll enjoy it!).
To start, I will provide pictures and a bill of materials (BOM**) of the materials you'll want to gather.
- (1x) breadboard or protoboard
- (1x) small, strong (i.e. neodymium) magnet
- (1x) pair of pants or shorts with a zipper
- (1x) 3V coin cell battery (CR2032)
- (1x) coin cell holder (BAT_HLD-001-THM)
- (1x) slide switch (optional, JS202011CQN)
- (1x) ATtiny24A (ATtiny24a)
- (1x) hardware programmer (AVR Dragon or Atmel ICE)***
- (1x) Hall Effect Sensor (A3212EUA)
- (1x) Small vibration motor (removed from old phone, or Motor)
- (1x) Bicolor LED (LHG3392)
- (1x) Small PFET (TP2104)
- (2x) Small NFET (BS270)
- (1x) Small Diode (1N4148)
- (2x) 100 Ω****
- (2x) 680 Ω
- (1x) 22 kΩ, 1% tolerance
- (4x) 120 kΩ, 1% tolerance
- (4x) 100 nF ceramic capacitor
*Feel free to leave a comment or message asking for more detail! It can be a lot of things coming quickly and I'll do my best to elaborate.
**Believe me, this acronym never gets old.
***I'm sorry, these can be a little pricey. The Arduino IDE could be used with some of ATtiny's, but the hardware programmer is needed to change fuse settings (necessary for setting clock frequency and power saving options).
****For passives such as resistors and capacitors, there are many many that could work. No special requirements are needed except for a 22k and 120k resistor with 1% tolerance used in the battery checking circuit.
Step 9: Basic Theory of Operation
Here I'll give you a block diagram of the hardware, a circuit schematic of the hardware, and a block diagram of the software. I'll explain things in the same order as I designed the circuit.
I knew I wanted to detect the opening and closing of a zipper. There were many ways we could do this - in the first part of the instructable, we showed you a way using an LED and a photodetector. However, there are many ways to skin a cat with a zipper - I chose to go with magnetic detection, by placing a magnet on the back of the zipper. There are even more ways to do this - see if you can think of more!
This leaves me with how to detect the magnetic field - I could use a magnetic reed switch (can be expensive or fragile) or use a hall effect sensor (which can sense both the strength and polarity of the field). I pulled a hall effect sensor out of a small computer fan to quickly test if this was sound. In my design, I chose a more readily available part, looking for cost and sensitivity. For ease of assembly I went with a omnipolar hall effect sensor - that is, it senses only the strength and not the polarity of the magnetic field.
Great! If it weren't for my love of over-engineering, we could be nearly done. But, we can get quite a neat little system by putting in some extra thought. Over the next few steps I will describe each of the component blocks of the hardware and software, describing its design, purpose, and tips on construction.
In the hardware block diagram, we can see that the battery will power everything. The outputs of a magnetic sensor and a batter sensor will be processed by the microcontroller, which will perform some calculations and set the power indicators or zipper alarm as needed. A programming interface is needed to upload our code.
Take a stab at the entire circuit schematic and get familiar - for ease of interpretation, each subdivision in the circuit schematic corresponds to a block in the hardware block diagram. We'll delve into the design choices and theory of operation for each in the next few steps. We want you to understand what you're building (so you can modify and improve on it!)
In the software block diagram, we have a simple process of what we want our microcontroller to do. It will go through some start-up operations, and then go into a loop of power-saving sleep (to prolong battery life) and occasionally waking up out of this low-power state to check zipper status.
Step 10: Improve the Attractiveness of Your Zipper
There are a number of ways you can attach the magnet to the zipper. Keep in mind the magnet is fairly strong and will jump to nearby magnetic surfaces, so take care not to disturb while your adhesives set.
Identify the North or South pole of the magnet by having it align with another magnet, and mark the poles.
Position the magnet on the zipper with one pole facing towards the top of the zipper and one pole facing the bottom of the zipper. (It does not matter which pole goes where).
I chose to simply hot glue the magnet into place as shown. Alternate methods could include epoxy (you should look up instructions and use proper equipment for this). Other types of strong glues may also work.*
*I have not tried duct tape
Step 11: The (Small) Battery
Ideally, we want a small, low-cost power source. I chose a CR2032 lithium coin cell as a good compromise between small size (20 mm diameter), sufficient voltage (3V), and capacity (225 mAh). The datasheet can be found here. The slide switch is included to be able to disconnect the battery from the circuit easily.
In understanding and building a circuit from a schematic, consulting datasheets is critical. I can relay most of the important information about a part in my explanation, but there are many subtleties I can't convey (or may not even know about!)
For instance, in checking the datasheet more thoroughly, I can see that the operating temperature is between -30 and +60 degrees Celsius. Also, there will be additional graphs on characteristics with respect to various parameters. I've excerpted two graphs from the datasheet and uploaded them above.*
The voltage versus discharge current graph shows that as load current increases, the battery voltage will drop (sometimes quite dramatically!). This could get us into trouble later, and is one part of this design that could be improved. If we draw too much current, the battery voltage could dip below the operating voltage of some of our components!
The capacity versus load resistance graph shows that as more current is drawn, the capacity of the battery is decreased. At 1 mA discharge, we get ~190mAh or 190 hours of lifetime! At 2 mA discharge, we get ~165mAh or only 82.5 hours of lifetime. Notice that the decrease is nonlinear - i.e. at 2 mA discharge we don't get 190/2 = 95 hours of lifetime. The lower current we can run at, the even longer our battery will last.
*Future discussions may simply refer to the datasheet, I don't want to clutter with too many pictures! It's also a good exercise to get used to finding relevant information in these (sometimes obtuse) documents.
Step 12: The Hall Effect Sensor
I chose this part mostly based on power considerations - it can run from 2.5V to 3.5V supply, and draws at worst 10 uA. While many of the hall effect sensors intended for use in brushless dc motor applications have similar pinouts, we'll check the datasheet to determine how to use it. We can see that when a magnet is brought nearby, the output is low (near 0 Volts). Otherwise when the magnet is far away, the output is high (near the battery voltage).
Since we're running on a small battery and we'd like it to last awhile, we should choose the pull-up resistor* so that minimal current is drawn.
*The resistor is called a pull-up resistor because the sensor doesn't really actively "drive" the output signal high. Instead, it can be thought of simply as a switch. A magnet nearby causes the switch to close, connecting the output to ground. When a magnet is not nearby, the switch is open, allowing the output to float - by connecting a pull up resistor, the voltage on the open side of the switch goes high to the battery voltage.
Step 13: The Microcontroller Brains
We could use the output of the hall effect sensor directly, but imagine the consternation of the user! Sometimes we do actually have to unzip our pants, and we don't want to be buzzed continuously.
So, we'll add a microcontroller to periodically check the zipper state and alert the user if their fly is down. Most of the work here will be done in software. In choosing a microcontroller, I went with a family I already had a programmer for, and that was familiar to a wider audience - the ATtiny. These are small 8 bit microprocessors with operating voltage down to 1.8V, perfect for a small battery powered application. We won't need much processing power, and we won't need many inputs or outputs.
For this Attiny processor family, we need to wire up a connector for an ISP (In-system Programming) interface. This will rob us of some of the pins on our device, but luckily we have many to spare. It's a good time to note that there appears to be nothing connected to the microcontroller, except named wires! In schematics, it is often common practice to label a wire (or "net"), and two nets are considered connected if they share the same label, even if there is no explicit wire shown. For instance, pin 1 on the ISP connector (MISO) is connected to pin 8 on the ATtiny (PA5) through the net named "MISO". The RESET, SCK, and MOSI lines are connected in the same way.
The datasheet is very long (238 pages). Many times you can get away with not having to read all of it, as there are often standard libraries or development tools that can abstract away the nitty gritty details of toggling bits in registers. The Arduino IDE is a good example of this.
Step 14: The Alert System
In the image above, I show the motor circuit (with the motor replaced with an LED for taking pictures of operation).
The motor requires more current than we can supply with our microcontroller pins, so we'll use transistors as switches. We use BS270 NFET transistors - these can be thought of as a voltage controlled switch, where a low voltage opens the switch (turning off the motor) and a high voltage closes the switch (turning on the motor).
It's also a good idea to add a diode across the motor as shown - the inductive winding of the motor can produce a voltage spike when turned off. The diode clamps this voltage and prevents it from damaging other components.
R3 is chosen as a high value resistor which will bring the gate back to an off state as default, shutting off the motor. This is a good idea while the microcontroller is starting up, or insurance in case something goes wrong.
Note that I used two transistors in parallel to increase the current handling. The BS270 transistors were the lowest threshold (how much voltage it takes to "close" the switch) transistors I had on hand, but are still not ideal. Future selection could look for transistors with thresholds around 1V.
Step 15: Checking the Battery Voltage - Hardware
In the images above, I show a power good state (Battery voltage >2.8V) and a power bad state (Battery voltage <2.8V).
Our microcontroller has a built-in ADC, with a built in bandgap reference of 1.1V. While our microcontroller can run down to 1.8V, our hall effect sensor can only run down to 2.5V, and our motor out circuit can only run down to 2.7V (for our chosen NFET switching transistors). This actually works out great - we can use our microcontroller to detect if the battery voltage gets too low, and not try to check the sensor if the data won't be valid.
R4 and R5 are chosen to scale the input voltage down by 0.155. [Calculated by R5/(R4+R5)]. However, this divider will continuously draw current, even if we only want to check the battery once on power up.
The transistor at the top is a PFET - it can be thought of as a switch that is closed when its gate voltage is low (near 0V) and open when its gate voltage is high (near the battery voltage). Like we did with the NFETs in the motor circuit, R7 pulls the gate high, keeping the divider off when not needed by default.
Step 16: Indicating the Battery Voltage - Hardware
We want to indicate to the user somehow if the battery voltage is too low. We could use the motor we already have, but adding some LEDs to indicate is pretty cheap as well. (It's not a project without an LED somewhere)
I used a bicolor LED - a red and green LED in the same package. This saves some space, but you could just as easily use a separate red LED and a separate green LED.
These LEDs will only be on for a little bit when the device powers up, but we still don't want to draw too much power. Additionally, the I/O (input / output) pins of the microcontroller can only source or sink a small amount of current. So, we'll add some series resistances to set the LED currents. We need to strike a balance between power consumption and visibility.
R8 and R9 limit the current through the LEDs.
Step 17: Software Things...
Unfortunately, to explain the software would take another (longer) instructable. I've attached the code (.c and binary files). The code is heavily commented, but is not as readable for those used to dealing with the Arduino IDE.
I use Atmel Studio 6.2 to write code for, compile, and program my microcontroller. I used an AVR Dragon with an ISP interface to upload my code. These are not trivial steps, and there are existing tutorials on Atmel's website to help get you started.
Keep in mind that the code follows the logical flow of the software block diagram, and look through it one module at a time.
Step 18: Future Steps, Work, and Plans
Neither of the approaches are very concealable... for now!
Potential Improvements and Next Steps
- Further work on sleep modes to reduce power further
- Design a standalone PCB (Printed Circuit Board) to reduce size
- Switch to a more suitable NFET with lower gate threshold voltage
- Better handling of low battery conditions
- Port code to more friendly Arduino IDE (though a hardware programmer is still needed to change fuses)
For photodiode detection method
- Switch to flat faced photodiodes and IR LEDs
Step 19: Glutton for Punishment? Look Further...
Inspiration and Similar Projects
The inspiration for this project came from a brainstorming session on Google Hangouts, thinking of how we could try for a strange entry in the zipper contest. We noticed afterward someone has previously made a different version!
Where to find Technical Information
There are many great sources of information on the web. If you have the time, the datasheets of the parts can't be beat for the low level details. However it's not always clear how to implement these things - for that, there's other people's projects, and application notes
Hall Effect Sensors
Low Power in AVR Microcontrollers
We have a be nice policy.
Please be positive and constructive.