In short, I have a bag that I'm making, the top of the bag is lined with LDR's (3-6), and there's a hidden switch to toggle the alarm on/off. I'm horrible at circuits, but I program, so it looks like this with code :D






A few questions:

Would it be simple to do this?

Would this require programming/PIC chips?

Is there anywhere I could get help drawing a circuit diagram?

Cheers, Karan

  • \$\begingroup\$ I'm wondering if using a reed switch and a magnet would provide a more reliable detection of the bag beeing opened, as a LDR obviously will fail in darkness (or is this a feature?). \$\endgroup\$
    – 0x6d64
    Commented Sep 1, 2011 at 8:48

3 Answers 3


As Oli noted, this does not require a microcontroller. Your requirement is simple enough to do in dedicated hardware.

However, using a micro has some advantages:

  • The light/dark thresholds can be easily adjusted if the LDRs are driven into A/D inputs and the decision done in firmware. This also leaves more flexibility for changes as you get experience with the unit. For example, you might want to detect sudden light changes, not just absolute level.

  • Lower power. The dedicated circuit would be on all the time unless you make it complicated. The micro can easily power down all the sensors and sleep most of the time. Sampling the light levels every 500 ms is most likely plenty fast enough. Even if it takes 100 µs to turn on the LDRs, wait for things to settle, take the A/D readings, make the decision, and go back to sleep, that's still only 1/5000 of the time. The rest of the time the total current draw should be 1 µA or less. If the current draw is 20 mA when on (that's high), the average would still be under 5 µA. Note that all these estimates have been quite pessimistic.

  • Less parts. All you need is the micro, the on/off switch, the LDRs, a resistor per LDR, and a transistor to switch on the LDR circuit.

  • \$\begingroup\$ If you're really adventurous you could ditch the on/off switch and program the microcontroller in such a way that light hitting the LDRs in a certain order would switch the alarm off. That way it's purely down to how you open the bag that determines whether or not the alarm goes off. \$\endgroup\$
    – Majenko
    Commented Aug 31, 2011 at 14:34

This would be possible with an opamp/comparator, and a few transistors or logic gates, so no microcontroller programming necessary.
You would set up the comparator to detect the change across the LDR(s), and wire the result into an 2 input AND gate (other input being the switch) and then output of this to alarm trigger. If you give some details about the alarm (buzzer? light?), and the power supply (9V battery?, AAs?) then I'm pretty sure someone will help you draw up a circuit and recommend components.

Simple is always a relative term, but yes, I think this would be regarded as quite an easy project for someone not so experienced with electronics.


AND gate

Voilà. Done!

Seriously, you need very little hardware to this, especially since your program consists for the most part of "don't do anything" statements. These are superfluous, so we keep

Sound alarm IFF both the Alarm Switch is on AND the LDR sees light.

That's what the AND gate above does; it makes the output active if both input A and input B are active. The switch can be simply connected to the A input. For the light sensor you have to compare the incoming light level with a predefined threshold. This is done by a (surprise!) comparator. So, comparator and AND gate and you're set.

The disadvantage of this solution is that it's anything but flexible. The least change you want to make will force you to start over again. That's where the microcontroller comes in. We can have a black-box approach to it, and attach a number of sensors to its inputs, like the LDR and the alarm switch, and drive a number of activators with from its outputs, like a siren.
The internals of the black box can be (re)defined at any time by downloading a new program to it. While your current application will fit in the smallest existing microcontroller I wouldn't pick that one. I'd choose a microcontroller with enough inputs and outputs, a few dozen would be nice, and also enough Flash to fit a bit more complex code. Experience learns that you need a new feature from the moment the current system is up and running.
Inputs for a typical alarm system are often just logical, 1 or 0, like the switch (alarm systems often have all kinds of switches), but also a number of analog inputs, like the light level you're starting with. Outputs will often drive relays, which in turn may switch about anything, from the siren I mentioned to a door opener.

So IMO the microcontroller is the way to go. Arduino is immensely popular and offers good systems to start with, which can always be extended with extra function boards known as shields. The Arduino site should get you started in no time, especially since you already have programming experience.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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