What a basic example to control an output? (for a PWM buzzer on ESP32)

I have used the poll sketch to blink the ESP32 on board led. That was a step in the direction I want to increase. I’d like to start next with a buzzer that i can turn on and off using a Blynk button.
[believe it or not, this is to wake a family member lol!]

In the poll sketch i placed button on a digital pin. Can it remain so and activate an ESP32 output?

Unrelated to Blynk I am having probs compiling the arduino function " tone()". It is not declared but is called and so is returning an error. Is there an accompanying preprocessor directive for it?
I have TDK piezo tranducer from adafruit.

Thanks in advance, best regards!

I knew I answered this question earlier… but looks like you changed account name or deleted something as the system automatically removed your past topic…

Anyhow… This is not a Blynk related issue as the ESP32 Arduino/core does not currently support the analogWrite() (PWM) command yet. (and thus the failed compiling of that tone() function).

There are some complicated workarounds for servo control and such, but not easy to implement in your case as you need a PWM signal for your piezo element to make buzzy noises.

You can try to find a buzzer (basicly a piezo element with built-in oscillator) that will make noise by simply supplying power, then turning it ON and OFF with a GPIO and transistor or optocoupler switch.

1 Like

Not sure I understand your question… but assuming you want a Button Widget to act as a switch instead as a momentary button?? If so, then just change that setting in the widget settings itself (from PUSH to SWITCH).



I am looking for a minor minor in 3 hours. I have sound by connecting the esp32 pin 12 and 3.3v pin to the piezo. int freq = 3000, channel = 0, resolution = 10.

I have tried most of the day working with poll but it does switch. the moment the chip connects to blynk the sound starts. I’d like the blynk to cut the sound on or off. Thanks in advance.

I managed to to get this to work. I have the originall poll sketch modified with an if statement. first i placed the if statement in the checkpin debounce function. (what IS debounce?) Due to the low frequency of the piezo and the stutter of the button it worked once but then didnt work again. So I retooled the sketch and put the sound making if statement in the loop. that was more consistent. so now when i press the button both the led and buzzer tone go on. i release the button led dim sound ends. I cant upload the sketch because I am new.

Your first port of call for any subject you are not familiar with should be a Google search.

i like bing

So you know what a search engine is then?

1 Like