Setting up the Sketch

Since we don't want our motion detector to alert us of daytime activities (like a dog passing by, or a skateboarder), we'll need to give the sketch (prior to uploading) the current hour, minutes and seconds. In the sketch, set startingHour, currentMinutes, and currentSeconds to the current time (we're using 24-hour time for this sketch). For the sketch to work properly, you'll need to change these values on every upload.

  • While we're not using one for this project, you can add a Real-Time-Clock (like the RTC FeatherWing) if you'd like more precise timekeeping. 
/**** Time Setup ****/
// set the current hour
int startingHour = 0;
// set the current minutes
int currentMinutes = 0;
// set the current seconds
int currentSeconds = 0;

Code Overview

The first chunk of the code (setup()) starts a MQTT connection to the Adafruit IO server. We also add three message handlers to handle changes from the indoor-lights, outdoor-lights, and home-alarm feeds from the dashboard blocks. We also set up the PIR sensor, reed switch, SGP30 sensor, and initalize the NeoPixel strip.

void setup() {
  // start the serial connection

  // wait for serial monitor to open
  while(! Serial);
  Serial.println("Adafruit IO Home: Security");
  Serial.println("Connecting to Adafruit IO");
  // start MQTT connection to
  // attach a message handler for the `home-alarm` feed
  // subscribe to lighting feeds and register message handlers

  // wait for an MQTT connection
  // NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
  // method to check on MQTT connection status specifically
  while(io.mqttStatus() < AIO_CONNECTED) {
  // we are connected
  // declare PIR sensor as input
  pinMode(pirPin, INPUT);
  // declare reed switch as input
  pinMode(doorPin, INPUT);
  // set up the SGP30 sensor
  // init the neopixel strip and set to `off`

The next chunk of code (loop()) calls which keeps the client connected to Adafruit IO. Next, it makes a call to getTime() which calculates and returns the current time. Then we read the door, SGP30, and PIR sensor (readDoorSensor, readPIR, readSGP30) and send their values to their respective Adafruit IO feeds.

void loop(){;

  Serial.println("* read door sensor...");
  Serial.println("* read motion detector");
  Serial.println("* reading SGP30...");

The toggle button on your Adafruit IO dashboard is connected to the handleAlarm message handler. When the toggle button's value is changed, it receives the current value of the feed and checks if it's ON or OFF and sets the isAlarm boolean.

void handleAlarm(AdafruitIO_Data *data) {
// handle the alarm toggle on the Adafruit IO Dashboard
  String toggleValue = data->toString();
  Serial.print("> rcv alarm: ");
  if(toggleValue == String("ON")) {
    Serial.println("* Alarm Set: ON");
    isAlarm = true;
  } else {
    Serial.println("* Alarm Set: OFF");
    isAlarm = false;

Finally, we'll check if the alarm is armed (isAlarm==true). If the alarm is armed, we check if the door is open (doorState == HIGH) or if the hour is later than alarmHour and if the motion detector is triggered, we'll make a call to the playAlarmAnimation() function.

  // check if the alarm toggle is armed from the dashboard
  if (isAlarm == true) {
    if (doorState == HIGH || (hour>alarmHour && pirState == HIGH)) {

Using the Smart Home Security System

Connect your Feather Huzzah to USB and upload the io_home_security sketch. 

Open up the IO-Home dashboard to interact with your smart home. Push the door open. You should see the Front Door Indicator change from green to red.

Switch the toggle from OFF to ON to arm your house's security system. When you push the front door open (or move past the PIR sensor past the time set by alarmHour), the inside lights will blink red and the piezo will make noise.

Tired of the alarm? Want some peace and quiet? Toggle the alarm OFF.

This guide was first published on Sep 05, 2018. It was last updated on Mar 08, 2024.

This page (Arduino Code) was last updated on Mar 08, 2024.

Text editor powered by tinymce.