Skip to content

Limitations of multi buzzer configuration

Fabiano Riccardi edited this page May 15, 2022 · 1 revision

Nowadays, the most common hardware limitation may affect the number of simultaneous active buzzers you can use. This value will vary from board to board, and more specifically it may depend on the available number of timers, presence of PWM peripherals and interrupt management.

ESP8266

ESP8266 has 1 hardware timer available to end-user and no dedicated PWM peripheral, hence PWM is generated by the software. It supports PWM on multiple pins at the same time thanks to superb implementation of the timer interrupt routine. You can use every available pin on ESP8266. Note that if you use this library, the timer is completely dedicated to PWM generation (you can continue to use other libraries whose benefit Arduino tone() function), but you cannot use the timer for other tasks.

ESP32

ESP32 has 4 hardware timers and a PWM peripheral. Each timer can be used by PWM peripheral to emit only 1 square wave. Hence you are limited to use only 4 buzzers at the same time. With respect to ESP8266, this solution is completely hardware, hence PWM generation won't affect CPU speed. To support more than 4 buzzers at the same time (and to save timers for other purposes), I should resort to a software solution, like the one exploited in ESP8266. Please note that (till today) tone() function is not yet implemented by esp32-arduino-core, and this library use hal-ledc.

MelodyPlayer constructor takes 2 parameters: the pin and the PWM channel.

MelodyPlayer(PIN_NUM, CHANNEL_NUM);

If you want to use multiple buzzers at the same time, you must specify LEDC Channels feeded by different timers. The following table shows the mapping between LEDC Channels and timers (reference: ESP32-Arduino repository). For example, to use all 4 buzzers, you can choose the LEDC Channel 0, 2, 4, and 6.

LEDC Channel Group Channel Timer
0 0 0 0
1 0 1 0
2 0 2 1
3 0 3 1
4 0 4 2
5 0 5 2
6 0 6 3
7 0 7 3
8 1 0 0
9 1 1 0
10 1 2 1
11 1 3 1
12 1 4 2
13 1 5 2
14 1 6 3
15 1 7 3
Clone this wiki locally