RPi – Project #1 – Alarm Circuit

The first four episodes of the Cooking with Team 279 series guided participants through the basics of using a Raspberry Pi.  Topics covered included:

  • Intro to the Pi
  • Python basics
  • Controlling an LED
  • Using a button
  • Getting analog input

Having covered these basics, we will now build upon them by having you, the reader, build a complete circuit on your own to tie the topics together.  The steps to do this will not be provided, but the article below describes what to do.


Why is this project structured this way?

Most newcomers to electronics and programming will find that topics seem very easy, but that they don’t remember or truly understand them until implementing on their own without all the specific steps being provided. Taking the time to complete projects like this is very important to learning and understanding.

Don’t skip this! Finish this project completely!

By the time you’ve finished working through this, you should be comfortable that you understand the basics of working with a Raspberry Pi and simple circuits, and can look up basic howto info on the relevant topics. And while that may not seem like much, many complex projects can be completed by knowing these fundamentals, and customizing as needed.



The circuit to build will be a simple alarm circuit.  The circuit should have these elements:

  1. A button to arm, disarm, and reset the circuit
    1. Optionally, using a second button for resets may be desired. It is up to you
  2. A LED to indicate that the circuit is armed or not (we suggest a green color)
  3. A LED to indicate that the alarm is tripped or not (we suggest a red color)
  4. A sensor to detect if the alarm should be tripped
    1. Many different types of sensors can be used, but an analog sensor should be used to rely on the ADC
    2. We suggest TRCT5000 Reflective IR Sensor (or similar), as it is an inexpensive way to simulate a door/window breach


The complete project should function as follows:

  1. Power on the Pi with the circuit
  2. Start the alarm program
    1. All LEDs should start off
    2. The sensors input should be ignored
  3. Push the main button to arm the alarm
    1. Only allow this if the sensor indicates that the the site is “safe”, ie, the door is closed, etc..
      1. If it is not “safe”, print a message and do not arm
    2. If the circuit successfully arms, turn on the first LED to indicate that
  4. Once the circuit is armed, wait for the sensor to indicate that a breach has occurred (ie.. a window/door was opened)
  5. Set the alarm to a tripped state, and flash the red alarm LED until the disarm button is pushed
  6. Once the disarm button is pushed, the alarm should return to the starting state (alarm not armed, input ignored, LEDs off)


Suggested Approach

Remember that you’ve done each element of this program individually before.  Look back at the previous articles for help with a given piece.

It is suggested to test the LEDs and buttons separately to ensure the circuit is working correct before starting the main alarm program, so that you can isolate any issues between code or circuit more quickly.


Create variables that will track the state of each LED, and the overall state of the alarm.  So at a minimum, you’ll need variables for:

  • Alarm circuit armed or not
  • Alarm tripped or not
  • Armed LED on or off
  • Tripped LED on or off

The button (or buttons if you use more than one), will be used to change the value of the armed/tripped variables, and thus allow arming and disarming.

The Analog Sensor value will be used to set  the “alarm tripped” variable to true, IF the alarm is armed

The program structure should look like:

global variables (armed, tripped, LED on/of, etc..)
Functions (button pressed, etc..)
Main Loop


Inside the main loop, the program should should:

  1.  Check the value of the variables
  2. Respond to sensor input (if armed)
  3. Set the state of the LEDs
  4. Sleep briefly



Good Luck!