attiny85 tutorials

ATtiny85 LDR-Based Security Alarm System Using Arduino IDE


In today’s world, where security is a paramount concern for both homes and businesses, technological advancements have paved the way for innovative and cost-effective solutions. Among these, the ATtiny85 LDR-Based Security Alarm system stands out as a prime example. This compact system utilizes the ATtiny85 microcontroller in conjunction with a Light Dependent Resistor (LDR) to detect changes in light, signaling potential intrusions. Its simplicity, combined with its effectiveness, makes it an ideal choice for those seeking a reliable yet affordable security option.

Not only is this system energy-efficient and easy to install, but it also offers the flexibility of integration with other smart devices, enhancing overall security measures. Its appeal extends beyond just practicality; tech enthusiasts and DIY hobbyists find the ATtiny85 LDR-Based Security Alarm system particularly engaging due to its potential for customization and experimentation. In essence, this system represents a harmonious blend of modern technology and user-friendly design, making advanced security accessible to a wider audience.

Understanding the ATtiny85 LDR-Based Security Alarm

The ATtiny85 LDR-Based Security Alarm system stands out as a highly efficient and user-friendly security mechanism, leveraging the compact yet powerful ATtiny85 microcontroller in conjunction with a Light Dependent Resistor (LDR). This combination makes it an ideal choice for safeguarding both residential and commercial spaces, providing a robust yet affordable security solution.

The core component of this system, the LDR or photoresistor, is ingeniously simple yet effective. It operates on a straightforward principle: its electrical resistance varies in proportion to the amount of light it is exposed to. In bright conditions, the resistance drops, and in darker settings, it increases. This characteristic enables the LDR to act as a vigilant light-sensitive sensor.

The ATtiny85 microcontroller, renowned for its small size and low power consumption, is the brains of the operation. Despite its diminutive stature, it packs enough computational power to effectively process the signals from the LDR. When the LDR detects a change in light intensity—such as would occur if someone passes by it in a normally dark area—the microcontroller interprets this data and assesses whether the change is significant enough to suggest a security breach, like unauthorized entry or an intrusion attempt.

Upon detecting a potential security threat, the system springs into action, triggering an alarm. This alarm can be configured in various forms, such as a loud siren, flashing lights, or even sending a notification to the property owner’s mobile device. The flexibility in alarm options ensures that the system can be tailored to meet the specific needs and preferences of the user.

One of the most appealing aspects of the ATtiny85 LDR-Based Security Alarm system is its versatility and adaptability. It can be installed in a variety of environments, from the interiors of homes and offices to more exposed locations like gardens or garages. The system’s sensitivity settings can be adjusted, making it less prone to false alarms caused by minor or natural changes in light levels, such as those due to shifting clouds or the gradual change from dusk to darkness.

Additionally, the system’s low power consumption makes it an eco-friendly and cost-effective option. It can run on batteries for extended periods, reducing the need for frequent replacements and thereby minimizing maintenance efforts. This feature is particularly beneficial for remote or hard-to-reach installations.

The ATtiny85 LDR-Based Security Alarm system also offers scope for customization and expansion. For instance, it can be integrated with other security systems or smart home devices, enhancing its functionality and providing a more comprehensive security setup. DIY enthusiasts and tech hobbyists will find this system particularly appealing due to its simplicity and the potential for tinkering and modifications.

the ATtiny85 LDR-Based Security Alarm is a testament to the power of combining simple yet effective sensors with smart microcontroller technology. Its ability to provide reliable, customizable, and cost-effective security makes it an attractive option for a wide range of applications, from basic home security to more complex commercial security arrangements.

Key Features and Benefits

The ATtiny85 LDR-Based Security Alarm system offers several features and benefits that make it an ideal choice for enhancing security:

Easy Installation: The system is designed for easy installation, requiring minimal technical expertise. With clear instructions and a user-friendly interface, you can have it up and running in no time.

