Go Down

Topic: ESP32 passive peizo buzzer (Read 154 times) previous topic - next topic

Val42

I want to drive a passive peizo buzzer from an ESP32.  I have found schematics on how to drive one of these from a 5v Arduino board, but not for the ESP32 that runs at 3.3v.  I don't want to burn out my ESP32 so I'm looking for a schematic on how to connect it.

Can I connect it directly?  Do I need a resistor and/or transistor?

DVDdoug

It should be safe but if you're paranoid put a ~200 Ohm resistor in series.

Val42

Okay, I hooked it up then used a minimal program to generate a tone:


Code: [Select]
const int duration = 500;
const int middle_c = 440;


void setup() {
 
}
 
void loop()
{
  tone(13, middle_c, duration);
  delay(duration);
}



This code does not compile when the board is set to Heltec_WIFI_Kit_32 (ESP-32) but does when set to Arduino/Genuino UNO.  I can understand that setting the board has several implicit included header files, but why doesn't the ESP-32 have support for audio output?  How do I include it?

Grumpy_Mike

#3
Dec 06, 2018, 08:52 am Last Edit: Dec 06, 2018, 08:55 am by Grumpy_Mike
The tone function uses PWM to generate the signal. PWM is generated by hardware timers in the processor. The arrangement, hardware address and function of these timers are very different in the two processors, that is why tone will not work on your processor.

Fortunately the software tells you this before you even try to use it, in the form of a compiler error.

Quote
How do I include it?
You add extra code to the tone library, a task I think that is beyond you with your current state of knowlage. Without looking in detail at the processor you have, I am not even sure if this is possible.

Val42

#4
Dec 06, 2018, 07:36 pm Last Edit: Dec 06, 2018, 07:39 pm by Val42
Quote
You add extra code to the tone library, a task I think that is beyond you with your current state of knowlage. Without looking in detail at the processor you have, I am not even sure if this is possible.
There is no way to know a person's level of knowledge on these forums, so I don't have any problem with you stating this with what you currently know about me.  I'm in no way being sarcastic or snarky.  There is so much of communication that occurs non-verbally when people talk in person, so that is why I stated this.  I am speaking (writing) in good faith, and assume that you are doing so as well.

Let me give you some more information about myself.  I am a professional programmer who has been programming for a few decades.  I have programmed from assembly language to 4-GL.  I'm currently programming in Java.  I know software, but I have only dabbled in hardware.  I am new to Arduino.  The questions I ask are to find out the proper way to do things, and to not damage the hardware that I do have.

If it is possible to do this with the ESP-32, via PWM (pulse width modulation) or otherwise, I will just need to be pointed to the proper documentation and I can probably do it.  I may have to ask some further questions, but maybe not.  I have been searching for this information myself, but I haven't yet found the type of documentation that I need to generate tones on the ESP-32.

Using just the delay() function I have got my ESP-32 to emit tones using the piezo buzzer.  This relies on the granularity of the delay() function, which is 1 ms.  If there is a finer resolution timer then I could generate more frequencies.  PWM would be better, but I've at least got a tone out of it.  This was my goal for now, though I'll want to do better later.

Code: [Select]
const int buzzer = 13;  // Buzzer pin


void setup() {
  pinMode(buzzer, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED on (HIGH is the voltage level)
}
 
void loop()
{
  for(int i = 250; i > 0 ; --i)
  {
    digitalWrite(buzzer, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1);
    digitalWrite(buzzer, LOW);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1);
  }

  delay(500);
}


And looking at the pin-out diagram of the ESP-32, I don't see any pins for PWM.  I see the pins labelled for PWM on the Arduino UNO.  It appears I have to live with the simple tones I'm generating now, unless I want to add an audio shield.

Grumpy_Mike

Well the cue I picked up on was:-
Quote
I have found schematics on how to drive one of these
Now you don't "find" a schematic if you know anything about hardware. Hence my assumption you were a beginner, sorry.

