Electronic – arduino – How to output sounds over a headphone wire

arduinosound

I soldered some headers to a headphone line and was wondering how to make sounds come out of an audio device connected to that. I don't mean music, or anything advanced. Just two tones for RTTY. I'm using an arduino and know it has a sound library. Would that help at all?

Best Answer

The easiest way to make noise is to rapidly turn an output on and off.

The faster you turn it on and off the higher pitched the sound will be.

One common way of doing it is to use one of the PWM outputs, which gets turned on and off for you by the chip.

If you want to get more fancy you can look into how you can emulate a Digital to Analog converter using PWM, or read up on creating an "R/2R" resistor ladder DAC. That would allow you to create more complex waveforms.

Edit: I wrote this before I had used an Arduino much.

It's very easy on an Arduino.

  1. Connect a speaker / headphones / piezo sounder between one of the PWM pins (marked with a ~) and ground.*
  2. In your sketch call the tone function: tone(pin,frequency[,duration]);

(Frequency is in hertz, and duration is in milliseconds)

For example, if the speaker is on pin 6 then you can use:

tone(6,440,1000);

Which will play A4 for 1000ms (1 second).

*: Because the tone you are playing is an AC signal, good circuit design would dictate that you place a capacitor in line with power to couple your signal. However, you're using headphones here so it doesn't really matter too much; the effects become more significant when you're driving a larger load (e.g. a speaker).