Customizable Sensitivity: The sensitivity of the LDR can be adjusted according to your specific requirements. Whether you need high sensitivity for maximum security or lower sensitivity to avoid false alarms, the ATtiny85 LDR-Based Security Alarm system can be easily customized to suit your needs.

Compact and Discreet: The small size of the ATtiny85 microcontroller allows for discreet installation, making it less noticeable to potential intruders. Its compact design also ensures that it can be easily integrated into any existing security system without taking up much space.

Low Power Consumption: The ATtiny85 microcontroller is known for its low power consumption, making it an energy-efficient option. This ensures that the security alarm system can operate for extended periods without the need for frequent battery replacements.

Cost-Effective: Compared to other security systems in the market, the ATtiny85 LDR-Based Security Alarm system offers excellent value for money. Its affordable price point and low maintenance requirements make it an attractive choice for homeowners and small business owners.


The ATtiny85 LDR-Based Security Alarm system can be used in a variety of applications, including:

Residential Security: Protect your home and loved ones by installing the ATtiny85 LDR-Based Security Alarm system at entry points such as doors and windows. It provides an added layer of security, giving you peace of mind even when you are away.

Office Security: Ensure the safety of your office premises by integrating the ATtiny85 LDR-Based Security Alarm system into your existing security setup. It can be easily connected to other devices such as CCTV cameras and door access control systems for comprehensive protection.

Storage Unit Security: Safeguard your valuable belongings stored in storage units by installing the ATtiny85 LDR-Based Security Alarm system. Its compact size and wireless capabilities make it an ideal choice for securing storage units.

Vehicle Security: Protect your vehicles from theft or vandalism by using the ATtiny85 LDR-Based Security Alarm system. Its sensitivity can be adjusted to detect any suspicious activity near your vehicle, triggering the alarm and alerting you instantly.

ATtiny85 Pinout:

ATtiny85 LDR-Based Security Alarm System

The ATtiny85 is a popular microcontroller from the AVR series by Atmel, now part of Microchip. Its pinout is as follows:

PB5 (Reset): This pin doubles as a reset input and a general-purpose I/O line.

PB3 (ADC3): This pin can be used as an analog input or general-purpose I/O.

PB4 (ADC2): Another analog input/general-purpose I/O pin.

GND (Ground): The ground connection for the circuit.

PB0 (MOSI/SDA/AIN0/OC0A): This multi-function pin can be used for SPI communication (MOSI), I2C communication (SDA), analog input (AIN0), or as a timer output (OC0A).

PB1 (MISO/AIN1/OC0B): Similar to PB0, this pin serves as SPI MISO, analog input, or timer output.

PB2 (SCK/ADC1/T0/INT0): This pin functions as the SPI clock (SCK), an analog input, a timer input (T0), or an external interrupt (INT0).

VCC (Power Supply): The power supply pin for the microcontroller.

This pinout arrangement allows the ATtiny85 to be versatile in small and simple embedded projects, where space and power consumption are critical factors. It’s widely used in DIY electronics, wearables, and small-scale automation projects.

Code uploading Circuit diagram:

When using the Arduino IDE with the ATtiny85 microcontroller, we need a specific circuit setup. This is essential because, unlike some other microcontrollers, the ATtiny85 does not support direct code uploads. The circuit typically involves making connections from the Arduino to the ATtiny85, which allow for the transfer of the compiled program from the Arduino IDE to the ATtiny85’s memory.

Please follow the circuit diagram provided below for uploading the code. If you encounter any confusion, I recommend reading my previous article, where I have explained everything in detail.

ATtiny85 LDR-Based Security Alarm System

ATtiny85 LDR-Based Security Alarm System Program:

After uploading the code remove the wires from the Arduino.

ATtiny85 LDR-Based Security Alarm System Circuit diagram:

ATtiny85 LDR-Based Security Alarm System

