Pages: [1]   Go Down
Author Topic: Simultaneous PWM to multiple output devices  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My project is a musical instrument that generates a tone when the divided voltage reading through a photo-resistive cell dips below a threshold. There are multiple sensors, each with its own speaker. The problem I am having is that when one tone initiates on one speaker, no other tone will play on the other speakers. I believe there should be no problem generating 2 or more distinct PWM signals to the speakers. I have looked at manipulating the PWM frequencies more directly, but I believe I am limited by the discrete set of divisors.

This is a stub of the code to illustrate the scheme, and perhaps highlight any causes of my problem:

void setup() {}
void loop() {
if (sensor1 < threshold1) tone(outputPin1, frequency1, duration1)
if (sensor2 < threshold2) tone(outputPin2, frequency2, duration2)

If I trigger the sensors simultaneously, the first tone to play will continue to do so until the trigger action is disengaged, while the other remains silent. My desire is to get any speaker to play at any time, regardless of other speakers playing.

Left Coast, CA (USA)
Offline Offline
Brattain Member
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe the tone library deals with this issue of different simultaneous outputs by assigning free timers with each instance of the class, so a mega board having more timers can play more tones at the same time then a uno board can. Or something like that.  smiley-wink


Topsham, Vermont USA
Offline Offline
Edison Member
Karma: 33
Posts: 1926
... in The Woods In Vermont
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You are not using Arduino PWM there, you are using the Tone library.

Try using Arduino PWM, by outputting using analogWrite to multiple pins...

Regards, Terry King  - Check great prices, devices and Arduino-related boards at
HOW-TO: http://ArduinoInfo.Info

Pages: [1]   Go Up
Jump to: