I've searched and searched, but drawn blanks so far, so here's my project.
I have a Chromecast audio connected to a chinese Class T amplifier, running a pair of nice bookshelf speakers. There is a 12v 5a PSU feeding the Amp, and a 12-5V converter spliced into this to power the Chromecast. All of this works very nicely, but the Amp is On all the time unless I physically switch everything off. This is less than ideal as I intended to duplicate this setup and install some behind in-ceiling speakers.
I'd like to be able to install a relay to switch on the 12V to
the Amp when audio is detected on the Chromecast's output jack, and hold On for 20 seconds or so once silence is detected. The 12V will always be available before the relay, as it's effectively powering the Chromecast 24/7 and, all being well, an Arduino Nano.
I have all the physical parts I need such as the relay, and I understand I need to DC-bias the audio with a few passive bits to keep the Nano safe from harm.
I basically need help with the programming side of things, and any advice from folks who've tried similar would be a great help.
you will need extra hardware to be able to detect correctly audio output from the Chromecast. Typically, the line level is 0dB (0.775V RMS), but you have to assume that the minimum signal you have to detect is -40dB or -50dB (hard to say without test), which is only a few mV RMS.
The ADC on Arduino will not be sensitive enough to detect "safely" a signal from the Chromecast. It may either consider that there is no signal while the output is very low (don't forget that human hearing range goes down to -60dB, which is 1/1000).
Or, depending on the Chromecast signal, the Arduino may consider the background noise as a valid signal (since it will be a value around 5 or 6 on ADC input).
The problem is not even solved at this moment if you put a clipping amplifier in order to raise the level to something that the Arduino can detect. The Delta-Sigma converters on DAC output of the Chromecast are very, very noisy. Just put a scope on the output, you will understand. This noise comes from the dithering process. It can not be heard by human hears, but it will be amplified...
So, simply said, the first thing you have to do is to raise the signal level with an external opamp in order to have the Arduino able to "see" it, then simply make a program with a threshold and a timer to control the relay.
But as I said, the external opamp processing will not be so easy to do (probably requiring a low-pass filter too, to remove the dithering noise)
I think you'll be fine without an amplifier. Read the analog input and send the numbers to the serial monitor to see what you're getting. (If you don't understand what I'm saying, look at the [u]Analog Read Serial Example[/u].
With the input biased at 2.5V (half the supply voltage), silence should read about 512 and with sound, you should read values above AND below that. You can use the higher numbers as-is to decide if you want the relay on or off, or you can subtract 512 and take the absolute value, etc.
If there are long periods of near-silence, you can simply increase the delay time.
"Deciding" is simply an if-statement.... If the reading is above some (currently unknown) level, turn the relay on.
For the 20-second timer, you can use the method from the [u]Blink Without Delay Example[/u]. The idea is to read the signal in a "tight loop" and set a new end-time whenever the signal is high-enough to keep the relay on (whatever signal level you determine is appropriate). In other words, keep "resetting" the timer as long as the signal is present.
If you use a normal delay(), it won't work properly because the software can't read the analog signal (or reset the timer, or do anything else, during the delay-time.