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