fishing bite alarm

Hi I’ve just started a new project for a bite alarm for feeder fishing. I plan to make it with a sw-420 vibration sensor and a d1 mini. It will have a pot on the front to adjust the sensitivity and would like some help to implement this I have setup a 10k pot on the A0 to read the output of it and plan to use if statements. to achieve this but don’t know how to create the functions to call from the sensor.
e.g if (readval>=3 && readval<50){ sensitivityhi().
how would i create the sensitivityhi function or is there a better way to do it?.
Thanks.

Looking at https://docs.rs-online.com/33d0/A700000006575659.pdf it appears that at least some sw-420 modules have a sensitivity control built in

Hi yes they do but it isn't very good it seems its rather very sensitive or not at all if i can write it in to the code i can have more control over the sensitivity of it.

Setting the sensitivity in software is not going to be easy because the sensor appears to have a digital output

The module has a digital output. The actual vibration sensor is just a switch. The module LM393 circuit "stretches" the pulses by fully discharging a capacitor whenever the sensor switch closes, and comparing the voltage to a threshold set by the on board potentiometer. So actually, the potentiometer can't control the basic sensor sensitivity, just the number of "hits" of the sensor that will result in an output pulse.

Consequently, you can't do what you ask. The actual sensitivity to vibration is fixed in the design and build of the sensor device (the blue barrel). Once you set the potentiometer to maximum sensitivity, you've done everything you can to increase it.

A better way, would be to discard the circuit, and connect the bare sensor directly to a digital input on the Arduino, so that any vibration at all can be seen by software. Then you can have fun with various algorithms to shape the signal and change the sensitivity in software. Basically you would be writing software to perform the same function that the LM393 circuit does.

The barrel sensors are available online. Do understand that the device operates with a simple internal spring and weight. If the vibration is not sufficient to bring the spring contacts together internally, there can never be any indication from the device. That can not be changed with any external information processing.

Thanks for the detailed answer that helps a lot. is there another sensor i can use to achieve what i want perhaps a mpu 6050 would e a better choice. I initially chose the sw-420 because it seemed easy to work with as I am fairly new to it all.

Had a little play with a bare vibration sensor and managed to get serial output but my original question still stands how can i set it up so i can adjust the sensitivity with a pot? thanks.

have you tried with a simple piezo on an analog input? they tend to pickup vibration pretty well.

put a piezo on your table, attach negative pole to GND and positive pole to A0

run this code

int valMin = 1025, valMax = 0;
const int sensitivity = 20;

void setup() {
  Serial.begin(115200);
  Serial.println("Calibration. Don't touch anything, don't move, don't breathe ;)");
  delay(2000);
  for (int i = 0; i < 500; i++) {
    int v = analogRead(A0);
    if (v > valMax) valMax = v;
    if (v < valMin) valMin = v;
    delay(4);
  }
  Serial.println("Calibration done");
  Serial.print("Min = "); Serial.println(valMin);
  Serial.print("Max = "); Serial.println(valMax);
}

void loop() {
  static bool atRest = true;
  int v = analogRead(A0);
  if (atRest && ((v > valMax + sensitivity) || (v < valMin - sensitivity))) {
    atRest = false;
    Serial.println("ACTION");
  } else if (!atRest) {
    atRest = true;
  }
  delay(10);
}

there is a 2s “calibration” function at the start to see what value is read when idle and there is a sensitivity constant that you can use to fine tune detection

lay your piezo flat on your table, run the script and don’t touch anything for a few seconds, then tap on the table and you should see in the Serial monitor @115200 bauds ‘action’ being written.

typed here - so beware of typos. totally untested

tooter:
Had a little play with a bare vibration sensor and managed to get serial output but my original question still stands how can i set it up so i can adjust the sensitivity with a pot? thanks.

By smoothing the input with a digital filter and then comparing it with a threshold value (controlled by the potentiometer setting). As I said earlier, you are simply emulating the hardware circuit on the PCB, in software.

Thank you J-M-L The code works great. Forgive me if I’m being daft but as I said I’m fairly new to this and trying to learn.
Is there a way to link two analog readings together. (eg link a potentiometer reading to the sensitivity reading and scale it so as the pot reading increases so dose the sensitivity of the sensor). or would i have to use if statements to say if pot val is <50 >100 set a range for sensitivity. thanks in advance.

It's easy just change

const int sensitivity = 20;

to

int sensitivity = 20;

and insert code to read the potentiometer, scale the value and assign it to 'sensitivity'.

something like this should do

const byte piezoPin = A0; // piezo + pin on A0 (other on GND)
const byte potPin = A1; // sensitivity potentiometer on pin A1

int valMin = 1025, valMax = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Calibration. Don't touch anything, don't move, don't breathe ;)");
  delay(2000);
  for (int i = 0; i < 500; i++) {
    int v = analogRead(piezoPin);
    if (v > valMax) valMax = v;
    if (v < valMin) valMin = v;
    delay(4);
  }
  Serial.println("Calibration done");
  Serial.print("Min = "); Serial.println(valMin);
  Serial.print("Max = "); Serial.println(valMax);
}

void loop() {
  static bool atRest = true;
  int v = analogRead(piezoPin);
  int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200

  if (atRest && ((v > valMax + sensitivity) || (v < valMin - sensitivity))) {
    atRest = false;
    Serial.println("ACTION");
  } else if (!atRest) {
    atRest = true;
  }
  delay(10);
}