Without knowing the details of the exact buzzer you use it is hard to tell if you need a transistor to drive it or if you can just connect it to a pin. The important parameter is the capacitance of the buzzer. Then you can work out the current it will draw at the highest frequency you want to use.

The way to do it in software would be to have an interrupt being triggered at a fast rate say in the region of 40KHz and use the ISR to count and toggle a pin every N interrupts. By changing the value of N you can change the frequency and make it work like a tone function.

This requires to look at the data sheet for the processor and investigate any hardware timers in it. In the absence of an internal timer you can use an external NE555 timer chip to feed pulses to an interrupt pin and so achieve a tone function that can work in an asynchronous manner so as not to be blocking code.

As there is not libiary implementing a PWM function if you write your ISR as a libiary function you would be doing a great service to the community.

Val42

#6
Dec 06, 2018, 10:23 pm Last Edit: Dec 06, 2018, 10:28 pm by Val42
Quote
Now you don't "find" a schematic if you know anything about hardware. Hence my assumption you were a beginner, sorry.
No problem.  In these forums, we really don't know each other or each other's abilities.  I dabble in hardware, but I've been dabbling since high school electronics class.  Since I don't regularly do hardware I'll say that I know enough to be dangerous, so I compensate and become cautious; I ask questions when I'm not pretty sure.

DVDdoug suggested above:

Quote
It should be safe but if you're paranoid put a ~200 Ohm resistor in series.
It is working now, but I don't know about the long term issues with this.  I know that over-driving an LED works for a while, but it will burn out faster that way.  Adding a 220Ω resistor about halves the volume.  I'll try a lower value from my parts box, because I have been dabbling for a while.

I have also been searching, in the meantime, and discovered delayMicroseconds() from a tutorial on options to do sound.  It gives me finer control of the frequency.  Thanks for the idea of making a library to use ISRs to toggle pins.  I'll look into that.  I may have to go back to assembly to be able to do that, and I'll look into how to do that too.

And why the moniker of Grumpy_Mike?  I don't find you grumpy.  So far you've been helpful to me.

Val42

Okay, I used Ohm's Law to calculate the needed resistance.

The maximum drive current from any GPIO pin on the ESP-32 is 12 ma.

R = V/I, but I'll spell them out; Resistance = Voltage/Current = 3.3 v/12 ma = 275Ω

I can't find a part number on the piezo buzzer, but I've seen places that use 8Ω.  220Ω resistor with the piezo is a little low.  That value makes the buzzer much quieter.

I don't want to burn out a pin so I'll go with a transistor.  This discussion has a schematic that I'll adapt to 3.3v; I can look up the datasheet on the NPN transistors that I have.  BTW, this site gives 47Ω for the piezo.  I'll find the part number from where I ordered and find the actual value.

What is this about the capacitance of the buzzer?  Do you mean resistance?


Grumpy_Mike

#8
Dec 07, 2018, 09:58 am Last Edit: Dec 07, 2018, 10:01 am by Grumpy_Mike
Quote
What is this about the capacitance of the buzzer?  Do you mean resistance?
No.
While the resistance of a piezo is very high, it has capacitance. When you feed AC through a capacitor you get a current which depends on the frequency and the capacitance value. It is a lot more complex than this as you can see here because it also alters the phase of the signal and is a property of both capacitors and inductors.

This means that the capacitor has what you can think of as a frequency dependant resistance which is known as a reactance this can be calculated as:-
1/( 2* Pi * F * C )

Where F is the frequency in Hz and C is the capacitance value in Farads.
This can be used to calculate the current draw from the pin.

It is a bit more complex than that because a discharged capacitor looks like a short circuit for a tiny instance but for the small capacitance of this device other things like the output impedance of the output pin come into play so don't worry about that.

If you are using a transistor to drive it then I wouldn't bother with a seriese resistor at all, apart from the one in the base.

MarkT

I can't find a part number on the piezo buzzer, but I've seen places that use 8Ω.  220Ω resistor with the piezo is a little low.  That value makes the buzzer much quieter.

You may be confusing a speaker with a piezo buzzer?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up