In this circuit, I am using a battery pack to power the project. The GND pin of the LDR module is connected to the GND of the ATtiny85 (pin number 4). The 5V pin of the LDR is connected to the VCC of the ATtiny85, (pin number 8), and the LDR’s Analog (A0) pin is connected to A2 ( pin number 3) of the ATtiny85. The GND pin of the laser is connected to the common GND, and its Signal pin is connected to PB3 (pin number 2) of the ATtiny85. The VCC and GND pins of the buzzer are connected to the VCC and GND of the ATtiny85, respectively, and the IO pin of the buzzer is connected to PB0 (pin number 5) of the ATtiny85. Here, the button is used to reset the alarm: once the alarm is activated, it can be turned off by pressing the button. The VCC and GND pins of the button are connected to the VCC and GND of the ATtiny85, respectively, and the signal pin of the button is connected to pin PB1 (number 6).

Code explanation:

The laserPin is an integer variable assigned to PB3, designating the digital pin connection for the laser module. Similarly, sensorPin is assigned to A2 and refers to the analog pin where the Light Dependent Resistor (LDR) sensor is connected, crucial for detecting changes in light. The buzzerPin, assigned to PB0, specifies the digital pin for the buzzer, which is an integral part of the alarm system. Additionally, buttonPin is linked to PB1 and represents the digital pin for a button used in the system.

Furthermore, laserThreshold is a critical integer variable, set at a value of 10, which serves as the threshold for the LDR sensor. This threshold value is pivotal in determining when the laser beam is interrupted, triggering the alarm system. Lastly, the boolean variable alarmTriggered is initially set to false and plays a crucial role as a flag in the system, indicating whether the alarm has been activated. This flag is essential for the control logic in the system, dictating the behavior of the alarm in response to changes detected by the LDR sensor.

The setup() function in the program is called a single time at the beginning when the program starts and is primarily used for initializing settings. Within this function, the pinMode command is used for configuring the pins on the microcontroller. Specifically, pinMode(laserPin, OUTPUT) is set, which configures the laser pin as an output, essential for controlling the laser module. Similarly, pinMode(buzzerPin, OUTPUT) is employed to set the buzzer pin as an output, enabling the program to control the buzzer. For the button, pinMode(buttonPin, INPUT_PULLUP) is used, which sets the button pin as an input with an internal pull-up resistor. This specific setup is advantageous as it negates the necessity for external pull-up resistors, simplifying the circuit design. Additionally, the function initializes serial communication at a rate of 9600 bits per second through Serial.begin(9600).

The loop() function is a critical part of the program that starts running continuously once the setup() function has finished executing. It encapsulates the core logic of the program. At the beginning of this function, digitalWrite(laserPin, HIGH) is called to turn on the laser, followed by a brief delay(10) to allow the laser light to stabilize. The program then checks if the alarm has not yet been triggered using the condition if (!alarmTriggered). Within this condition, the value from the LDR sensor is read using int sensorValue = analogRead(sensorPin) and then printed to the serial monitor with Serial.println(sensorValue). This step is crucial for monitoring the sensor’s response to light changes.

If the sensor value surpasses the predefined laserThreshold, it implies that the laser beam has been interrupted. In response, the buzzer is activated at 440 Hz using tone(buzzerPin, 440), and the alarmTriggered flag is set to true, indicating that the alarm system has been activated. On the other hand, if the alarm has already been triggered, the program checks whether the reset button has been pressed (if (digitalRead(buttonPin) == LOW)). If the button is pressed, the buzzer is turned off with noTone(buzzerPin), and the alarmTriggered flag is reset to false, effectively resetting the alarm system. Finally, another brief delay(10) is added at the end of the loop for stability. This loop function is the heart of the system, enabling the laser security mechanism to detect interruptions and sound an alarm, with the capability of being reset by a button press.


The ATtiny85 LDR-Based Security Alarm system offers a reliable and cost-effective solution for enhancing security in residential and commercial settings. With its easy installation, customizable sensitivity, compact design, and low power consumption, it provides an effective deterrent against potential intruders. Whether you want to secure your home, office, storage unit, or vehicle, this innovative security system is a valuable addition to any security setup. Invest in the ATtiny85 LDR-Based Security Alarm system and enjoy the peace of mind that comes with knowing your property is protected.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button