Vibration sensor to take photos


I've got a school project and I need to find a way to program receiving a high enough input from a vibration sensor to take a photo immediately, then a minute after that and one the following minute after that (and to upload it all to google drive and maybe).

I found this code from an Arduino project hub
( Arduino - Take Picture - Upload to Google Drive - Arduino Project Hub )

Would anyone know how to modify this code to do the above task, rather than take a photo when a button is clicked?


So let me get it right

You think that doing YOUR school homework means you find an existing project and ask someone to modify it for you to meet your need ?

What about actually learning something and trying to do it yourself ? (Yes there is some work involved).

Well the actual task is to design a method a project that can help aid in solving rural crimes (e.g. theft of livestock) with the bulk of the task being a report we have to write on the design and other things (this part is only a small bit).

Our teacher said we have to design something on the scale/complexity like this however has not reached us any audino code or really much to do with audinos. I asked if he could help us but he said he couldn't but instead we should check on the internet.

And what we are meant to learn about is design thinking and the design process (the class is STEM) of solving real-life issues using engineering. And also the main part is actually reflecting on the idea (not creating it)

This would probably be an ideal job for an ESP32CAM module (although it would need to have wifi access to upload to a google drive unless you attached a GSM module and did it via the cellphone network)
A 'sw-420' mercury switch module would be the easiest way to do vibration sensing, with these you can set a threshold above which it triggers.


here is the outline of a state machine that does what you want:

const byte sensorPin = 2;
const byte ledAlertPin = LED_BUILTIN;

unsigned long startTime;
const unsigned long timeBetweenPictures = 60000ul; // ∆t between pictures = 1 minute in ms


void takePictureAndUplaod(int pictureNumber)
  Serial.print(F("Taking and uploading picture #"));

// returns true if alert is triggered
bool alert() {
  return (digitalRead(sensorPin) == LOW); // simulate alert triggering with a button

void alertManagement() {
  switch (currentState) {
    case NOMINAL:
      if (alert()) { // alert triggered
        Serial.println(F("*** ALERT ***"));
        digitalWrite(ledAlertPin, HIGH); // turn the alert LED ON (or whatever is on that pin)
        startTime = millis();
        currentState = ALERT_PICTURE_TWO;

      if (millis() - startTime >= timeBetweenPictures) {
        startTime = millis();
        currentState = ALERT_PICTURE_THREE;

      if (millis() - startTime >= timeBetweenPictures) {
        currentState = WAIT_ALERT_END;

    case WAIT_ALERT_END:
      if (!alert()) {
        Serial.println(F("*** END OF ALERT ***"));
        digitalWrite(ledAlertPin, LOW); // turn the alert LED OFF  (or whatever is on that pin)
        currentState = NOMINAL;


void setup() {
  pinMode(ledAlertPin, OUTPUT);
  pinMode(sensorPin, INPUT_PULLUP); // will go low when alert triggered (simulated with a button)
  Serial.println(F("*** MONITORING ACTIVATED ***"));

void loop() {

The state machine is pretty simple to read. We start in state NOMINAL

  • we wait for an alert to be triggered, in which case we take picture #1 and go to next state
  • timeBetweenPictures ms later we take picture #2 and go to next state
  • timeBetweenPictures ms later we take picture #3 and go to next state
  • where we await until the alert signal to be turned off and we start over

if you run it Serial Monitor (@ 115200 bauds) will show (here I triggered twice)

*** ALERT ***
Taking and uploading picture #1
Taking and uploading picture #2
Taking and uploading picture #3
*** END OF ALERT ***
*** ALERT ***
Taking and uploading picture #1
Taking and uploading picture #2
Taking and uploading picture #3
*** END OF ALERT ***

you will have to provide code for

  • the setup() to configure correctly your system
  • write the alert() function so that it really reads your sensor
  • write the takePictureAndUplaod() function so that it really takes a picture and upload to GDrive

that should get you going

Ok, thankyou!

Ok, thankyou very much!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.