Arduino Tutorial 08 - Detecting Vibration

In this project, we are going to use the tilt switch sensor included in your kit. The tilt switch sensor can detect basic motion and orientation. It contains two contacts and a small metal ball. Once held at a particular orientation, the ball bridges the two contacts and completes the circuit. We have also added an LED to this project. When the sensor detects movement, the LED is HIGH (ON). When no movement is detected the LED is LOW (OFF).

 

COMPONENT LIST

 

HARDWARE

A tilt switch sensor behaves much like a push button. You need to add a pull-down resistor to the sensor to ensure the circuit is disconnected when no signal is detected.
You also need to add a current-limiting resistor to the LED.

fig 1 Detecting Vibration Circuit

 

Code

 

Sample code:

CODE
// Project - Detecting Vibration  
int SensorLED = 13;       // Define the LED pin as digital pin 13  
int SensorINPUT = 3;      
/* Connect the vibration switch to interrupt 1, which is digital pin 3*/ 
unsigned char state = 0;  
  
void setup() {   
  pinMode(SensorLED, OUTPUT);         
// Set the LED pin as OUTPUT mode  
  pinMode(SensorINPUT, INPUT);        
// Set the vibration switch pin as INPUT mode  
  
  /*Trigger interrupt 1 (pin 3) on the RISING edge, calling the blink function */ 
  attachInterrupt(1, blink, RISING);     
 }  
  
void loop(){  
      if(state != 0){              // If the state is not 0  
        state = 0;               // Set state to 0  
        digitalWrite(SensorLED, HIGH);   // Turn on the LED  
        delay(500);          // Delay for 500 milliseconds  
      }    
      else   
        digitalWrite(SensorLED, LOW);
// Otherwise, turn off the LED  
}   
  
void blink(){                // Interrupt function blink()  
state++;             
// Increment the state value every time the interrupt is triggered  
}

Excepted behaviour:
When we shake the board, the LED is HIGH (ON). When we stop shaking, the LED is LOW (OFF).

 

CODE REVIEW

 

In this section, we are going to examine the interrupt function that we used in the code. The program works as follows: when there is no interruption to the program, the code keeps running and the LED stays LOW (OFF). When there is an external event and the tilt sensor is activated, such as someone shaking the board, the program runs the blink() function and state starts incrementing. When the if statement detects that the state is no longer 0, it triggers the LED to be HIGH (ON). At the same time, it resets the state to 0 and waits for the next interruption. If there is no interruption, the LED is LOW (OFF).

what is interrupt?
Imagine you are watching TV at home and the phone rings. You have to stop watching TV and pick up the phone. After the call has ended, you continue to watch TV. In this case, the call is the interrupt and the ringing of the phone is the condition.

 

The attachInterrupt function specifies a named function, or an Interrupt Service Routine (ISR), to call upon when an interrupt occurs. This replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). Different boards have different interrupt pins.


Check the references on Arduino.cc for details: 

https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/

 

There are 3 parameters in "attachInterrupt":

 

interrupt :
The number of the interrupt (int), is either number 0 or 1. If it is 0, you must connect the jumper wire to digital pin 2. If it is 1, you must connect the jumper wire to digital pin 3.

 

function :
- The function is called upon when the interrupt occurs.
- The function has no parameters and returns nothing.
- As delay() and millis() both rely on interrupts, they will not work while the function is running. 
- The function cannot read values from the serial port. You might lose data connected from the serial port.

 

mode:
mode defines when the interrupt should be triggered. Four contstants are predefined as valid values: LOW to trigger the interrupt whenever the pin is low, CHANGE to trigger the interrupt whenever the pin changes value RISING to trigger when the pin goes from LOW to HIGH, FALLING for when the pin goes from HIGH to LOW.

 

Now let’s go back to the program:

 

attachInterrupt(1, blink, RISING);

 

- 1, because the tilt switch sensor is connected to digital pin 3.
- Blink is our interrupt function.
- RISING, to trigger when the pin goes from LOW to HIGH.

 

Why have we chosen RISING?
When the tilt sensor does not detect any signal, pin 3 is LOW. When it detects a signal, it connects with 5 volts and this changes the pin from LOW to HIGH.

HARDWARE REVIEW

 

Tilt Switch Sensor

The tilt switch sensor goes by many different names: ball switch, bead switch, vibration switch, etc.
Though it has different names, its working principles are the same. In simple terms, it is a switch made up of a cylinder with a small metal ball inside. When the metal ball rolls to either edge of the cylinder, they touch one of the contact pins and the circuit is complete. Examine the diagram for further detail.

 

 

fig 2 Tilt Switch Sensor

icon Detecting_Vibration_English.zip 1KB Download(0)
License
All Rights
Reserved
licensBg
0