Simple LAN Controlled Stereo Audio Switch

{lang: 'en-GB'}

Audio Switch (Main Board)

I needed an idea that I could use to teach myself about using a relay with Microcontrollers and solve that age old problem: Switching between speakers and headphones on my PC without having to actually get up and mess around in the rats nest of wire’s behind it.

The solution: Use a 5v DPDT relay, a few 3.5mm stereo sockets and an Ethernet-capable Arduino to create a simple audio switch which would allow me to switch one audio source (my PC’s Soundcard in this case) to one of two outputs (headphones or speakers) via a web interface running on the Arduino from the comfort of my web browser.

What You’ll Need

A list of  the hardware and software components you will need to build this project can be found below:

Hardware

  • 1x Arduino equipped with an ATMEGA168 (or 328) and  an  Ethernet Shield
  • 1x 5v DPDT Relay. (I got mine from here.)
  • 1x BC337 NPN Transistor.
  • 1x 4.7K Resistor.
  • 1x 1N4001 Diode.
  • 3x 3.5mm Stereo Jack Sockets.
  • 1x 3-way 0.1″ pin header.

Software

  • The Webduino library for the Arduino IDE. (Installation instructions can be found at the same site.)
  • The NetAudioSw Arduino sketch. You can get this from the software page.

Calculating the Relay Coil Current

In order to determine whether the Arduino could supply enough current to power the relay and whether the transistor I had in mind was up to the job of switching a sufficient  amount of current, I had to calculate how much current the relay would need. In order to do this you’ll need to know two things:

  1. The supply voltage of the relay.
  2. The resistance of the Relay’s coil.

Both items of information can be found on the datasheet for your chosen relay. The supply voltage should be printed on the relay itself and you can use a multimeter to measure the resistance of the relay’s coil if you’re unsure. I cheated a bit and used the values given on the website I bought the relay from.

Once you have those two bits of information, the simple formula below will give you the Current value (In Amps) we need to supply to the relay coil.

Coil Current = Supply Voltage / Coil Resistance

In my case the supply voltage was 5v and the coil resistance was 125 Ohms. So, putting those values into the formula above gives us:

Coil Current = 5 / 125 = 0.04 amps.

To get this value in Milliamps (ma), just multiply it by 1000:

Coil Current (in Milliamps) = 0.04 * 1000 = 40ma.

Choosing the Transistor

So, from doing the calculations above, I determined that I needed a transistor that could switch at least 40ma of current. A quick look at the datasheet for the BC337 NPN transistor shows that it can indeed switch this much current as the Collector Current value in the datasheet shows it’s rated for an absolute maximum of 800ma. So, I could get what I needed as well as a very comfortable safety margin to play with if I wanted to add anything else to the circuit in the future.

Putting It All Together

To better explain how everything fits together, I’ve decided to do two schematic diagrams (which can be found below.) The first schematic (Fig1) shows how the relay driver circuit is connected to the Arduino, while the second schematic (Fig2) shows how the single audio input and two outputs are connected to the relay pins.

Fig1: Relay Driver Circuit

Fig 1: Relay Driver Circuit

Fig 1: Relay Driver Circuit. Click image to view full-sized version

The relay driver circuit is simple enough. Vcc (5v) is taken from the Arduino board to power the circuit and a digital I/O pin (Pin 4 is used in my Sketch) from the Arduino is connected to the base of the BC337 NPN transistor via a 4.7K resistor. Note that you can’t use Digital I/O pins 10,11,12 and 13 as these are used by the Ethernet Shield.

The 1N4001 diode, connected in parallel across the relay coil, is used to protect the transistor and Arduino from brief high voltage spikes when the relay is activated and de-activated. A much better explanation of the use of a protection diode and why it is necessary can be found here.

Fig 2: Audio Input/Output Connections to Relay

Fig2: Relay Pin Connections

Fig2: Relay Pin Connections. Click image to view full-sized version

Both the left and right channel pins of the audio input connector are connected to the two pins nearest the relay coil.

The left and right channel pins (Labelled L and R on the diagram) of the output 1 and output 2 connectors, respectively, are connected to the two remaining pairs of pins on the relay, as shown in Fig 2.

Important: Separate grounds must be used for power and audio input/outputs in this project! These are marked on both the diagrams as Audio Ground[For the audio input and outputs] and Power Ground [For the relay driver circuit].

I cannot (and will not) be held responsible for any damage to equipment (or people for that matter) if you don’t take note of this.



How it Works

When the relay is off (No power, or relay control pin on the Arduino is LOW), output No. 1 will be connected to the audio input source.

When the relay is in (Powered up, relay control pin set HIGH), output no.2 will be connected to the audio input  source.

Using the Software

The NetAudioSw sketch is simple to use. To customise it for you own needs you will need to alter the following lines:

  • #define CTRL_PIN- This determines which digital i/o pin is used to drive the relay. By Default it is set to pin 4.
  • static uint8_t mac[]- This sets the MAC address for the Ethernet Shield. Change this if it conflicts with the MAC addresses of other  devices on your LAN.
  • static uint8_t ip[]- This sets the IP address of the Ethernet Shield. Change this to suit your own LAN’s addressing scheme.

Once you’ve customised it to suit your needs, just type the IP address you assigned to the Ethernet Shield into your web browser.

You can add the suffix /output1.html to switch to output 1 and /output2.html to the address to switch to output 2. If you just type the IP address of the Ethernet Shield without a suffix into your browser, you get a page with links to both URL’s. A better idea would be to bookmark both URL’s so you can switch between outputs with a single click.

The sketch also stores the last known state of the relay in EEPROM. So, if the Arduino looses power or is reset, it will restore the relay to the last known state at startup.

Limitations

I’ve tried to keep this as simple as possible, so I’ve omitted things like indicator LED’s and a switch to toggle the relay from the Arduino board. These would be trivial additions to make, but as this project was meant to be out of sight behind my desk (when I get around to soldering a proper board together, I wouldn’t do that to my poor Arduino), I didn’t think these things to be necessary.

Until Next Time…

Hope this is useful to someone. If you build it yourself, let me know how you get on. Remember, any and all (constructive) criticism is welcome. Happy hacking, everyone.


7 thoughts on “Simple LAN Controlled Stereo Audio Switch

Comments are closed.