I want to make an easy project of a simple alarm on my basement where there is no electric supply. It will have a magnetic contact on the door and when someone opens it, an Arduino powered by a 12V battery will wait for 7 seconds and then will let a 12V buzzer alarm for some minutes. It will also have a push button for me, to reset it when I open the door, so not to ring when I'm getting.
As I said, 12V battery is my though of solution, because there is no supply in the basement.
My questions:
For how long can an Arduino (Uno or Mini?) run on a 12V battery performing this simple loop?
Should I use a relay for the 12V buzzer? For example, after 7 seconds, a simple relay controlled by Arduino will close its contact and the buzzer will turn on.
If done correctly, the Arduino will draw no current until the door opens, so it will last until the battery self-discharges.
Depending on the buzzer, you might get away with a small transistor, but a relay would certainly work. Get relays designed for direct connection to Arduino. These have a circuit that means an Arduino output can drive it directly
Get the type where the switch is open when the door is closed. (I'm not sure what "normally" means in the context of door switches!) That way, you can power the Arduino through the door switch. If needed, you can also power the Arduino through another relay so the Arduino can keep its power on until it decides to shut down.
By "correctly", I meant in such a way that the Arduino is off (and not consuming battery) until the door is opened. If the door switch is closed by opening the door, this would then power up the Arduino by connecting the Arduino's power pin to the battery through the door switch.
The Arduino might then need to activate a relay to keep itself powered, in case the door is immediately closed again. When the button is pressed, and the door is closed again, the Arduino can shut itself down, when it is ready to do so, by deactivating the relay.
To be able to detect if the door is still open, a digital input pin would be used. To allow this pin to detect if the door is open, a diode would be needed to isolate it from the power coming from the relay.
I hope that makes sense. If not, I can draw you a schematic later.
So far, I think your project will need two digital input pins and one or two digital output pins. So it could be done by an attiny85. But you would use whatever you have now, such as Uno, to get the code working and tested. Then you would upload the code to the attiny for final testing.
If all your Arduino has to do is wait for a switch to activate and then operate a relay, there is no need for that Arduino at all.
The switch closes upon someone opening the door, and the alarm goes off. A simple RC circuit with transistor lets it go for a while, accurate timing isn't that important here.
And if going for the ATtiny, why not a 25, or even a 13? Doesn't have to do much, anyway.
PaulRB:
The Arduino might then need to activate a relay to keep itself powered, in case the door is immediately closed again. When the button is pressed, and the door is closed again, the Arduino can shut itself down, when it is ready to do so, by deactivating the relay.
I think I would use a hard wired relay with the read switch to seal in the power - like the old fashioned motor starter circuits with the Arduino cycling a second relay to shut itself down when the shut down conditions are met
The Arduino will take a short amount of time to power up to do anything with the program, that is why my hardwired suggestion. I had to fix an alarm system a few years ago and they had an alarm sensor with a microprocessor of some sort, the sensor was powered up and checked for alarm status during the power up and the alarm would always false trip until I had the wiring changed to power up the sensor early.
Yes, attiny45 or 25 would probably be fine, I guess. Never use attiny13, so I would have to trust you on that, but this application does not need much in the way of resources at all.
This is so simple project everyone will have (slightly) different idea how to do it and each one will have some advantages and disadvantages. Powering Arduino only when door opens is quick and easy way how to do it but have one problem: when the door stays open Arduino will consume current all the time unless you add some additional hardware to prevent this. It will drain the battery - time will depend on its capacity and Arduino used.
More "clever"*) way is to use sleep mode of (nearly any) microcontroller. Simple microcontollers (i.e. already mentioned ATTiny or ATMega used in Arduino) consume less than 1uA (1/1000 of mA!) while in deep sleep, less than self discharge of most of batteries.
If you are interested there is Nick Gammon's tutorial about reducing power consumption: https://www.gammon.com.au/forum/?id=11497
*) I mean: you get slightly better results using sleep but if you don't know how to use it you have a lot to learn before it will work as expected. If you want to get it quickly done the other method may be considered more clever.
Only if your magnetic contact is closed for 5-10 seconds to allow the Arduino time to start up and activate the relay.
You can also easily make a latching relay kind of circuit, and then have itself switch off after some time. Much better for battery operation (relays draw quite a bit of power).
Warning: haven't tried this but confident it will work.
If your supply voltage is 12V connect you of course have to connect it to Vin of the Arduino. Maybe add a resistor as extra protection for the Arduino port (1-2kΩ or so). Replace the push button by your magnetic switch.
The relay will switch on and latch; the MOSFET's get gets pulled high so it conducts. Then when done, the Arduino does a digitalWrite(HIGH) to the appropriate pin, releasing the relay and switching itself off in the process. No current leaks whatsoever.
Another option would be to use three MOSFETs to do the same. Two form a latching power circuit, the third is to break the latch and switch it all off. This circuit is based on circuits found online, again haven't tried it.
wvmarle:
Only if your magnetic contact is closed for 5-10 seconds to allow the Arduino time to start up and activate the relay.
If you decide to follow the suggestion made already by vwmarle, using an attiny chip instead of a full Arduino, then the time required to activate the relay would be almost zero. The delay of 2~3s you get when a regular Arduino is powered up is because of the Arduino's Bootloader. If using an attiny, you would upload the program using ISP ("in-circuit serial programming"). This means no bootloader and no delay.
I liked these two circuits, but I want the power-off to be made by a push button and not only from Arduino itself. Arduino will make a buzzer or siren alarm for 2 minutes (example), but if I push a button, the siren will immediately stop and Arduino will return to power-off mode. Can I simply use a stop button somewhere in relay's coil??
Yes - use a button that breaks the circuit when pressed.
This can be placed in many points of the circuit, whatever convenient for your wiring. As long as it breaks the power to the relay coil it'll switch off the circuit immediately.
Is this circuit - the last I uploaded - good? Will it work? What do you think guys? As you can see (PaulRB uploaded it as a photo), when the door opens, a small relay with 2 pairs of contacts will immediately self hold and also power on Arduino. The circuit will power off, by an Arduino command, or by pressing a stop button.
Save for the Arduino off switch (how is that supposed to work?) and you suggesting to use a double relay this is exactly what I posted before - so yes, that'll work.