TMRPCM and analogWrite

Hey All,

I'm having an issue using the TMRPCM library. I can get everything to work great until I add in analogWrite for some RGBs. As soon as I do that my speaker outputs a constant high pitch noise that is very annoying. I have tried multiple fixes found when searching but can't seem to get this working unless I remove the analogWrite from my code. Any ideas?

#include <SdFat.h>      //sd card library
SdFat sd;               //sets SdFat library to act like SD library (saves space and improves performance)
#include <TMRpcm.h>     //Sound library
#include <SPI.h>

#define SD_ChipSelectPin 4 //sd shield uses pin 4 for CS

TMRpcm tmrpcm;   // create an object for use in this sketch

// Init the Pins used for toggles!
const int blueTogglePin = A4;
int blueToggleRead;
int playedBlue;

// Init the Pins used for PWM
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;


void setup()
{
  Serial.begin(9600);
  pinMode(blueTogglePin, INPUT);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  tmrpcm.speakerPin = 9;
  //tmrpcm.setVolume(0);
  tmrpcm.quality(1);

  if (!sd.begin(SD_ChipSelectPin, SPI_FULL_SPEED)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
    else {
    Serial.println("SD Succeeded");
  }
}









void loop() {

     // setColorRGB(random(255),random(255),random(255));
      analogWrite(redPin,255-random(255));;
      analogWrite(greenPin, 255-random(255));
      analogWrite(bluePin, 255-random(255));

      delay(30);

        if (!tmrpcm.isPlaying()) {  //if no sound is playing disable the speaker to prevent the annoying high pitch noise
           tmrpcm.disable();
        }


         blueToggleRead = digitalRead(blueTogglePin);
        if (blueToggleRead == HIGH){
        //Serial.println("Blue Toggle On!");       
        if(tmrpcm.isPlaying()){
          //do nothing if something is playing
        }
        else {
          if (playedBlue != 1){
          tmrpcm.play("smallstep.wav");
          }
          playedBlue = 1;    
          }         
        }
        else {
          playedBlue = 0;
        }
delay(30);
}




void setColorRGB(unsigned int red, unsigned int green, unsigned int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

The problem is one of construction. We need to see a schematic and a photograph of how you have implemented it.

Nothing to do with the code.

Sorry it took so long to reply. Attached is a fritzing breadboard view. Things to note (and it may or may not be part of my problem?)

Wireless SD Shield used
Toggle Switch: SPST switch is being used in my circuit (fritzing only has SPDT).
RGB: common Anode
Speaker: It is a 4 wire speaker from an older Dell desktop. On it I am using the +5V, GND, and Green data line. I do know this works because in other sketches I can get files to play just fine through the speaker. There is also a 100 OHM resistor on the data line.

I definitely appreciate any help you and all others can provide! You guys have saved my butt before!

I did ask for a schematic and photograph. You gave me a piece of Fritzing crap.

It shows a speaker and then you say

Speaker: It is a 4 wire speaker from an older Dell desktop. On it I am using the +5V, GND, and Green data line

So even the layout is not what it purports to be. That is why we hate Fritzing here, people just can not use it correctly and just substitute parts willie nillie. And is it all on a bread board? Bad idea if it is. You should take the audio ground directly from one of the Arduino Gnd pins.

You need a reconstruction filter on the audio output to smooth the PWM, use an RC filter with 1K5 series resistor and a 10nF going to ground. You might want to lower the impedance on the input to your amplifier by putting a 4K7 resistor from ground to amplifier input, this will reduce the amount of interference pickup you get from the LEDs switching.

Those LEDs have no current limiting resistor meaning that not only are you over stressing the LED and Arduino but you are generating much more interference than you need to. You need at least a 220R to 470R resistor in each of the cathodes.

Then, maybe if you still get interference, we can address the layout.

Wow. Well I understand how you got your name.. :slight_smile:

Grumpy_Mike:
I did ask for a schematic and photograph. You gave me a piece of Fritzing crap.

It shows a speaker and then you say So even the layout is not what it purports to be. That is why we hate Fritzing here, people just can not use it correctly and just substitute parts willie nillie.

This is part of a bigger project. I do not have a readable schematic as it was built in Fritzing. While Fritzing is not perfect, it does have a lot of advantages. But I can see how the experts around here may dislike it. But if you do take time to look at the picture I clarify with a textbox with a third line going to the speaker that it is the data line for it. I could also show you a picture but that would not provide any clarity as the fritzing layout is cleaner and as I said before stripped down from the bigger project.

Grumpy_Mike:
And is it all on a bread board? Bad idea if it is. You should take the audio ground directly from one of the Arduino Gnd pins.

As with all of my projects I first build them on a breadboard.

Grumpy_Mike:
You need a reconstruction filter on the audio output to smooth the PWM, use an RC filter with 1K5 series resistor and a 10nF going to ground. You might want to lower the impedance on the input to your amplifier by putting a 4K7 resistor from ground to amplifier input, this will reduce the amount of interference pickup you get from the LEDs switching.

This is helpful and something I can try. Thanks.

Grumpy_Mike:
Those LEDs have no current limiting resistor meaning that not only are you over stressing the LED and Arduino but you are generating much more interference than you need to. You need at least a 220R to 470R resistor in each of the cathodes.

I apologize yet again. My LEDs are shrinktube wrapped and I simply forgot about the 220R resistors. I can assure you that there is one on every cathode.

Grumpy_Mike:
Then, maybe if you still get interference, we can address the layout.

I used a 1K resistor and 22nF cap. Its all I have at the moment. This drastically lowered the high pitch noise (although you can still barely hear it). The problem now is that none of the other sounds play from the speaker. Not sure if that is because my values are off or if the filter in general is not the best idea.

But if you do take time to look at the picture I clarify with a textbox with a third line going to the speaker that it is the data line for it.

Sorry but that clarified nothing.

I could also show you a picture but that would not provide any clarity as the fritzing layout is cleaner and as I said before stripped down from the bigger project.

The whole point of a photograph is that it shows the layout as it is and so allows us to spot any layout problems that simply will not show up on a "clean" layout.
It is odd how you will admit to not knowing about electronics but you are adamant about what information you need to supply to solve your problem. I think the person trying to solve your problem might just know what information they need.

I do not have a readable schematic as it was built in Fritzing.

I often here this from beginners. You can not build anything without a schematic, attempting to do so is just folly.

While Fritzing is not perfect, it does have a lot of advantages.

Does it? Never noticed one at all. Much better to use a pencil and paper and post a picture.

Wow. Well I understand how you got your name

It might surprise you to know that I chose my name, just like you chose yours.

I wonder if when you go to the doctor and he says "where dose it hurt" you say "guess"? When he says "lets listen to your chest" you say "Oh no need I can do an impression of it". I suspect you cooperate more.

The problem now is that none of the other sounds play from the speaker. Not sure if that is because my values are off or if the filter in general is not the best idea.

The values are close enough, and if you wired it correctly it will not stop sounds from being produced, so therefore I can only conclude that you have not wired them up correctly.