Free Shipping over ₹1199

ESP32-S3 Buzzer with Arduino IDE

Introduction

In this tutorial, we learn how to generate sound using a buzzer with the ESP32-S3 board. We manually create different tones by rapidly switching the buzzer on and off using code. This is a beginner-friendly way to understand how frequencies and delays affect sound output. By the end, you’ll know how to make simple beeps and tones with your ESP32.

Required Components

  • ESP32-S3 Board
  • Buzzer module
  • Jumper wires
  • Breadboard (optional)

Pinout

Circuit Diagram / Wiring

  • BUZZER VCC → 5V (ESP32-S3)
  • BUZZER GND → GND (ESP32-S3)
  • BUZZER SIGNAL → Pin 14 (ESP32-S3)

Code / Programming

/*
  Filename: pwm_buzzer_tone.ino
  Description: Play different tones using a buzzer connected to an ESP32-S3 via PWM
  Author: www.oceanlabz.in
  Modification Date: 1/4/2025
*/


int buzzerPin = 14;  // Connect buzzer to GPIO23

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  playTone(1000, 500);  // Play 1kHz for 0.5 seconds
  delay(500);
  playTone(1500, 500);  // Play 1.5kHz for 0.5 seconds
  delay(1000);          // Pause between tones
}

// Function to generate square wave tone manually
void playTone(int frequency, int duration) {
  long period = 1000000L / frequency;         // Microseconds per cycle
  long cycles = (long)frequency * duration / 1000;  // Total cycles to play

  for (long i = 0; i < cycles; i++) {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(period / 2);
  }
}

Explanation

  • Generates sound using a buzzer by toggling GPIO14 at specific frequencies, simulating tones.
  • The playTone() function creates square waves manually without using PWM functions.
  • Suitable for ESP32-S3 and all board versions, ensuring compatibility without ledcWriteTone().

Troubleshooting

  • Ensure the buzzer is connected properly (positive to GPIO14, negative to GND).
  • Use an active buzzer for reliable tone output with digital signals.
  • Select the correct ESP32-S3 board and COM port in the Arduino IDE before uploading.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Need Help?