Target Impact With Vibration Sensor SW-420

Hi,

I know that there are some posts talking about this same topic but i already read them and cannot see a solution for my problem.

I have an Arduino Mega with a a Vibration Sensor SW-420 for Arduino and i want to have some LED lights lid up when an impact is registered.

So far so good, and my code was working flawless until something went wrong, the led makes what is suposed to do but sometimes it keeps on flashing and the light on the sensor for the D0 pin is out and stays out for some time until it returns to normal or if we registered another impact.

This is the code:

//Arduino Code - Vibration Sensor

const int ledPin = 11;      // led connected to digital pin 5
int Vibration_signal = 7; //Define the Digital Input on the Arduino for the sensor signal
int Sensor_State = 1; 
int blinkCounter = 0;

void setup() {
  pinMode(Vibration_signal, INPUT); //Set pin as input
  pinMode(11, OUTPUT); //Set pin as input
  Serial.begin(9600); // Start the serial communication
}

void loop() {
  Serial.print("Vibration status: ");
  Sensor_State = digitalRead(Vibration_signal);
  if (Sensor_State == 1) {
    //Acende o led
    while(blinkCounter <= 10)
            {
            Serial.println(blinkCounter);
            digitalWrite(ledPin, HIGH);
            delay(250);
            digitalWrite(ledPin, LOW);
            delay(250);
            blinkCounter++;
            }
        blinkCounter = 0; 
        delay(50); 
  }
  else {
    //Sensor_State = 0;
    //digitalWrite(11, LOW);
    Serial.println("No vibration");
  }
  delay(50);
}

So, is there something wrong on the code that could trigger this event?

Already swap for 5 new Sensors and they all do the same thing.

I'am connecting 9v 330ma on the arduino and i have all connected directly on the arduino

I can put on the shematics here for you to check connections.

Cheers

To give you more info, this is the output on the Serial:

Vibration status: No vibration
Vibration status: 0
1
2
3
4
5
6
7
8
9
10
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: No vibration
Vibration status: 0
1
2
3
4
5
6
7
8
9
10
Vibration status: 0
1
2
3
4
5
6
7
8
9
10
Vibration status: 0
1
2
3
4
5
6
7
8
9
10
Vibration status: 0
1
2
3
4
5
6
7
8
9
10
Vibration status: 0
1
2
3

This should be INPUT_PULLUP, and you should have sensor wired from the pin to GND. The pin will read LOW when vibration is detected.

The code spends 99.9999% of the time printing at a very slow (9600) Baud rate or delaying, and vanishingly little time actually reading the switch.

Do you think that's related to what it's happening?

What do you mean, having a wire from the pin to the ground?

Just made a test and found out that, this happens if we rub the side of the sensor, somewhat like a short circuit, maybe the problem is on the type of sensor used, or the voltage usage?

I'am a little confused with this....

The sensor is just a switch, buffered with an opamp as threshold detector.

Try to re-write your code as the StateChangeDetector example in the IDE.
To only register a hit when the pin has been LOW, and changes from LOW to HIGH.
Leo..

Do you have the naked SW-420 switch, or the module with an amplifier?

If you have the naked switch, wire it like any button or switch, from digital pin to ground, and set the port pin to INPUT_PULLUP.

For this version, digitalRead returns HIGH indicating vibration, and switch opening.

I don't know if I understood your need well.

But this case of yours seemed to me a case where the use of interrupt applies.

To use this feature, you need to change sensor pin from 7 to 2 or 3.
I used 2 and modified its pinMode.

As there is no vibration sensor in the simulator, I used a button to replace the vibration sensor.

See if these modifications meet your needs.
"Vibatrion - Wokwi ESP32, STM32, Arduino Simulator

This is the one i use

And here is the schematic of what i have

Already test it, works like mine but still makes that weird situation unfurtunatly.

Did you read post#6 (StateChangeDetection).
Did that solve your problem.
Leo..

Hi Guys,

Already changed the sensor to a Piezo and already make some changes to the code but now i cannot get the leds to light up.

Maybe i'am looking at the wrong tree, can you guys help.

//Arduino Code - Vibration Sensor

const int ledPin = 11;      // led connected to digital pin 5
int Vibration_signal = 10; //Define the Digital Input on the Arduino for the sensor signal
int Sensor_State = 0; 
int blinkCounter = 0;

void setup() {
  pinMode(Vibration_signal, INPUT_PULLUP); //Set pin as input
  pinMode(11, OUTPUT); //Set pin as input
  Serial.begin(9600); // Start the serial communication
  attachInterrupt(digitalPinToInterrupt(Vibration_signal), pick, FALLING);
}

void loop() {
  //Serial.print("Vibration status: ");
  Sensor_State = digitalRead(Vibration_signal);
  if (Sensor_State == 1) {
    //Acende o led
    while(blinkCounter <= 10)
            {
            Serial.println(blinkCounter);
            digitalWrite(ledPin, HIGH);
            delay(500);
            digitalWrite(ledPin, LOW);
            delay(500);
            blinkCounter++;
            }
        blinkCounter = 0; 
        delay(50); 
  }
  else {
    Sensor_State = 0;
    digitalWrite(11, LOW);
    Serial.println("No vibration");
  }
  //delay(50);
}
void pick() {
  Sensor_State = 1;
}

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