Detecting High Vibration Events with Arduino: Turn on LED Using Millis Function

Detecting High Vibration Events with Arduino: Turn on LED Using Millis Function

Introduction

When working with sensors and actuators using Arduino, one common task is to trigger an action based on sensor readings. In this article, we will discuss how to turn on an LED in response to a high vibration event. Specifically, we want the LED to turn on if the vibration sensor detects a high vibration event two times within one minute. We will use the millis() function to implement this logic while addressing a potential issue related to time wrapping.

Hardware Setup

To begin, you will need the following components:

An Arduino board (Uno, Nano, etc.) A vibration sensor A LED A resistor (220 Ohms or similar) Breadboard and jumper wires

Connect the vibration sensor and LED to the Arduino as follows:

Vibration sensor signal pin to a digital I/O pin on the Arduino (e.g., pin 2) LED anode (longer leg) to the same digital I/O pin through a resistor LED cathode (shorter leg) to ground Supply 5V to the Arduino VCC pin

Code Implementation

The following steps outline the code implementation:

Initialize the previous vibration time with unsigned long to ffffffff. Define a variable previousVibe to store the last vibration time. Whenever a vibration event is detected, check the time difference with millis(). If the time difference is less than 60 seconds, set the state for the second vibration and trigger the LED. Store the new vibration time in previousVibe.

Below is the full code implementation:

const int vibrationPin  2; // Vibration sensor connected to this pinconst int ledPin  3; // LED connected to this pinunsigned long previousVibe  FFFFFFFF;void setup() {  pinMode(vibrationPin, INPUT); // Set vibration sensor as input  pinMode(ledPin, OUTPUT); // Set LED as output}void loop() {  if (digitalRead(vibrationPin)  HIGH) { // Check if vibration sensor is triggered    unsigned long currentTime  millis();    // Check if the vibration occurred within the last 60 seconds    if (previousVibe ! FFFFFFFF  (currentTime - previousVibe)  60000) {      // Second vibration within 60 seconds, turn on LED      digitalWrite(ledPin, HIGH);    } else {      // Reset previousVibe to mark first vibration      previousVibe  currentTime;    }  }}

This code snippet initializes the variables and checks for vibrations. If the vibration sensor is triggered and the time difference is less than 60 seconds, the LED will turn on. The time difference is checked against the stored previousVibe, which is reset after the initial event to handle the second occurrence.

Potential Time Wrapping Issue

millis() is a function that returns the number of milliseconds elapsed since the processor started running the Arduino program. However, it can wrap back to zero after about 50 days of continuous operation. If this might be an issue, you can implement a workaround using a more complex comparison logic. For instance, you could use a combination of millis() and variables to store the last known time and handle the time overflow manually. However, this is an advanced topic and is not covered in this article unless requested.

Conclusion

In this article, we have explored how to control an LED in response to high vibration events using the Arduino board. By utilizing the millis() function, we were able to achieve the goal of turning on the LED when the vibration sensor detects two high events within one minute. This simple yet effective solution demonstrates the power and flexibility of Arduino for sensor-based projects.