ESP32 - Button - Buzzer

In this tutorial we will learn how simple and easy it is to Play Melody on Piezo Buzzer or Speaker using the Arduino tone () function.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
3×push button
1×Buzzer
1×1k ohm resistor
1×Breadboard
5×Jumper Wires

About Button This Project

ESP32 - Button

Please review our lessons about Button.

Tone() function

Before we can understand how a tone () works we should know how a Piezo buzzer works. We might have learnt about Piezo crystals in our school, it is nothing but a crystal which converts mechanical vibrations into electricity or vice versa. Here we apply a variable current (frequency) for which the crystal vibrates thus producing sound. Hence in order to make the Piezo buzzer to make some noise we have to make the Piezo electric crystal to vibrate, the pitch and tone of noise depends on how fast the crystal vibrates. Hence the tone and pitch can be controlled by varying the frequency of the current.

Okay, so how do we get a variable frequency from ESP32? This is where the ledcWriteNote () function comes in. The ledcWriteNote () can generate a particular frequency on a specific pin. The time duration can also be mentioned if required. The syntax for ledcWriteNote () is

ledcAttachPin(pin, channel) 
ledcWriteNote(channed, frequency, resolution)         

pin: the pin on which to generate the tone frequency: the frequency of the tone in hertz - unsigned int

Playing Musical Notes

Now, we know how to produce some noise using the arduino ledcWriteNote () function. But, how do we know what kind of tone will be generated for each frequency?

Arduino have given us a note table which equates each frequency to a specific musical note type. This note table was originally written by Brett Hagman, on whose work the tone() command was based. We will use this note table to play our themes. If you are someone familiar with sheet music you should be able to make some sense of this table, for others like me these are just another block of code.

Wiring Diagram

Schematic Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

Breadboard Connections

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Copy

int BUZZER_PIN = 13;
int BUZZER_CHANNEL = 0;
int b1,b2,b3;


void setup() {
  pinMode(5,INPUT_PULLUP);
  pinMode(18,INPUT_PULLUP);
  pinMode(19,INPUT_PULLUP);
  ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL);
}
void loop() {
  b1 = digitalRead(5);
  b2 = digitalRead(18);
  b3 = digitalRead(19);

  if (b1== LOW)
  {
      ledcWriteNote(BUZZER_CHANNEL, (note_t)NOTE_D, 8);
      delay(10);
  }
  else if (b2== LOW)
  {
      ledcWriteNote(BUZZER_CHANNEL, (note_t)NOTE_A, 8);
      delay(10);
  }
  else if (b3== LOW)
  {
    ledcWriteNote(BUZZER_CHANNEL, (note_t)NOTE_G, 8);
      delay(10);
  }
  else
  {
      ledcWrite(0, 0);
      delay(10);
  }
  
}              
           

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • Press and keep pressing the button several seconds
  • See the changes you made

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!