Vibration Sensor Options

Tags: #<Tag:0x00007fe21f2d8a08>


Hi Guys!

I am new to the Particle community, and quite frankly pretty green when it comes to electronics and coding.
I have purchased a particle photon, and am excited to dive in on all of its iot capabilities. The first project I would like to accomplish is the following:

Attach and monitor a simple, yet sensitive vibration sensor.
I would like the particle photon to wake up when motion is first detected, and then record data or begin a certain function when vibration reaches a certain frequency.

For example: when the vibration sensor detects liquid moving through a pipe, x function turns on. And when liquid stops running through the pipe, after 10 seconds the x functions turns off.

I would like to know what would be a good vibration sensor/accelerator to help me accomplish this. Something that is friendly with the Photon.
Also where could I find code to help me accomplish this? Any coding styles or programs that you would recommend to someone who has only coded so much as a web page?? lol



I think you could use a LIS3DH accelerometer.

It could let you easily measure vibrations. It also has a configurable interrupt pin that you could use to wake the photon from sleep.

The LIS3DH is very popular and there are many libraries and tutorials about it.


Hi Klucas007 -

From someone equally, if not even more, green especially on the coding side. I agree with @nrobinson2000 that module will work just fine and you should be able to Google some code examples quite easily. Just something to keep in mind, coding accelerometers might seem a bit daunting at first.

I wanted to use something similar to activate a battery indicator in a device I built a while back. As I needed a simple on/off function, I decided to go with a tilt switch instead. Much easier to handle in the code :slight_smile:

I suppose depending on exactly what you need, I would maybe rather go with the simple vibration/tilt switch. Each trigger should have a time stamp, so it should be easy to determine the “running time” by simply subtracting the two time stamps? I do agree that an accelerometer will most certainly give you more options though, I doubt you will be able to bring the Photon out of sleep mode using a tilt switch.

With regards to a sensor that works well with Particle, I am sure most of them will do just that. A simple search in Amazon or the all-knowing Google should give you plenty of options. If it is indeed for liquid flowing through a pipe you want to measure, I will opt to go with a flow sensor to eliminate false readings caused by outside vibration or bumps. A flow meter will also give you the option to determine flow rates and volumes.

Best of luck!


@nrobinson2000 has given you a good steer. Have you experience of using an accelerometer to measure liquid movement in a pipe? What sort of frequency of vibration would you need to detect?

I am surprised if this would be a reliable method - usually non-invasive / clamp-on sensors use ultrasound. Doppler and Transit Time are two very popular types of flow meter for non-invasive measurement of flow in full pipes. In flow sensors usually have a turbine which generates a signal proportional to the flow. Depending upon the pipe material these can read from outside of the pipe, so that if simple flow (yes or no) is required a flap type sensor can be used.

Maybe a microphone would be possible? You then have to think about amplification of the signal and filtering to get a reliable output.


@friedl_1977 I like the idea of beginning with a simple vibration sensor. I am sure its a good place to start my journey.
This is the sensor I am looking at using. Any input on which ports to wire it, resistors and code to get started would be much appreciated.

Thanks man!


That “sensor” is a switch, meaning you can use it as a digital input. Whenever there is vibration the switch will be briefly closed. (Realistically the switch will probably open and close several times due to the metal spring inside.)

The easiest way the use the switch would be to connect it between GND and any GPIO pin on the photon. In your code you would set the pin to use an internal pull-up resistor.

You could read the pin as a digital input. In the examples below I will use pin A0:

// Set A0 as an input with the internal pull-up resistor
pinMode(A0, INPUT_PULLUP);

// Since A0 is connected to ground, when the switch is closed it will be read as LOW
// and when the switch is open it will be read as HIGH due to the pull-up
bool vibration = digitalRead(A0);

What you probably want to do is setup an interrupt for the pin so you can have an ISR (Interrupt Service Routine) get called whenever the switch is closed. (ISR’s run in the background and the handler function must execute quickly.)

attachInterrupt(A0, vibrationHandler, FALLING);

I would suggest creating a vibrationHandler function something like this:

void vibrationHandler()
// Ideally you should make this function as concise as possible.
// A good use of this function would be to increment or
// set global variables that get used in loop() to know that vibration has been detected.

// Example: assuming this is a bool
vibrationDetected = true;

// Example: assuming this is an int
vibrations += 1;

Any variables modified in an ISR should be declared using the volatile keyword.

volatile bool vibrationDetected;
volatile int vibrations;

You can implement this switch however you like, but I think using a pull-up and an ISR makes the the most sense.



No problem, there are some guys in this forum that showed plenty of patience with me, so I will gladly help out wheref I can :smile:

@nrobinson2000 gave you a very good example of how to use this sensor. I am not sure how familiar you are with coding Particle devices (or Arduino or something similar for that matter) so I will give you a very simple, but note - less comprehensive - way in which I probably would have done it :rofl:

