Unstable tilt sensor SW-520D

i've been working with tilt sensor module SW-520D, in some tutorials, the sensor's position must be placed on a horizontal position, but when i upload my code, it gives me unstable data, so i try to set the module in vertical position but turns out it keeps reading 1 (no matter i change the position of the module or give it a shake), it should be 0 right when there's change of orientation?

image

so i try again, but the output now is always turning 0, i place the sensor as flat as possible, but it never read digital 1 anymore no matter the position. i adjust the sensitivity by rotating the potentiometer but no effect.

#include <Arduino.h>
#include <Wire.h>

const int pintilt = 12;
const int buzzer = 16;
// int tiltState = 0;

void setup(){
  Serial.begin(115200);
  pinMode(pintilt, INPUT);
  pinMode(buzzer, OUTPUT);
}

void loop(){
  int tiltState = digitalRead(pintilt);
    if (tiltState == LOW) { 
      Serial.print(tiltState);
      digitalWrite(buzzer, HIGH);
    } else{
      Serial.print(tiltState);
      digitalWrite(buzzer, LOW); 
    }
  delay(1000);
}

because it's very afforadable, could it be that the sensor is not precise? or maybe my sensor module is broken?

Posting a link to the datasheet will make helpers not familiar with this module more interested.

What about the status LED? It should be off when horizontal and on when vertical.

could it be that the sensor is not precise? or maybe my sensor module is broken?

yes, yes

The delay(1000) in your code causes you to miss 1000 opportunities to catch the pulse every second :wink:

As the pulse of the tilt sensor can be short (depends on its internals) you might need an interrupt routine to catch its pulse. Check attachInterrupt examples.

"maybe my sensor module is broken?"

• Set the sensitivity pot in the center
• PWR LED ON?
• Point sensor up
• DO LED should be on
• Point sensor down
• DO LED should be OFF

Yes. But first, just remove the delay. The device shouldn't make such fast pulses as to need interrupts - it is two small balls that make continuous contact at certain angles.

So lose the delay and slowly move the sensor pointing it in every direction to discover where it turns on.

As I read about the sensor itself, I can't explain why the module would need a sensitivity control; the sensor is a switch and could just as well be attached directly to an input pin in the manner of a regular pushbutton.

If you have the skill, you could try removing the sensor and using it in that way. I suppose it is possible that the sensor itself is bad, but if the module is bad it seems more likely to be some other failure.

a7

A ball switch has a crazy amount of switch bounce. A cap across the switch would probably be enough.

Maybe something time related for debounce or for vibration sensing?

Maybe.

I'm looking at things like

and if I used one, would probably just use traditional switch handling or, as you say, a small capacitor. Or both. :expressionless:

It's odd they refer to it as a vibration sensor. There is a similar package ih which find themselves a spring and a probe. It is open in all orientations after it settles; sufficient vibration causes it to present briefly as closed.

While even in this case I don't see a legitimate need for any other circuitry or module, it may be that interrupts have a place for grabbing very brief events.

a7