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 |
Wiring Diagram
Schematic Diagram
Image is developed using Fritzing. Click to enlarge image
Breadboard Connections
ESP32 Code
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
- 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
See Also
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!