Relay, when turned on, messes up analogread()!

I have put up a simple circuit to control the living room lights.

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: [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:
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” ( If someone could point me in the right direction I’d be greatly appreciated :slight_smile:

Schematic. Post it. If necessary, scribble it out on paper and post a photo of the scribbles.

If the relay is [u]not[/u] connected to pin 3, does everything work correctly?

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.

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 ]

First of all thank you for quick responses! :)

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.

What sort of mains lighting are you switching? If the interference occurs all the time the lights are on (not just while switching on or off), then using an SSR is unlikely to help.

The lights are normal fluorescent lights that (given I am in Portugal) are operated with an electric tension of about 230ACV.

As for the schematics I'll upload them into the first post right about now.

And thanks for your help once again! :)