Speaker Whine and Loop Termination

Hello all,

I am trying to get a project working where when someone approaches the device, audio plays. The way I have gone about it is by having a nano controlled ultrasonic sensor to determine distance, then a microSD card reader to play a sound file. There is an amplifier, Adafruit class D amp (PAM8302), between the Nano and the speaker, which is a 2", 4 Ohm speaker.

After booting, the speaker does a quick beep and then is silent. When the ultrasonic sensor is triggered, there is immediate noise and a high pitched whine that remains even when the audio file is done playing. Triggering the file playing is in the loop portion.

One final note is that when the device is connected to a battery rather than a USB cable, the loop doesn't execute more than once.

I know I've missed a big part of this but I haven't found a solution in the forums (or I'm not using the right key words in my search). What is my next step for troubleshooting the problem? Thanks!


#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>

//SD Setup
SdVolume volume;
SdFile root;
TMRpcm soundBite;

//Pins
const int ultrasonicTrigPin = 7;
const int ultrasonicEchoPin = 6;

//Variables
long duration = 0;
int distance = 0;
int soundBiteDelay = 1000; //delay between sound bites
int volumeCtrlOutput = 5;
int soundBiteLength = 8000; 

void setup()
{
 pinMode(ultrasonicTrigPin, OUTPUT);
 pinMode(ultrasonicEchoPin, INPUT);
 soundBite.speakerPin = 9;
 Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) 
   return;
 soundBite.volume(volumeCtrlOutput); //Volume from 0 to 7
 soundBite.quality(1); //Only accepts 1 or 0 as inputs
}

void loop() 
{
 delay(1000);
 digitalWrite(ultrasonicTrigPin, LOW);//Ultrasonic sensor reset
 delayMicroseconds(2);
 digitalWrite(ultrasonicTrigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(ultrasonicTrigPin, LOW);
 duration = pulseIn(ultrasonicEchoPin, HIGH); //Returns pulse duration in microseconds
 distance= duration*0.034/2; //Convert to cm
 Serial.println(distance); // only necessary for testing, displays distance on PC
 if((distance < 50) && (distance != 0))
 {
   soundBite.play("1.wav");
   delay(soundBiteLength);
   delay(soundBiteDelay);
 }
}

What is my next step for troubleshooting the problem?

First please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

When you have done you should see that to help we need a schematic of your circuit including details of how you power it, what decoupling capacitors you have and preferably with a photograph of your wiring.

Thanks for the response! I updated the code section and will draw up a schematic and post that!

As for power, I'm using 4AA alkaline batteries to the Vin pin. The 5v out if the nano powers the rest.

I don't have any decoupling capacitors, that might be my problem.

The whine is probably digital switching noise coming through the power supply. You can test that by disconnecting the audio-input to the amplifier, or by turning the volume all the way down if you have a volume control. If it's getting-in through the power supply, a capacitor (probably 1000uF or more) across the power supply to the amplifier may help.

Audio circuits are sensitive to noise (or you can say our ears are sensitive to noise. :wink: ). In a perfect world, your audio circuitry would have a separate voltage regulator.

After booting, the speaker does a quick beep and then is silent. When the ultrasonic sensor is triggered, there is immediate noise

I don't know why there's a "beep" when you power-on but a click or pop is normal with an amplifier unless there is some kind of soft turn-on circuit.

There will also be a "click" when the audio starts because the I/O pin will be resting at 0V or 5V (probably 0V) until the audio suddenly kicks-in.

Also since the Nano (like most Arduinos) doesn't have a DAC, you are not feeding analog into the amplifier. You are feeding it 5V PWM (presumably unfiltered). I don't know what that does to a PAM amplifier but it's probably not good. :wink:

(I didn't look at your looping problem.)

As for power, I'm using 4AA alkaline batteries to the Vin pin.

This only gives you 6V above the regulation voltage which is not enough. Sure when they are new you will get more than this but actually need a minimum of 6.5 to 7V for proper regulation.

No decoupling is not going to do you any favours, and using batteries you need capacitors on the input more than ever because batteries have a high impedance and the capacitors lower it.

The initial beep is probably the PWM going off at the default frequency before the code pulls it into the correct ultrasonic mode.

DVDdoug

  • 1000uF capacitor across input to power supply definitely worked!
  • Thanks for the info on the click/pop on the speaker!
  • I'll have to look into using a DAC, I've never used on before but that is very helpful info!

Grumpy_Mike

  • I bumped the power up to 7.5 and it seemed to fix my looping problem, strangely enough.
  • Thanks for the beep information I would not have thought of that.

What I've got now is a little wavering screech that settles on a single high pitch tone, after the audio has been triggered and continues after the audio ends. This is after the addition of the 1000uF cap across the amp input and bumping the power up. When I hit the reset button it goes silent...until the ultrasonic sensor is tripped again. What else could be causing the high pitched tone?

Also, I have attached a snip of a Fritzing sketch I put together today. I hope it is helpful. Thanks for the help so far!

Try moving the battery input to directly across the input capacitor. The wiring is not well laid out for minimising interference. Ideally you should use what is known as star wiring where the power and ground all come from a common spot and are not part of a chain of wiring.
Some 0.1uF ceramics would also help cut down high frequency noise.

Grumpy_Mike, it's like you've done this before. Moving the cap across the input power helped. Being very new to audio, this was informative.

Thanks for the help. Where the project is now is a significant improvement.

Glad to be of help

it's like you've done this before

Well no, but then I would never use solderless breadboard in the first place. What I did do was apply my 50 odd years experience with electronics to your situation. Very happy to pass things on.