# How to output sounds over a headphone wire?

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?

• the tag does the job of noting what type of category it falls in, no need for arduino in the title. – Kortuk Aug 14 '11 at 23:25
• You should say "Arduino" in the title so the question isn't vague. – endolith Aug 15 '11 at 14:28

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).