the line   int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200will read the value of your potentiometer (which will vary between 0 and 1023) and map it to the 20…200 range. That value will be used for the sensitivity in the formula

H

J-M-L:
something like this should do

const byte piezoPin = A0; // piezo + pin on A0 (other on GND)

const byte potPin = A1; // sensitivity potentiometer on pin A1

int valMin = 1025, valMax = 0;

void setup() {
 Serial.begin(115200);
 Serial.println(“Calibration. Don’t touch anything, don’t move, don’t breathe ;)”);
 delay(2000);
 for (int i = 0; i < 500; i++) {
   int v = analogRead(piezoPin);
   if (v > valMax) valMax = v;
   if (v < valMin) valMin = v;
   delay(4);
 }
 Serial.println(“Calibration done”);
 Serial.print("Min = "); Serial.println(valMin);
 Serial.print("Max = "); Serial.println(valMax);
}

void loop() {
 static bool atRest = true;
 int v = analogRead(piezoPin);
 int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200

if (atRest && ((v > valMax + sensitivity) || (v < valMin - sensitivity))) {
   atRest = false;
   Serial.println(“ACTION”);
 } else if (!atRest) {
   atRest = true;
 }
 delay(10);
}




the line 


int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200



will read the value of your potentiometer (which will vary between 0 and 1023) and map it to the 20...200 range. That value will be used for the sensitivity in the formula

J-M-L:
something like this should do

const byte piezoPin = A0; // piezo + pin on A0 (other on GND)

const byte potPin = A1; // sensitivity potentiometer on pin A1

int valMin = 1025, valMax = 0;

void setup() {
 Serial.begin(115200);
 Serial.println(“Calibration. Don’t touch anything, don’t move, don’t breathe ;)”);
 delay(2000);
 for (int i = 0; i < 500; i++) {
   int v = analogRead(piezoPin);
   if (v > valMax) valMax = v;
   if (v < valMin) valMin = v;
   delay(4);
 }
 Serial.println(“Calibration done”);
 Serial.print("Min = "); Serial.println(valMin);
 Serial.print("Max = "); Serial.println(valMax);
}

void loop() {
 static bool atRest = true;
 int v = analogRead(piezoPin);
 int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200

if (atRest && ((v > valMax + sensitivity) || (v < valMin - sensitivity))) {
   atRest = false;
   Serial.println(“ACTION”);
 } else if (!atRest) {
   atRest = true;
 }
 delay(10);
}




the line 


int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200



will read the value of your potentiometer (which will vary between 0 and 1023) and map it to the 20...200 range. That value will be used for the sensitivity in the formula

Hi thanks again for the kind help. unfortunately when I compile I get a error too many arguments to function ‘int analogRead(uint8_t)’
and it highlights this line int sensitivity = map(analogRead(potPin, 0, 1023, 20, 200)); // sensitivity will vary between 20 and 200

I tried to i rectified this by removing the analogRead from the line but the sensitivity don’t seem to change. Am I missing something?. If i wanted to adjust the sensitivity what values would i change thanks.

It's a typo. There should be a closing bracket after potPin. You will need to remove one from the end of the line too.

Indeed, it Should be int sensitivity = map(analogRead(potPin), 0, 1023, 20, 200);

Sorry typing that from my iPhone

J-M-L:
Indeed, it Should be

 int sensitivity = map(analogRead(potPin), 0, 1023, 20, 200);

Sorry typing that from my iPhone

No problem I appreciate the help. will test asap. thanks

Hi first of all I would like to thank everyone for there input so far. my problem is for some reason when the alarm is triggered and sensitivity is turned down it don’t affect hoe sensitive it is. i.e the same force tap affects it weather it is turned up or down. I tried changing the sensitivity range but it has on effect.

const byte piezoPin = A0; // piezo + pin on A0 (other on GND)
const byte potPin = A1; // sensitivity potentiometer on pin A1

int valMin = 1025, valMax = 0;
const int buzzPin = 9;

void setup() {
  Serial.begin(9600);
  Serial.println("Calibration. Don't touch anything, don't move, don't breathe ;)");
  delay(2000);
  for (int i = 0; i < 500; i++) {
    int v = analogRead(piezoPin);
    if (v > valMax) valMax = v;
    if (v < valMin) valMin = v;
    delay(4);
    pinMode(buzzPin, OUTPUT);
  }
  Serial.println("Calibration done");
  Serial.print("Min = "); Serial.println(valMin);
  Serial.print("Max = "); Serial.println(valMax);
}

void loop() {
  static bool atRest = true;
  int v = analogRead(piezoPin);
  int sensitivity = map(analogRead(potPin), 0, 1023, 20, 600); // sensitivity will vary between 20 and 200

  if (atRest && ((v > valMax + sensitivity) || (v < valMin - sensitivity))) {
    atRest = false;
    Serial.println(sensitivity);
    tone (buzzPin, 800);
    delay(150);
   noTone (buzzPin);
  } else if (!atRest) {
    atRest = true;
  }
  delay(10);
}

may be (20, 600) is too big of an interval. may be make it 0 to 20 in

  int sensitivity = map(analogRead(potPin), 0, 1023, 20, 600); // sensitivity will vary between 20 and 600

==>

  int sensitivity = map(analogRead(potPin), 0, 1023, 0, 20); // sensitivity will vary between 0 and 20

Hi no that don't seem to make a difference either.

print the raw values you get and see how they vary based on the various inputs you get