user19076: When I call the tone function on a Uno, why do I not have to declare the pinmode in setup? Like so:
// Simplest tone
void setup() {
}

void loop() {
    tone (8,500); //on pin 8 output 500 Hz
}


user6628: Because the tone(hertz) code initializes the pinmode, along with the timer/counter control registers to produce the appropriate frequency.
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
  uint8_t prescalarbits = 0b001;
  long toggle_count = 0;
  uint32_t ocr = 0;
  int8_t _timer;

  _timer = toneBegin(_pin);

  if (_timer >= 0)
  {
    // Set the pinMode as OUTPUT
    pinMode(_pin, OUTPUT);
   ... 

See Tone.cpp - line 255 for the source.

19076: <p>When I call the tone function on a Uno, why do I not have to declare the pinmode in setup? Like so:</p>

<pre><code>// Simplest tone
void setup() {
}

void loop() {
    tone (8,500); //on pin 8 output 500 Hz
}
</code></pre>
 6628: <p>Because the <code>tone(hertz)</code> code initializes the pinmode, along with the timer/counter control registers to produce the appropriate frequency.</p>

<pre><code>void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
  uint8_t prescalarbits = 0b001;
  long toggle_count = 0;
  uint32_t ocr = 0;
  int8_t _timer;

  _timer = toneBegin(_pin);

  if (_timer &gt;= 0)
  {
    // Set the pinMode as OUTPUT
    pinMode(_pin, OUTPUT);
   ... 
</code></pre>

<p>See <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Tone.cpp#L255" rel="nofollow">Tone.cpp - line 255</a> for the source.</p>
