Read this article to checkout an ATtiny85 based intruder alarm.
iNTRODUCTION
An ATtiny85 is the perfect chip for a concealed intruder/security alarm. This small and compact chip can be powered only through a single button cell (CR2032). Additionally, you may customize your alarm sound through various tones. You can also add different functions to it through the open-source Arduino library.
Parts List
- ATtiny85 Chip
- Small 3-5V Buzzer
- Resistor LED combination
- AM312 PIR Motion Sensor
- A prototyping board or breadboard
ThE AM312 Mini PIR MOtion Sensor

Last time, the HC-SR501 sensor was discussed. This versatile PIR sensor can detect living things from about 7 meters maximum distance away from it. Now, this project uses a nearer detection distance (about ~3m). A smaller form factor is also needed to conceal the intruder alarm’s presence. Additionally, the quiescent current of the sensor should be very low (only at 8uA) to run it on a single-button cell battery. Based on these requirements, the AM312 Mini PIR Motion sensor was chosen as the PIR sensor.
Writing Code for thE ATtiny85 Chip
Below is the code for the intruder alarm.
#include
#include // needed for the additional interrupt
// Routines to set and clear bits (used in the sleep code)
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define led_pin 3
#define PIR_sense 0
#define Buzzer 4
// Interrupt handlers
ISR(PCINT0_vect){ // PB0 pin button interrupt
}
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
void alarm_siren(void);
void setup() {
// put your setup code here, to run once:
PCMSK = 0b00000001; // pin change mask: listen to portb bit 0
GIMSK |= 0b00100000; // enable PCINT interrupt
sei(); // enable all interrupts
pinMode(PIR_sense, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(Buzzer, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(PIR_sense) == 1)
{
digitalWrite(led_pin, HIGH);
alarm_siren();
//tone(Buzzer, 100, 500);
digitalWrite(led_pin, LOW);
}
system_sleep();
}
void alarm_siren(void)
{
byte i;
for(i=0;i<10;i++)
{
tone(Buzzer, 1000, 100);
delay(100);
tone(Buzzer, 5000, 100);
delay(100);
}
}
You’ll want to enable sleep mode for the circuit to have a very low power consumption. With this, you’ll need additional includes, defines, and functions:
#include
#include // needed for the additional interrupt
// Routines to set and clear bits (used in the sleep code)
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
// Interrupt handlers
ISR(PCINT0_vect){ // PB0 pin button interrupt
}
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
PB0, the input pin used for detecting motion presence on the AM312 sensor, is the same pin used to wake the ATtiny during sleep. An interrupt vector PCINT0_vect is used to detect this interrupt.Â
The system sleep( ) function will be invoked in the main loop. During setup, PB0 is set up as an INT pin, which is connected to the PIR motion sense pin. The led_pin and Buzzer pins are also set up here which are connected to pins PB3 and PB4 respectively.
void setup() {
// put your setup code here, to run once:
PCMSK = 0b00000001; // pin change mask: listen to portb bit 0
GIMSK |= 0b00100000; // enable PCINT interrupt
sei(); // enable all interrupts
pinMode(PIR_sense, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(Buzzer, OUTPUT);
}
The main loop looks out for the PIR sensor’s active signal. It will invoke a custom alarm sound through the alarm_siren( ) function if it does. Note that the loop sleeps and is woken up on the same PIR sensor active signal.
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(PIR_sense) == 1)
{
digitalWrite(led_pin, HIGH);
alarm_siren();
//tone(Buzzer, 100, 500);
digitalWrite(led_pin, LOW);
}
system_sleep();
}
void alarm_siren(void)
{
byte i;
for(i=0;i<10;i++)
{
tone(Buzzer, 1000, 100);
delay(100);
tone(Buzzer, 5000, 100);
delay(100);
}
}
Programming the ATtiny85 chip
Programming the ATtiny85 is similar to what was done in this blog. However, a different ATtiny core is used here. The core is the ATtiny core by Spence Konde. This core enabled the tone function missing in the Damellis ATtiny core. Follow this guide (similar to our guide) to install the board files. You may use the ATtiny85 (Micronucleus/DigiSpark) as board type or others that may apply to your setup.
Live Circuit
Below is the live circuit during programming and intruder detection.
SHOP THIS PROJECT
-
Mini PIR Motion Sensor Module – AM312
$15.95Original price was: $15.95.$14.95Current price is: $14.95. Add to cart