Tone doesn't work [Solved]

I’ve seen that the tone and noTone functions were added to the firmware, and there are even examples of sounds that @BDub has created.

However - I just can’t seem to make tone work!
To make sure I didn’t go out of my mind, I did the following:

  1. I used the simplest code I could think of, in two versions:

    void setup() {
    pinMode(D5, OUTPUT);
    tone(D5, 1047);
    }

    void loop() {

    }

and

void setup() {
    pinMode(D5, OUTPUT);
}

void loop() {
    tone(D5, 1047);
}
  1. I connected a multimeter to the D5 and GND legs, and it shows zero!

  2. To make sure the multimeter is not broken, I did the same with the V3.3 leg, and it showed the correct voltage. Also, it’s showing some voltage in an original arduino I have using the same setup.

I used the above steps to make sure the problem is not in my piezo, multimeter, jumpers, and such.

Am I insane ? Is it me ?

Thanks.

On the Github issue located here, I incorrectly stated tone works on D4-D7. I’ve edited that post and provide a sample application for you here that works.

The pins you can use for tone() / noTone() are: D0, D1, A0, A1, A4, A5, A6, A7, RX, TX

#include "application.h"

// The pins you can use for tone() / noTone() are: 
// D0, D1, A0, A1, A4, A5, A6, A7, RX, TX
const char tonePins[] = { D0, D1, A0, A1, A4, A5, A6, A7, RX, TX };

void setup() {
  for(uint8_t x=0; x<arraySize(tonePins); x++) {
    pinMode(tonePins[x], OUTPUT);
  }
}

void loop() {
  for(uint8_t x=0; x<arraySize(tonePins); x++) {
    tone(tonePins[x], 1000);
  }
  delay(5);
  for(uint8_t x=0; x<arraySize(tonePins); x++) {
    tone(tonePins[x], 2000);
  }
  delay(5);
  for(uint8_t x=0; x<arraySize(tonePins); x++) {
    noTone(tonePins[x]);
  }
  delay(5);
}
3 Likes

Yep, that did it. Moved it from D5 (also checked D4 before…) to D1 and it’s working!
Awesome. Thanks @BDub

1 Like

Resurrecting this topic for Photon: What about A6 (dac) and A7 (wkp)? Cannot get tone() working on A6, haven’t yet checked A7. The pin-description docs are somewhat ambiguous; am I to presume that tone() is only available on a PWM-capable pin? If so, then A6/DAC is definitely not compatible moving from Core to Photon. Can you clarify, @BDub?

@johnshifflett, @BDub is the guy but I can tell you that Tone is a variation of PWM and both are tied to hardware timers. I don’t believe that A6 & A7 are tied to timers so they would not support Tone or PWM. :smile:

Thanks for the response, @peekay123 - I’m sure you’re right about this.

Hi there,

just moved my little project from Core to Photon and noticed, that the buzer doesn’t generate any tone on A0 or A1. It works perfectly on D1 though.

I used the same code and wiring on the Core. When I replace the Photon with the Core on the breadboard the buzer works on A0 and A1.

Do I have to put extra code to get it working now on A0/A1 on the Photon?

Cheers

UPDATE

Found: https://github.com/spark/firmware/issues/483

As for my understanding tone will work on the following pins D0, D1, D2, D3, A4, A5, WKP, RX and TX

@zembrowski, thanks for the link to the bug. A4 to worked for me. A7 kind of worked but was really distorted.

These pins did not A0, A1, A5 or RX. I did not try D0, D1 or TX.

Can you please update the docs:
https://docs.particle.io/reference/firmware/photon/#tone-

The still state the full list of pins. I can also confirm that A6 is not working, which is a little unfortunate. Now I am using two pins to play waves and beeps.

I aslo switched from Core to Photon with a buzzer. A0 and A1 didn’t work. I switched to A4 and it did. Unless my Photon is faulty, the documentation of the tone() function seems incorrect.

@gdeflaux, you are correct about the documentation. The Photon analogWrite() docs say:

On the Photon and Electron, this function works on pins D0, D1, D2, D3, A4, A5, WKP, RX and TX with a caveat: PWM timer peripheral is duplicated on two pins (A5/D2) and (A4/D3) for 7 total independent PWM outputs. For example: PWM may be used on A5 while D2 is used as a GPIO, or D2 as a PWM while A5 is used as an analog input. However A5 and D2 cannot be used as independently controlled PWM outputs at the same time.

So unlike the Core, the Photon A0 and A1 don't support PWM and hence Tone. :smile:

I just pushed a change to the docs.

2 Likes

HI All
I'm not having any luck trying to play a tone from my Photon. I've got 2 typical piezo buzzers that work on an RPi and an Arduino, so I don't think it's them. I hooked them up to D0 and execute

tone(D0, 2000, 500);

and get nothing, same for D1. Is there something special that I am missing? In the end it read like D0 and D1 were in fact working pins for tone().

TIA

Did you set pinMode for D0 to output in your setup{}?

Yes, BUZZER_PIN is defined as D0,D1 (as appropriate)

pinMode(BUZZER_PIN,OUTPUT);

Well I decided to just reboot the effort and it’s working. I must’ve always had something missing at each stage, but when I just used D1 and all connections are correct, tone works as expected.

Sorry for the noise