Magnetic contact on door, buzzer alarm and Arduino on battery

I wonder how they do the programming over USB. Would be very useful. I'm using the 6-pin SPI header for that, and actually rolled my own development board on a piece of protoboard and some pin headers, but also using a solderless breadboard for it (the ATtiny 25/45/85 come in DIP package; others only in SMD so need a breakout board for those).

The board as such, maybe not too great. You can't remove the processor to place it in your project which is what you'd normally do with this kind of projects (otherwise you can just as well take a Pro Micro).

Do note that the ATtiny is a LOT harder to deal with than the Arduinos (those are meant to be easy, the ATtiny not: that's just the barebones processor). If this is your first survey in these processors and you have no intention to continue with it - stick to the Arduino line and use a Mini or Pro Micro for this. OTOH if you want to develop this in a more serious hobby and have more such little projects, by all means go for it. Just be aware of what you're getting yourself in to. Debugging the little things is a headache as communication is so hard.

I suppose there is no great need for miniturisation on this project, so maybe stick to the Nano.

I have had zero success with digispark up to now. But that is something to do with Ubuntu not recognizing it as a USB device. Users of Windows don't seem to have a problem with digispark. This is very unusual, normally everything just works with ubuntu. I must try to find time to solve that.

Normally I just use tiny84 & 85 in dip format on breadboard. I started out using a Nano as the programmer but now I use a usbasp programmer.

And I can't buy them in my city either. That's normal. Most stuff I buy online.

Hm, I will probably make this project with Arduino, since I am more familiar with it. I am between Nano and Pro Mini. I see that they have many similarities, but I can not understand their main difference. Probably I can use either Nano or Pro Mini.

And something last: Someone told some posts before that I have to connect -12V to GND Pin in my circuit. However if I feed Arduino with 12V DC from battery, wouldn't automaticaly provide -12V to GND Pin? I thought that you can connect DC power to jack and then you can take the same ground from ground Pin and you don't have to connect Ground Pin to DC negative. Am I wrong?

wvmarle:
I wonder how they do the programming over USB.

They bit bang USB protocol from SW, using "bootloader" to selfprogram memory. IIRC getting them working on Windows need some drivers installed but it is possible.

@OP if you are interested in other low power project it is better to use Arduino only for prototyping and for "final" project use stand alone processor. It is good time to start now to learn how to do it. If this is your only low power project I think cost difference between ATTiny and small Arduino is not worth the time you spend by learning.

Nano has a usb to serial converter built-in, to allow you to upload your sketch. With the Pro Micro, you need a separate usb to serial converter module. This makes Pro Micro slightly cheaper and slightly lower power consumption, in theory.

I think you must be slightly confused about the battery terminals. There is no -12V connection, only +12V and 0V/ground. So, yes, if you connect the battery via the barrel connector, you have already made a common ground. Some beginners think you can just connect 12V to the Vin pin and forget the ground connection.

Sorry, I made mistake by writing -12. I mean 0V. By the way, I tested with my multimeter that even if I power Arduino with 12 Volts, the Digital pins give 5 volts output. So, do I have to make any calculations about the resistors that I have to use in my two transistors?

Yes, the Arduino operates at 5V. It has a regulator on its board which converts 12V to 5V. But it is not difficult to overload this regulator and cause it to overheat and fail.

When you use a transistor with an Arduino, it is normally used as a "switch" to control a higher voltage or a higher current than the Arduino is able to. This is like a relay, but more efficient, faster, smaller, cheaper, more reliable, quieter.

There are two main types of transistor. The older type is called a Base Junction Transistor (BJT). This type amplifies a small current into a large current. When a small current flows into the BJT's base pin, a larger current can flow into its collector pin. But you must use Resistor to limit the current that flows into the base pin, or the Arduino pin can be damaged. The BJT has a "gain" which tells you how many mA must flow into the base to allow a higher mA to follow into the collector. Let us suppose this gain is 50x. For a collector current of 100mA, you would need a base current of at least 100/50 = 2mA. To make sure that the BJT does not overheat, we like to provide more base current than is needed, for example 5mA, which is 0.005A. To limit the current to 5mA, a resistor is needed between the base pin and the Arduino pin. If the output of the Arduino pin is 5V, then the resistor should be about 5/0.005 = 1000R or 1K.

So, if I use a transistor like MPSA13 (its hfe gain is over 3.000), I have to use a huge resistor?
On the other hand if I use a transistor like 2n2222 (its gain is smaller), I can use 1k resistor?

Am I right?

You don't have to use a big resistor. On the contrary - basically the smaller the better, as long as you don't overload your pins. The Arduino can supply 20 mA so your resistor should be at least 250Ω - or a little lower if you take the BE voltage drop of about 0.7V into account - so 20 mA at 4.3V means a 215Ω absolute minimum. But to be safe, use a 270Ω or higher value. 1k is fine, too, but the more you limit the base current the

The purpose is here to drive the transistor into saturation, and have less power losses so less heat there.

That MPSA13 is a darlington transistor, so basically two transistors in series. That's how you get the high gain. You could use a larger resistor here but don't have to.

For such power switching I prefer MOSFETs as they're voltage operated rather than current operated. So as long as you supply sufficient voltage to the gate (5V is enough for logic level MOSFETs) they conduct, without drawing current from your Arduino pin.

hey guys, I noticed a mistake. Lets say that I open the door, I get in the room and I push the button to reset the alarm. When I want to leave the room, I will reopen the door and so the alarm will be enabled again after I get out of the room. The system does not recognise if someone enters or leaves the room. It just activates every time the door opens. However I want the system to not be activated when I open the door to leave the room. How can I make it? I'm thinking of another small relay that will deactivates the reactivation, with another switch that I have to turn on before I leave, but I don't know..

Before you leave the room, push the silence alarm button. This starts a 30s countdown, giving you time to leave & close the door.

How can I make this? Have in mind that Arduino will be activated only when the door opens. All the other time it will be off (I made it like this because I don't have power supply, but battery).

unsigned long doorClosed = 0;
bool silence = false; // button not pressed.
bool door = true; // Door is open or this wouldn't start up.

// Alternative - delay to allow opening and closing the door without reactivating, add this line:
unsigned long pressedTime;

void setup() {
  soundAlarm(true); // Door opens, program starts, alarm!
}

void loop() {
  if (silenceButtonPressed()) {
    silence = true;  // button has been pressed.
    soundAlarm(false);

    // Alternative - delay to allow opening and closing the door without reactivating, add this line:
    pressedTime = millis();
  }

  // Reactivate upon door closed.
  if (doorOpen() == false && silence == true) { Door closed and someone has pressed the silence button.
    switchOff();
  }

  // Alternative - 5-minute delay to allow opening and closing the door without reactivating.
  if (doorOpen() == false && silence == true && millis() - pressedTime > 300000) {
    switchOff();
  }


  if (mills() > 600000) { // After 10 minutes of sounding the alarm, switch off.
    switchOff();
  }
}

Sound the alarm when the door is opened and the Arduino starts up. No delays or anything here.

Switch off the alarm when the silence button is pressed (it won't come on at all before the door is closed and reopens, and the device is reactivated that way).

Switch off the device itself the moment the door closes & the silence button was pressed (the alternative adds a 5-minute delay here if you keep the door closed while you're in that room, or it will reactivate when you leave the room).

Switch off the device after 10 minutes of alarm.

Hm.. I am a little bit confused about this: Does it need the Arduino to be powered on for all these functions? Because when someone enters the room and press the Stop button/switch, then Arduino will be Off. So how can Arduino (that is now off) understands if the silence button is pressed or all the other things in your example? Do I have to keep Arduino on for all the time that I am inside the room? Sorry for my misunderstanding.

(As you show in my circuit, the only reason I want Arduino to be off and I used the relay, is because the battery, if I had power supply I would have done it different).

thanks for your time

Yes - it'd have to be on, or it would lose it's state.

If you really want to have it switch off that time (it doesn't draw much power anyway) you would have to save the state to EEPROM, then check that every time upon startup.

It looks like it is more and more complicated. Maybe you want to reconsider the sleep + interrupt way?

So, the plan is Arduino to be Off when we leave the room, power on when someone enters and stay On during the time someone is inside. Then he press the apropriate button and when he leaves the Arduino is again Off, till someone reopens the door to get inside. Right?
If yes, my only concern is if battery runs out soon because of Arduino staying on while someone's inside.

ATMega (most important chip in Arduino) or ATTiny may "run" from a coin battery for years in deep sleep waiting for button press/signal from opening the door. As soon as you learn how to modify Arduino for minimal power consumption or how to use standalone chips (better solution) you will be able to do everything what this project needs with no need for cutting power, no need to try to figure out if someone is still inside etc.

ok. I will look for the "sleep" way, to see what's going on with this. Do you have any good articles about Arduino sleep mode to look at? I mean something easy and comprehensive, so to understand and study this way.

For example I found this and it seems easy and good:

http://www.engblaze.com/hush-little-microprocessor-avr-and-arduino-sleep-mode-basics/