Matha Goram
Published © GPL3+

Check Your Vibes

The vibration sensor is a low-cost solution for simple applications where impact (e.g. patting a toy) translates to a switch event.

BeginnerProtip30 minutes4,477
Check Your Vibes

Things used in this project

Hardware components

Elegoo Arduino UNO R3
×1
DT-MEAD Vibration Sensor
×1
Elegoo Resistor 1M ohm, 5%
×1
Breadboard, full-size
×1
Elegoo Dupont
×5
Baseplate
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Assembly (digital pin)

Assembly (analog pin)

Schematics

Vibration sensor digital pin test

Basic test for MEAS-DT piezo vibration sensor using digital pin

Vibration sensor analog pin test

Basic test for MEAS-DT piezo vibration sensor on analog pin

Code

MEAS-DT-01.ino

C/C++
Basic test for MEAS-DT piezo vibration sensor on digital pin
/*
 * MEAS-DT-01.ino
 * Basic test for MEAS-DT piezo vibration sensor on digital pin
 * Copyright (C) 2019 baqwas (use Gmail)
 * 
 * 2019-06-05 armw initial DRAFT
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or  any later version. 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 * 
 */

#define pinVIBE 2                     // digital pin for piezo vibration sensor

int vibeMarker = 100;                 // threshold for vibration status
boolean vibrating = false;            // =true if vibration detected

void ISR_vibration()                  // handle pulse change interrupt signal
{                                     // keeping the code inside the ISR as simple as practical
  vibrating = true;                   // yes!
}

void setup()
{
  Serial.begin(115200);               // initialize serial port
  /*
   * The UNO R3 board supports two interrupt numbers: 
   * INT.0 on digital pin 2
   * INT.1 on digital pin 3
   */
  attachInterrupt(                    // assign external interrupt handler
    digitalPinToInterrupt(pinVIBE),   // map digital pin to interrupt number - see above
    ISR_vibration,                    // interrupt service routine invoked upon detection
    RISING);                          // trigger when pin goes from low to high
}

void loop()
{
  if (vibrating)
  {                                   // RISING occured
    vibrating = false;                // reset flag for next interrupt
    Serial.print("Timestamp: ");      // simple example only
    Serial.print(millis());           // customize to suit your needs
    Serial.println(", triggered!");   // e.g. (for your own exercise)
                                      // if more than x interrupts in y seconds
                                      //   raise general alarm
  }
  delay(100);                         // nominal delay to catch your breath
}

MEAS-DT-02.ino

C/C++
Basic test for MEAS-DT piezo vibration sensor on analog pin
/*
 * MEAS-DT-02.ino
 * Basic test for MEAS-DT piezo vibration sensor on analog pin
 * Copyright (C) 2019 baqwas (use Gmail)
 * 
 * 2019-06-05 armw initial DRAFT
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or  any later version. 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 * 
 */

const int pinVIBE = A0;               // analog pin for piezo vibration sensor

int vibeMarker = 100;                 // threshold for vibration status
                                      // leaving vibeMarker as var for adaptive tuning
int vibeOutput = 0;                   // will retain current output from sensor

void setup()
{
  Serial.begin(115200);               // initialize serial port
}

void loop()
{
  vibeOutput = analogRead(pinVIBE);   // current output from vibration sensor
  Serial.println(vibeOutput);         // temporary use to understand the range
                                      // of values from the vibration sensor
  if (vibeOutput > vibeMarker)
  {                                   // sensor output has breached threshold
    Serial.println("Triggered!");     // e.g. (for your own exercise)
                                      // if more than x for y seconds
                                      //   raise general alarm
  }
  delay(100);                         // nominal delay to catch your breath
}

Credits

Matha Goram

Matha Goram

27 projects • 22 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.

Comments