0

How to Turn on an External Amp via GPIO Pins/Arduino

Hey everyone! So I searched and searched both on here and through Google/other forums but could not find a way to use the GPIO pins on the RPi to remotely turn on an external amp. So I came up with my own way and would like to share as I've seen many people ask/wanting a solution.

I use my Raspberry Pi and Hifiberry DAC+ Pro as an AirPlay server for a multiroom speaker system. With this setup, my music/amp turns on within 1-2 seconds of receiving a stream from any AirPlay device. Then turns off within 5 seconds of the stream stopping. Now I no longer need to go to the basement to turn the amp on or off!

This setup has worked flawlessly so far but may not be for everyone. My amp allows for a 5-24v DC input to control the power on state. However, with the 5v output from the Arduino, you can easily modify this setup to trigger a 5v relay, NPN transistor, etc to power your amp.

First off, hardware...

  1. Raspberry Pi 3
  2. HifiBerry DAC+ Pro
  3. Arduino Pro Mini

Ok so I probed all the GPIO pins on the DAC+ Pro and found that pin 40 (GPIO21) would hover around 1.4-1.6v when playing music. When there was no music playing, this pin went to 0v. Its basically in sync with the LED on the DAC+. Now reading this voltage on a multimeter looked stable. However the Arduino would occasionally pick up a reading of 0. At first I tried using a simple if statement but this caused the amp to turn on and off every few seconds due to the random 0 value reading. Adding a 2-5 second helped, but once in awhile it would read 0 and turn the amp off. Using the delay actually made the situation worse and I would have to wait 2-5 seconds for the amp to turn back on!

Then I came across smoothing!...which averages the readings and allows me to use the average as a value. See below on how the RPi3/DAC+ Pro and Arduino are setup.

Arduino Wiring:

  • RPi Pin 40 (GPIO21) -> Arduino A1
  • RPi Pin 2 or 4 (5v to power Arduino) -> Arduino VCC
  • RPi Pin 39 (or any other ground pin) -> Arduino Ground
  • Arduino Pin 5 (5v output) -> Amp
  • Arduino Ground -> Amp

Note: As mentioned above my amp takes a 5v input to control the amp. It uses an RCA connection for this. So I just cut up an old RCA cable I had laying around and used the inner, insulated wire for 5v and the outer shielding for ground.

Arduino Code:

//Code by Ryan Lee
//Auto Amp Turn On
const int numReadings = 10;

int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int val = 0;
int REM = 5;
int Trigger = A1;

void setup() {
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
pinMode(REM,OUTPUT);
}

void loop() {
total = total - readings[readIndex];
readings[readIndex] = analogRead(Trigger);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
delay(1);
if (average > 0)
{digitalWrite(5,HIGH);
}
if (average < 1)
{digitalWrite(5,LOW);
}
}

 Hope this helps those who are looking for a similar solution!

3 comments

Please sign in to leave a comment.