0

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

UPDATE: IT seems that there is a problem using pin 11 and 13 together but if I use a different pwm pin like pin 5. It's okay. I'm guessing that the timers used at pin 11 is connected to tone() but not sure...

I’ve encountered a weird problem with an Arduino with this simple task. It involves just a buzzer and LED and a very simple custom library that I made.

I wanted the buzzer to sweep back and forth with a specified frequency range (f1, f2), the led will also display a specific brightness (0, 50) based on the frequency being played.

The buzzer works perfectly on its own, and also the LED, but when you put them together, it malfunctions.

A more detailed explanation can be found here: https://tilarduino.wordpress.com/2015/11/27/weird-arduino-problem-buzzer-led-brightness/ And i've included all the files i've used here: https://gist.github.com/mithi/09ae419a3c7eba285221

Using this code below, commenting out line 19 with line 18 uncommented-out will make the buzzer work, and commenting out line 19 with line 18 back in will make the led work as expected. But together, the code malfunctions. buzzer is attached to pin 13 and led is attached to pin 11.

#include "SimplestLibrary.h"

Sweeper sweeper; 
Buzzer buzzer; 
AnalogLED led;

int f1 = 3000;
int f2 = 4500;
int f = 0;

void setup() {
    sweeper.New(f1, f2, BACKANDFORTH); 
    buzzer.New(13);
    led.New(11);
}

void loop() {
    f = sweeper.Update(1); 
    buzzer.Play(f, 4, 2);
    led.NewBrightness(map(f, f1, f2, 0, 50));
    delay(1);
}
2

From Arduino.cc:

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

Simple solution is to use a PWM that isn't an output from Timer2 :

Timer      OCxA pin    OCxB pin
  0           6        5
  1           9        10
  2           11       3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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