[Solved-ish] Microphone output 'breaks' when a relay is connected and powered

Hey folks,

To explain my project very quickly, I've made a line of lamps (halogen), which would visualize the amount of sound, more lamps going on if the sound is loud.

I'm using this microphone: MAX9812.

The setup works really well if it's just the arduino running. The relay/code reacts nicely to the amount of sound coming in. (More relay gates opening if the sound is loud, and closing when it's quiet again).
However, when I put power on the relay and lamps the sound doesn't reset, but instead uses the highest number it got as a base.
For example, if there was only background noise the signal would be around 300, blow into the mic or tap on it and it would go up to 500 and when I stop it goes back to 300.
When the power is on it doesn't go back to 300.

It doesn't reset if I reset the code, or reupload it. The only way I have found to reset this, is to remove all power from both the arduino and the relay, leave it for a while and then come back.

The analog output of the microphone goes directly into (tried with different resistors and and doesn't make a difference) the Arduino A0 port.

Can anyone help me with this? I don't think it's the code and instead the lamps messing with the signal?

Let me know if you need the code and/or setup, and sorry if my explanation isn't clear.

Thanks!

How I solved it, probably a stupid way but it worked.
I got another arduino uno which only houses the microphone, and uses serial communication to send the received value to the other arduino.

InsaneCold:
Can anyone help me with this? I don't think it's the code and instead the lamps messing with the signal?

We can't see the code or the wiring setup so there's no hope of any of us being able to tell what the issue is. How do you think we could figure out what's wrong with something when we have ZERO information about it other than the fact that it seems to not work?

Delta_G:
We can't see the code or the wiring setup so there's no hope of any of us being able to tell what the issue is. How do you think we could figure out what's wrong with something when we have ZERO information about it other than the fact that it seems to not work?

I thought it was some trivial mistake, like, hey a relay interferes with other components.
But I've added the code and an attempt at the wiring.
Please keep in mind that I have very little experience with arduino.

The wires at the top go to the relay, but the sketch thing didn't have one.

// Values
int total = 0;
int amount = 0;
int average = 0;

int analogValue = 0;

// Pins
int lamp1 = 4;
int lamp2 = 5;
int lamp3 = 6;
int lamp4 = 7;
int lamp5 = 8;
int lamp6 = 9;
int lamp7 = 10;

//Tresholds
int treshold1 = 350;
int treshold2 = 375;
int treshold3 = 400;
int treshold4 = 425;
int treshold5 = 450;
int treshold6 = 475;
int treshold7 = 500;

void setup() {
  // Zet lampjes als Output
  pinMode(lamp1, OUTPUT);
  pinMode(lamp2, OUTPUT);
  pinMode(lamp3, OUTPUT);
  pinMode(lamp4, OUTPUT);
  pinMode(lamp5, OUTPUT);
  pinMode(lamp6, OUTPUT);
  pinMode(lamp7, OUTPUT);
  digitalWrite(lamp1, HIGH);
  // En serial aan voor debugging
  Serial.begin(9600);
}

void loop() {
  // Lees de value die de sensor terug geeft.
  analogValue = analogRead(0);

  if (analogValue > 350) {
    amount++;
    total += analogValue;
  }
  if (amount > 20){
    average = total / amount;
    Lamps(average);
    Serial.println(average);
    ClearValues();

  }
  //Serial.println(amount);
  delay(1);
}
void ClearValues(){
    average = 0;
    amount = 0;
    total = 0;
}
 
void Lamps(int loudness) {
  // Lamp 1
  if (loudness > treshold1) {
    digitalWrite(lamp1, LOW);
  }
  else if (loudness < treshold1) {
    digitalWrite(lamp1, HIGH);
  }

  // Lamp 2
  if (loudness > treshold2) {
    digitalWrite(lamp2, LOW);
  }
  else if (loudness < treshold2) {
    digitalWrite(lamp2, HIGH);
  }

  // Lamp 3
  if (loudness > treshold3) {
    digitalWrite(lamp3, LOW);
  }
  else if (loudness < treshold3) {
    digitalWrite(lamp3, HIGH);
  }

  // Lamp 4
  if (loudness > treshold4) {
    digitalWrite(lamp4, LOW);
  }
  else if (loudness < treshold4) {
    digitalWrite(lamp4, HIGH);
  }

  // Lamp 5
  if (loudness > treshold5) {
    digitalWrite(lamp5, LOW);
  }
  else if (loudness < treshold5) {
    digitalWrite(lamp5, HIGH);
}

  // Lamp 6
  if (loudness > treshold6) {
    digitalWrite(lamp6, LOW);
  }
  else if (loudness < treshold6) {
    digitalWrite(lamp6, HIGH);
  }

  // Lamp 7
  if (loudness > treshold7) {
    digitalWrite(lamp7, LOW);
  }
  else if (loudness < treshold7) {
    digitalWrite(lamp7, HIGH);
  }

}

InsaneCold:
I thought it was some trivial mistake, like, hey a relay interferes with other components.

That would depend on how things were wired.

InsaneCold:
But I've added the code and an attempt at the wiring.
Please keep in mind that I have very little experience with arduino.

The amount of experience you have with Arduino has no bearing on whether or not you can figure out that a person can't debug what they can't see. That's just basic common sense.

InsaneCold:
The wires at the top go to the relay, but the sketch thing didn't have one.
https://i.gyazo.com/fbf5748228c7a7ec9085860ac278b963.png

Forget fritzing. Just get a pencil and a piece of paper and draw us a schematic. Post it here so we don't have to go offsite to find it.

Perhaps you haven't got free-wheel diodes on the relay windings? But guessing wildly isn't as
good as looking at your circuit and code.

It seems the relay and/or halogen lamp currents interfere with the microphone.
We DO need the FULL picture to give you the proper advice.
How is the relay module powered, how is the Arduino powered, how are the lamps powered, etc.

You could try powering the (sensitive) mic board from 3.3volt.
That should be "cleaner" than the 5volt rail.
Leo..

I've added the wiring scheme online at this link:

The arduino is powering both the microphone and the relay, could this be it?

Still weird it only breaks if the halogen lights receive power.

Are you driving those relays directly off the Arduino pins? You probably need a transistor on each one so you aren't trying to pull so much power through the Arduino.

Yeah, the relay goes straight to the pin, no transistor between.

InsaneCold:
Yeah, the relay goes straight to the pin, no transistor between.

You may be hurting the Arduino that way. Is this a replay shield of some sort or are those just normal relays? If the latter then you should probably be using a transistor to drive them.

Delta_G:
You may be hurting the Arduino that way. Is this a replay shield of some sort or are those just normal relays? If the latter then you should probably be using a transistor to drive them.

Just a normal 8-way relay, thanks for the advice, I'll look into that.

But that can't be the problem right? I mean, the relay works fine if there is no power coming from the wall, it's just when I plug it in, it breaks. Does the relay send power back into the Arduino somehow?

InsaneCold:
Does the relay send power back into the Arduino somehow?

No, but it can consume an awful lot of current from the Arduino and cause the voltage on the Arduino to sag. Hence the need to keep them isolated.

Delta_G:
No, but it can consume an awful lot of current from the Arduino and cause the voltage on the Arduino to sag. Hence the need to keep them isolated.

Got it. I have a few short questions how to implement them though.

This is how I would wire them right?

right pin to ground, left to pin, and middle to relay.

Would the code be any different?

Thanks.

No, that's not how you would wire it. The pin would go to the base which is the middle pin. Which of the other two is which is hard to say since i don't know which way round you have the transistor in the drawing. But the collector goes to the relay low side and the emitter goes to ground.

Look here: http://www.electronics-tutorials.ws/blog/relay-switch-circuit.html

The advice given by Delta_G is for bare relays, not for a relay board.
I'm going to assume you use a common 8-relay board.

It is not a problem to control an 8-relay board with the Arduino pins,
but powering such a board from the Arduino is probably not ok.

If all relays are "on", the board (relay coils) draw about 650mA.
Too much for USB (500mA fuse), and likely too much if you power the Arduino from the DC socket (hot regulator).
You should have answered all the questions from post#6

You might have to use a separate 5volt/1Amp (cellphone) supply to power that relay board.
Post a link (or picture) of the board, so we can give the right advice.
Leo..

It's this one:

What kind of seperate power supply would I need?

Forgot to add, the relay is low active. Not sure if that would change anything.

Purchased this one here: 5V relais 8-channel laag-actief - 5VREL8

You need a 5volt supply, e.g. old cellphone or tablet charger, with a minimum rating of 1Amp, because the board can use up to 650mA.

Remove the JD-VCC jumper.
Connect the 5volt supply(+) to JD-VCC and relay ground(-).
Connect VCC (from the long row) to Arduino 5volt.
Connect relay inputs to Arduino pins.

DO NOT connect relay ground to Arduino ground.
If you do, you don't have opto isolation.

The Arduino needs it's own supply (for opto isolation).

If you don't need the extra safety of opto isolation, then you can connect relay ground to Arduino ground.
In that case, the Arduino will be powered from the relay supply.

Low is indeed active.
To stop the relays from chattering during bootup,
digitalWrite a HIGH to the pin BEFORE you set the pin with pinMode to OUTPUT.
Leo..

Wawa:
You need a 5volt supply, e.g. old cellphone or tablet charger, with a minimum rating of 1Amp, because the board can use up to 650mA.

Remove the JD-VCC jumper.
Connect the 5volt supply(+) to JD-VCC and relay ground(-).
Connect VCC (from the long row) to Arduino 5volt.
Connect relay inputs to Arduino pins.

DO NOT connect relay ground to Arduino ground.
If you do, you don't have opto isolation.

The Arduino needs it's own supply (for opto isolation).

If you don't need the extra safety of opto isolation, then you can connect relay ground to Arduino ground.
In that case, the Arduino will be powered from the relay supply.

Low is indeed active.
To stop the relays from chattering during bootup,
digitalWrite a HIGH to the pin BEFORE you set the pin with pinMode to OUTPUT.
Leo..

Thanks man, that's really helpful!

One last question though, I only have USB power supplies. Could I cut a usb cable in half and use those?

InsaneCold:
One last question though, I only have USB power supplies. Could I cut a usb cable in half and use those?

Yes.
Make sure the supply is able to provide at least 650mA.
Leo..