How to connect Arduino to headphone jack?

This project connects the headphones directly to a digital pin. Here is the schematic:

enter image description here

I would, however, be more cautious and add a limiting resistor. The output of an Arduino pin should be limited to 20mA as stated here. So, using

V=I*R

5 = 0.02 * R

R = 5 / 0.02

R = 250ohms

So I would add a ~250ohm resistor in series with the head phone. Especially if I were debugging my code. That is, in case I accidentally left the output pin high for an extended period of time.

The head phones should have some DC resistance. If the sound is not loud enough you might measure that DC resistance of the head phones and subtract it from the 250 ohm resistance calculated above and use a limiting resistor of that value instead.