Arduino - How do I reduce the noise from the speaker

I have a fairly simple circuit consisting of an arduino an accelerometer and an amplifier. I am collecting data from the accelerometer using I2C and playing some sounds through a switching amplifier (this). The problem is I am getting a constant noise in the background. I am 90% sure it is because of the I2C connection between the accelerometer and the arduino.

I am using an inductor of 10mH and cap 100nF The amplifier is connected across the capacitor and is receiving power from arduino 5 volt supply. (I didn't draw it here). Also, I am not using external pull-up resistors with the accelerometer because apparently I don't need to when working with adxl 345

How can I reduce this noise? Where is it coming from? Could anyone provide reading resources for problems like these?

There are several possible reasons for the noise - if you are sharing power between
the Arduino and the amplifer the digital noise on the supply is likely to be audible
(that's why a separate analog supply is normally used).

You may have quantization noise from the samples themselves.

There may be timing jitter (aka phase noise) on the samples to the amp, again
leading to audible distortions / noise.

anath2:
playing some sounds through a switching amplifier

Somewhat common blunder that turns up here from time to time.

You do not amplify PWM with a switching amplifier!

There are several possible reasons for the noise - if you are sharing power between
the Arduino and the amplifer the digital noise on the supply is likely to be audible
(that's why a separate analog supply is normally used).

You may have quantization noise from the samples themselves.

There may be timing jitter (aka phase noise) on the samples to the amp, again
leading to audible distortions / noise.

Okay I looked up the terms you threw at me. I don't think the noise is because shared power or quantizaton. What I am experiencing is a sort a of high frequency hum in the background
My code consists of the if else statements (When the accelerometer value is above a threshold, play some sound). I think the noise has something to do with the fact that I am polling the accelerometer data all the time.

There's no noise in the background when I disconnect the accelerometer

The noise is normally carried by the power rails, whatever the actual cause, since those are
the wires that connect noise source and analog amplifier together.

What noise? White noise? Buzzing? A hum?

My car is making a noise. What is wrong with it?

If you are using analogWrite, that is a PWM of only 490Hz, or 980Hz, depending on the model of Arduino and which pin you are using. That is probably what you are hearing.

I would assume it's normal PWM noise. (You don't exactly get high-fidelity from the Arduino's PWM. :wink: )

I am 90% sure it is because of the I2C connection between the accelerometer and the arduino.

Do you hear the noise when there's silence (no signal)?

If you hear noise when there is otherwise silence, it could be the I2C signal, or other "digital noise", or it could be from the power supply.

If you only hear noise only when there is a signal, that's probably just normal low-resolution, low-frequency PWM noise.

It should be easy enough to write a sketch that either ignores the accelerometer and puts-out silence, or a sketch that makes sounds & silence without running the I2C.

What noise? White noise? Buzzing? A hum?

My car is making a noise. What is wrong with it?

I am getting a high frequency hum

If you hear noise when there is otherwise silence, it could be the I2C signal, or other "digital noise", or it could be from the power supply.

Yes that right! I am getting a hum when there's silence otherwise.

It should be easy enough to write a sketch that either ignores the accelerometer and puts-out silence, or a sketch that makes sounds & silence without running the I2C.

My code works by constantly polling the accelerometer for data . When the acceleration from one of the coordinates is greater than a threshold sound is produced. How can I do the above ?

Oh, so you don't believe me eh?

Ah well. Must I explain the obvious?

The "switching amplifier" works by converting it input signal - to PWM! It expects this PWM to be later filtered out by the inductance of the speaker (which is by the way, not going to happen if as your diagram subtly implies, you are using a piezo speaker).

So you are now feeding a chopped signal into a further chopper - what happens as the two choppers intermodulate, is anyone's business. But probably the result you are experiencing is one.

I can only say - you do not amplify PWM with a switching amplifier. You simply feed it into a buffer (an LM386 would in fact do, or just a couple of transistors since there is no need for linearity; it is digital) and feed that through your LC filter to your speaker.