The former light switches (230V) are working with a 5V relay currently driven by its own power source (in a desperate try to work out the problem) a 9V battery, the schematics are similar to these:http://arduino.cc/playground/uploads/Main/relays.pdf [although I think I've missed the point of "GND" in the schematics as I've only connected the "RELAY power GND" just to arduino's ground]The pin driving the relay is pin 3 (irrelevant to the case).

I've also put an electret microphone with a 2n3904 transistor amplifier being read at analog pin 0.

The code is a modified version of this:http://grathio.com/2009/11/secret_knock_detecting_door_lock/which perfectly fits the needs.

The problem I'm facing is that whenever the relay is turned on, the microphone readings spike every now and then and (the whole mic thing works as a trigger) they are understood as claps.

Don't know what more to try, I've searched google for the past few days and the most valuable resource I could find was this post about "Understanding grounding" (http://jeelabs.org/2011/12/06/how-grounding-works/). If someone could point me in the right direction I'd be greatly appreciated

Assuming the mic isn't actually picking up the noise of the relay activating, what's probably happening is that switching the lights is causing mains transients which are getting picked up by the microphone amplifier. Some suggestions:

- use screened cable to connect the mic to the transistor amplifier, unless the wires are extremely short;- keep all the relay wiring well away from the microphone and amplifier wiring;- a zero-crossing SSR would probably generate smaller transients than a mechanical relay.

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Like dc42 says, and perhaps a metal box to enclose the Arduino/amplifier/battery (but keep the relay separate and at some distance). Placing all the analog circuitry in screened cables and enclosure protects it from the capacitive transients of mains switching.

Other tricks are 100pF cap across the incoming microphone input to shunt fast transients, and adding more decoupling to the transistor amplifier.

[ Oh, the obviouse software solution - when you switch the relay stop looking at the microphone signal for a few ms ]

I will post the schematics as soon as I can, and yes CodingBadly, the relay would work if it was connected to any other digital pin as I'm using digitalWrite to turn it on.

As to the suggestions dc42 gave, moving the relay further away it's not an option as I've got to the fit the whole circuitry in a relatively small space, but the SSR is a really good hipothesis!

Also MarkT, the capacitor comes as an obvious choice for debouncing and I'm definitely trying that when I can but I'm afraid that it would also spoil the great sensivity it has right now. And the box as I said will be difficult but we'll see.

As for a software solution it isn't that simple because I can cope with the initial oscillations, however the analog readings spike continously every time the lights are on (maybe I could ignore with software some low tension values that spike?).

Thank you so much guys, I'll post the schematics later so you can have look.