0

I'd like to play an asynch 'beep' sound (2.5kHz, pin 8) on an Arduino Mega with several other modules connected. I write them here, just for the record: SDCard reader and RFID reader through SPI bus, RTC through I2C, GSM module through hardware Serial 1. And I use PWM on two other pins (11,12) for controlling LEDs brightness.

It works well, however, sometimes (1 of 10) the beep frequency is about half-tone higher.

The code itself is good, because I use a constant in the tone() function call for specifying the frequency.

Does anybody have an idea what can influence the tone's PWM frequency such way, maybe on hardware level?

1
  • 3
    Post your code and schematic. Without that info your question is unanswerable. How do you know beep is half-tone higher?
    – user31481
    Commented Feb 14, 2018 at 15:42

2 Answers 2

1

As far as I know, tone() implements pin toggling in software (using timer interrupt), so it's possible that the frequency is lower when there's a lot of activity and higher when the CPU is mostly idle.

Try calling your beeping code alone inside an otherwise empty sketch and with no external hardware, and check out whether the sound is higher than you expect.

-1

Half-tone, that's a lot of change. I would expect a lowering of frequency if anything from other resources being active. An increase in frequency suggests perhaps something is changing the internal counter values that is used for the toggling. 2.5KHz is 400microseconds, toggling every 200us. You might consider using Arduino "blink without delay" to monitor the passing microseconds and toggling based on that. I've created up to 20KHz signal that way, but not with anything else running.

Your Answer

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

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