Lesson 9 - Basic Alarm system

We have now covered a few key areas of Arduino Programming. We are now ready to write our first complex program to build a system using the Arduino. The way in which we will conduct this lesson is by following a specification.

Program Description

The program we will be writing in this lesson will be a simple alarm system. We will be using the Arduino in conjunction with the the Multi Function Shield to use the different sensors and components to accomplish this. The alarm system will have three states it can be in. The first will be a disarm state where no action is being performed even if triggered. The second will be an armed state where still nothing is happening but if triggered the alarm will be aware to activate. And the third state being the trigger state which activates an alarm if the system is armed. This basic alarm system has many real life applications.

Program Specification

1. To begin with, you will need a button to control between the armed and disarmed states. If this button is on, the alarm is armed, if the button is off then the alarm is disarmed.

2. Whilst in the disarmed state, nothing must be happening except the 4x7 segment display must read 'off'. Also, in the disarmed state, the alarm cannot be triggered.

3. When the alarm is armed, the display must read on, and there must be a visual indication to show that the alarm is armed.

4. Only whilst the alarm is armed, if button 3 on the MFS is on, the alarm will trigger. We are using button 3 as our trigger sensor. When triggered, a different visual indication and an audio indication must show that the alarm has been triggered. Until the system is not reset the alarm must loop.

5. To reset the alarm, users must simply disarm the alarm using the first button.


Note: We will be going through the steps in completing this program as all programming techniques and functions required for this program have already been covered in previous lesson through the course. The final source code will be included at the end. Remember on the MFS the digital commands are reversed, HIGH = OFF and LOW = ON. Refer to the source code for guidance.

Global Scope - To start of our programming sketch, we will begin by declaring all required information in the global scope. We will first include any libraries we can use within the program. For this system we will be using the 4x7 segment display and therefore we will need to use the MFS library and the timer one library. We must also declare any global variables which can be used in any function throughout the program. We will need to declare two LEDs, two push buttons and the buzzer. The buzzer will be used to give an audible indication for the alarm and it is connected to digital pin 3 on the MFS. After the digital pins have been declared, we also need to declare digital variables for the two separate buttons to determine their digital status. (Refer to Lesson 5) Remember to group the variable names to avoid syntax errors and confusions.

Setup - Moving on to our setup, we need to configure the pin mode for all digital components which will either be used as inputs or outputs. Then we will initialise the timer library, followed by the MFS library which is initialised in conjunction with the timer.

Loop - In the main loop section of our program, we need to write instruction sets to detect whether the alarm is armed or disarmed. Taking code efficiency in to consideration, we will use two individual subroutine functions (Arm and Disarm). Before we can move to the functions, in the loop we will program button 1 to be used as a toggle switch. When the button is on, the arm subroutine will be called for and alternatively, when the button is off, the disarmed subroutine will be called for. (Remember ON = LOW and OFF = HIGH)

Function 1 (Arm) - Whilst button 1 is toggled as low (on), this will call for the Arm subroutine. The program specification states that when the alarm is armed, the MFS must read as 'on' and there must be an LED indication. During this state, if button 3 is toggled to be on, the alarm must trigger and a different LED must show this, as well as a buzzer alert. The way we will code this is by again writing the instruction set for button 3 and when the button state is low (on) the alarm will activate and if button 3 is high (off) the trigger state will exit and reset the alarm.

Function 2 (Disarm) - Whilst button 1 is toggled as high (off), this will call for the Disarm subroutine. When the alarm is disarmed, the MFS display must read as 'off' and there should be no operation happening until button 1 is toggled again to exit this subroutine. To avoid bugs and errors, we will add code to turn off all LEDs and the buzzer in this subroutine.

This program is now complete and the source code is available below to refer to. When using the MFS it is important to remember the digital states are reversed due to the electronic configuration internally.

Source Code

We recommend copying the source code to the IDE


By Zaqyas Mahmood, Electronics Engineer