Can't use digital pins 0 or 1 for speaker, it makes high pitched tone

  • Arduino Uno

I'm trying to use my only two remaining pins, either Digital 0 or Digital 1 (rx and tx for serial too, I know) to drive a small piezo speaker. But as soon as it's connected and the moment the Uno receives power, the speaker starts making a very high pitched tone, one constant tone. Higher pitched for D1 than D0.

All the other pins are occupied by this 2.4" TFT LCD touchscreen:

My code does not have any Serial. statements in it, I am not engaging the serial interface.

I am including these libraries:

But I searched through them and they ALSO do not have any Serial. statements. As far as I can tell no part of my code is using the serial interface, so why does my speaker make a high pitched tone when it's connected to D0 or D1?

How do You power Your UNO? By USB? If so, that can be the reason. Try powering it using the 7 - 12 volt input plug.

Oh, that barrel jack is supposed to be 7-12v? No wonder my 5v didn't work right...

Anywho yes I am using USB, I had thought of that, and I tried using a USB wall adapter instead of my PC, but it produced the same problem.

BUT I found a semi-solution!

I found this on google, added this line to my void setup:

UCSR0B = 0;

I still get the tone while the Arduino is booting up, but at least once that line of code runs, the digital pin and speaker work properly. No idea what that line of code really does, I'm surprised that variable is even declared somewhere, but it is.

I take my hat off. You know more then I do. UCSR0B probably refers to a “controller register”…

Are you using all the analog pins on your Uno. If not, they can also be used as digital pins.

A quick look at the data sheet told me that this register controls the USART transmitter, which is apparently enabled by default even if not used.

Oh, that barrel jack is supposed to be 7-12v? No wonder my 5v didn't work right...

If you have a regulated 5V supply just connect it to the 5V or Vcc pin (name varies between boards). Not the Vin pin, which is the same as the barrel jack, and which is connected to the linear regulator.

"regulated 5V supply"