Friday, November 14, 2014

How to Combine a Vibration Sensor and Reed Switch to Detect Door Open or Tailgate Down

In this project I show how to make a door open or tailgate down sensor using an Arduino vibration sensor and Reed switch. This builds on my previous blogs of how to build a high sensitivity vibration sensor, and how to avoid false positives with a high sensitivity piezo vibration sensor. In this latest project I use a Reed switch to detect if something is open, for example a door or a tailgate. The sketch running on the Arduino Uno only records vibration events if the Reed switch is open. In order to trigger the vibration alarm it must receive at least 5 vibration events within a 5 second period (configurable in sketch below). Each vibration event is represented as a square pulse out of the comparator in the vibration sensor circuit. When the vibration alarm is triggered it fires the large LED . This could be placed in the view of a rear view mirror for example, to alert the driver that the car or truck is in motion and something is left open, for example a tailgate may be down.

Below is the sketch I used, running on the Arduino Uno:

#define VIBRATION_SENSOR_DIGITAL_INPUT_PIN 10
#define VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN 8
#define REED_SWITCH_DIGITAL_INPUT_PIN 6

#define VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT 5
#define VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS 5000

#define VIBRATION_ALARM_FLASHES 5
#define VIBRATION_ALARM_PERIOD_MILLIS 400

long vibrationEvents[VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT];
int vibrationEventIndex;

//------------------------------------------------------------------
void setup(){
  Serial.begin(19200);
  
  pinMode(VIBRATION_SENSOR_DIGITAL_INPUT_PIN, INPUT);
  pinMode(REED_SWITCH_DIGITAL_INPUT_PIN, INPUT);
  pinMode(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, OUTPUT);
  
  vibrationEventIndex = 0;
  clearVibrationEvents();
}

//------------------------------------------------------------------
void loop(){
  // only pay attention to vibration when the Reed switch is open
  if(digitalRead(REED_SWITCH_DIGITAL_INPUT_PIN) == HIGH && 
     digitalRead(VIBRATION_SENSOR_DIGITAL_INPUT_PIN) == HIGH){
       
    addVibrationSample();
    delay(100); // debounce current vibration shock
  }
}

//------------------------------------------------------------------
void addVibrationSample(){
   vibrationEvents[vibrationEventIndex++] = millis();
   
   // wrap index around end of sample array
   vibrationEventIndex %= VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT; 
   
  if(isVibrationAlarmTiggered()){
    Serial.println(String(millis()) + "\t ALARM");
    triggerAlarm();
  }
}

//------------------------------------------------------------------
boolean triggerAlarm(){
  for(int i = 0; i < VIBRATION_ALARM_FLASHES; ++i){
    digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, HIGH);
    delay(VIBRATION_ALARM_PERIOD_MILLIS/2);
    digitalWrite(VIBRATION_SENSOR_DIGITAL_OUTPUT_PIN, LOW);
    delay(VIBRATION_ALARM_PERIOD_MILLIS/2);
  }
  clearVibrationEvents();
}

//------------------------------------------------------------------
void clearVibrationEvents(){
  for(int i = 0; i < VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT ; ++i){
    vibrationEvents[i] = -1;
  }
}

//------------------------------------------------------------------
boolean isVibrationAlarmTiggered(){
  long thresholdMillis 
    = millis() - VIBRATION_EVENTS_ALARM_THRESHOLD_PERIOD_MILLIS;
    
  if(thresholdMillis < 0) thresholdMillis = 0;
  int numVibrationsSinceThreshold = 0;
  for(int i = 0; i < VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT ; ++i){
    if(vibrationEvents[i] >= thresholdMillis){
      ++numVibrationsSinceThreshold;
    }
  }
  
  Serial.println(
    String(millis()) + 
    "\t# events: " + 
    String(numVibrationsSinceThreshold));
    
  boolean alarmTriggered = false;
  if(numVibrationsSinceThreshold >= VIBRATION_EVENTS_ALARM_THRESHOLD_COUNT){
    alarmTriggered = true;
  }
  
  return alarmTriggered;
}

//------------------------------------------------------------------

If you are interested in how this type of sensor can be integrated into a broader solution that includes notification of a door open on a users Android phone see Detect if a Door or Gate is Opened

11 comments:

  1. Thanks for sharing this amazing and nice post with us,.
    gardens

    ReplyDelete
  2. Hi. If you're interested in IoT, check out internet of things - ax-dynamics.com. I learned a lot of interesting facts there. Do you recommend IoT as the small company solution?

    ReplyDelete
  3. Right off the bat, the telemetry level sensor is the device which is going to track the level of liquid, chemical and gas. Of course, it has different purposes and that's where the complications stem from. We are going to take a look at the things you'd need to consider and the questions you'd have to answer prior to choosing a regular telemetry level sensor. Visit for More details about Telemetry Tank Level Sensor

    ReplyDelete
  4. How Mr Benjamin Lee service  grant me a loan!!!

    Hello everyone, I'm Lea Paige Matteo from Zurich Switzerland and want to use this medium to express gratitude to Mr Benjamin service for fulfilling his promise by granting me a loan, I was stuck in a financial situation and needed to refinance and pay my bills as well as start up a Business. I tried seeking for loans from various loan firms both private and corporate organisations but never succeeded and most banks declined my credit request. But as God would have it, I was introduced by a friend named Lisa Rice to this funding service and undergone the due process of obtaining a loan from the company, to my greatest surprise within 5 working days just like my friend Lisa, I was also granted a loan of $216,000.00 So my advise to everyone who desires a loan, "if you must contact any firm with reference to securing a loan online with low interest rate of 1.9% rate and better repayment plans/schedule, please contact this funding service. Besides, he doesn't know that am doing this but due to the joy in me, I'm so happy and wish to let people know more about this great company whom truly give out loans, it is my prayer that GOD should bless them more as they put smiles on peoples faces. You can contact them via email on { lfdsloans@outlook.com} or Text through Whatsapp +1-989 394 3740.

    ReplyDelete
  5. Although this blog was very informative, I wanted to share some of my thoughts. For Automated website testing TestNG, a Java testing framework can drive the Selenium Automation script. Selenium is a mixture of several tools. Each tool/block plays an important role in web-app/website testing automation. It is free and can automate testing web applications in different browsers such as Firefox, Chrome, Internet Explorer, Microsoft Edge. Selenium, one of the most popular open-source automation tools, has been compatible with multiple programming languages (Java, JavaScript PHP, Python, C# Ruby, and PHP).

    ReplyDelete
  6. Extremely useful case! There is loads of suggestion here that could previously any situation income started further to a well off person to person communication cognizant up assessment. Smartdraw Crack

    ReplyDelete
  7. you have executed an uproarious errand upon this text. Its completely adjust and very subjective. you have even figured out how to make it decipherable and simple to make a get accord of into. you have a couple of precise composing competence. much obliged appropriately a lot. Birthday Wishes To My Love

    ReplyDelete
  8. Your blog gives a lot of information that should be passed on to people
    buy Instagram pva accounts

    ReplyDelete