/* Simple Vibration Sensor 
    Light LED when vibration is detected
const int LED = D0;  //LED connected to pin analog pin D0 of Photon
int sensor;           //Variable to store analog value (0-1023)

void setup()
	Serial.begin(115200);      //Printing to serial monitor
	pinMode(led_pin, OUTPUT);

void loop()
	sensor = analogRead(A5); //Sensor connected to A5
	//If Sensor is not detecting any value, the  analog pin receive a value of 1023~1024 but confirm on the datasheet
	if (sensor<1022){
		digitalWrite(led_pin, HIGH);
                delay (1000);
		Serial.print("Sensor Value: ");
		Serial.print("Sensor Value: ");
	delay(1000); // delay 1 second between reads.

Please feel free to use the code from @nrobinson2000 instead, I am sure it is much more comprehensive, but this should also do the trick. There might be even more simplified way, but then I suggest using a sensor module instead that will convert the analogue value to digital 0/1. You can then use something like below:

int vib_pin=D0;
int led_pin=D1;
void setup() {

void loop() {
  int val;

Please note I have not tested the code so my apologies for any mistakes… but it should work :slight_smile:

Hope this helps!!



I don’t think that using this digital switch as an analog input would provide much of an advantage. I also don’t think that using a module like the one you linked is required, but it could definitely reduce some of the noise from the switch. I’ll include a more complete example that extends the suggestions I had made earlier.

(Also, the ADC on Particle is 12 bit, so the range is 0-4095, not 0-1023.)

(LED on pin D0, Vibration switch between A0 and GND.)

#include "Particle.h"

// Pin definitions
#define LED_PIN D0
#define VIB_PIN A0

// Variables modified by ISR and used by loop()
volatile bool vibrationDetected;
volatile int vibrations = 0;

// ISR function
void vibrationHandler()
    vibrationDetected = true;
    vibrations += 1;

// Initialize pins and setup ISR function
void setup()
    pinMode(LED_PIN, OUTPUT);

    attachInterrupt(VIB_PIN, vibrationHandler, FALLING);


// Turn on the LED and print when vibration is detected
// Print the number of vibrations detected since the last loop()
void loop()
    if (vibrationDetected)
        vibrationDetected = false;
        digitalWrite(LED_PIN, HIGH);
        Serial.println("Vibration detected!");
        digitalWrite(LED_PIN, LOW);

    if (vibrations > 0)
        Serial.printlnf("The switch has vibrated %d times.", vibrations);
        vibrations = 0;

@Klucas007 I would suggest trying this out once you get the switch. If you want to try it now, you could temporarily replace the switch with a button wired between A0 and GND, since rapidly pressing the button would have the same effect as the spring vibrating.


@Klucas007 -

Did you manage to solve this? I have been giving some thought to this, but is is more challenging as I do not have the entire scope of what you are trying to achieve other than to know whether water is flowing through a pipe or not?

Coding is not my strong suit, but I have worked on couple of ad-hoc product developments before including a digital water meter with some very specific and custom requirements. I have learned a valuable lesson on that project which was to always consider the consequence of device failure as that will greatly affect the pricing point at which you will enter the market.

@nrobinson2000 - My only reason for suggesting the digital pin connection was that is seems simple enough, even for a complete novice such as myself :slight_smile: Refer the the image below. (apologies for the Arduino reference, could not find a Particle one) I find it is an easier start to work with on/off as opposed to analog values, but then again, maybe that is just me :rofl:

I do not have the sensor on hand so am not able to replicate your project, but this would probably be the place I would start, blow up a couple of things and then explore further :blush: Having said that (and as I mentioned about the consequence of device failure), if you want/need to go with a comprehensive solution as explained by @nrobinson2000 above and avoid faulty reads or miss reads, I am not sure a vibration sensor is the way to go in your case. If the accuracy of the data provided by your device is ‘mission critical’ and no false reads are allowed, I would maybe suggest the use of a flow meter instead as it will less prone to publishing reads caused by external factors.

Please feel free to let me know of I am way off base here, I value critique as it is a good way to learn :slight_smile:

I hope. this helps and beest of luck!


Hey guys! I just got my vibration sensors in today, and I am going to take some time over the next couple days to play around with the code given. Thank you so much for the help thus far! I will do my best to keep you updated on how things go and if I have any questions.


My last piece of advice, unless you know what you are doing of course :joy:

Start simple, get it working. Once you have the basics working, improve your code to include the code @nrobinson2000 gave you. Fairly quickly you will have your sensor working with proper code backing it up. If you can get the final code working from the start, good for you. I always start with the easier code just to get it working and then improve and add as I get more comfortable with the project. Keep in mind, my advice is from a novice, for a novice :slight_smile:

You will find some really clever guys in this forum and they are always eager to help.

Good luck and above all, have fun!! :slight_smile: