4

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
}

1 Answer 1

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.