SOLVED: Improve earthquake alarm

Hi, I'm trying to make work a quake alarm system with Arduino. In general terms it's working but it need some improvements.

It's working with a "Quake Alarm" device, in short words this system detects a "P" (primary) waves who happens before the perceptible waves ("S" secondary).

Inside of the device, has a pendulum, when this start to move, it touch another part of the device to allow the flow of the current (and the buzzer sounds).

I've an Arduino mega, looking in some websites, I found a way to connect the alarm with the arduino without open it. Normally the alarm is connected by a 9V battery, when it detects a p wave, it activate a buzzer. The connection with the arduino is removing the battery, I have to connect one analog pin in the positive (+) polarity of the alarm, and also a 5V to the positive and the GND.

The analog pin detects when the current low, so is possible to do an action with the code.

The problem is, at this moment, the reading of the pin is not sensitive enough, the buzzer sounds, but the arduino isn't detecting any current change so don't do anything.

the code I'm currently using is this:

#define QA A0

static int difference;

int qaVal           = 0;
int qaPreVal        = 0;
int threshold       = 10;

void setup () {   
  Serial.begin(57600);
  pinMode(QA, INPUT); 
} 

void loop () {

  qaVal       = analogRead(QA);
  difference  = qaPreVal - qaVal; 

  if ((difference > 0) and (difference >= threshold)) {
     //do an action
     Serial.println("EarthQuake!");
  } 
  qaPreVal = qaVal; 
}

Is any way to improve this?

What exactly is your arduino analog pin connected to? A microphone? a lead on the buzzer?

is connected where the 9V battery was, now the Arduino supply 5V to make it work.

Quake Alarm Arduino
+VCC 5V
+VCC A0
GND GND

gepd:
is connected where the 9V battery was, now the Arduino supply 5V to make it work.

Quake Alarm Arduino
+VCC 5V
+VCC A0
GND GND

WHAT? How on earth is connecting an input pin to 5v supposed to detect ANYTHING?

because as I said, it detects when the current goes low.
are you understanding how the device works?

gepd:
because as I said, it detects when the current goes low.
are you understanding how the device works?

But the when the voltage being supplied by the arduino is pulled low, it will also affect the analogRead function.

analogRead returns a figure that will be 0 to 1023. The 1023 being equal to the 5v line as a reference. If you pull down the 5v line you are also pulling down the reference, so it will always be 100% of the 5v line (ie 1023)

It's 100% most of the time, but when a earthquake begin the current goes low for a milliseconds, so there is when arduino detect the change. Well, is how it's working until now, and that is why I'm asking for help, I think there may be a better way to connect it.

gepd:
It's 100% most of the time, but when a earthquake begin the current goes low for a milliseconds, so there is when arduino detect the change. Well, is how it's working until now, and that is why I'm asking for help, I think there may be a better way to connect it.

If you have managed to get any results like this in the past it's a fluke.

Do you have some schematic of what the sensor consists of?

There's an attachment with the schema

OK If the ONLY method you have of connecting to that device is through the battery clip, here's what I'd suggest.

I assume that the buzzer is one of the usual 9v jobbies that draw about 30ma. Now if you were to put a resistor in the negative line of that battery, (a nice low one of about 1 ohm) it wouldn't stop the buzzer circuit working, but would provide a voltage drop that is readable with analogRead.

Something like this (excuse the pbrush rendition)

I uploaded the wrong image with the schema, can you check it again and tell if will be the same modification? plus there is an image with the kind of buzzer it has.

I think it's a very common buzzer.

If with "the only method" you mean if I can't open the device, I opened one, so won't be problem if there is another solution. Meanwhile, I'll test what you posted

I estimate you'll only get an increase of about 3 on your analog reading using a resistance of 1 ohm. If you were to increase it to 5 ohm, you'd probably get somewhere around 15 which is quite detectable. I doubt it would stop the sensor working either.

Agh that new image looks like a piezo buzzer. They draw an incredibly small amount of current. I'll have a rethink.

OK This should work. You should even be able to make it a digital input and set the pinMode(inputPinNumber,INPUT_PULLUP) in your setup function

It will be active LOW so when the alarm goes off, you should see a (digitalRead(inputPinNumber)==LOW)

It's just one simple diode.

The diode can be a 1N4148 or 1N4001? I have both of them here.

can be possible to use an interrupt to activate a function with this sensor?, thinking if I have the mega board testing some other things, the "priority" will be the this sensor, do you think it can be?

gepd:
The diode can be a 1N4148 or 1N4001? I have both of them here.

Either should work

can be possible to use an interrupt to activate a function with this sensor?, thinking if I have the mega board testing some other things, the "priority" will be the this sensor, do you think it can be?

If you put it on pin2 (ignore "analog" in the description, you want digital pin 2) you should be able to use interrupt 0 to capture it.
for example

 void setup(){
pinMode(2,INPUT_PULLUP);
attachInterrupt(0, runForTheHills, FALLING);

 }
 
void runForTheHills()
 {//do the necessary here
   
 }

BUT I'm getting ahead of myself. Which arduino are you using?

I'm testing it with arduino MEGA

Should be fine.

I have good and bad news; the good news is now, it's very sensitive and at the minimum movement, it call the interrupt.

the bad news is after a seconds it freezes. I print a text every 1 second in the normal loop, simulating it's another sensor doing something, and also I print an alert text in the interrupt, the serial monitor shows this:

happened 1 second.

happened 1 second.

happened 1 second.

happened 1 second.

Activated!

Activated!

Activated!

Ac

as you can see the last "activated" text wasn't wrote completely.
after that I have to reset the arduino and happen the same

gepd:
I have good and bad news; the good news is now, it's very sensitive and at the minimum movement, it call the interrupt.

the bad news is after a seconds it freezes. I print a text every 1 second in the normal loop, simulating it's another sensor doing something, and also I print an alert text in the interrupt, the serial monitor shows this:

I hope you're not Serial.printing from within the interrupt routine. (you can't do that:). You can set a global variable or use digital.write etc. But you can't use the serial port. (it relies on interrupts that are disabled during an Interrupt service routine).