active buzzer/Driving it/06. The first beep
Driving it · 06 of 9

The first beep

Three wires, no library, and a sketch that beeps for a fifth of a second every second. VCC goes to a 5V pin on every board, SIGNAL to the pin this book uses throughout: GPIO 4 on an ESP32-S3 or ESP32, D9 on an Uno, GP15 on a Pico. The first two lines of setup() drive that pin LOW, because the board has no pull-down.

Three wires

Three wires
Your board
VCC to
5V
SIGNAL to
GPIO 4
Pin gives
2.6 mA
GND to GND, VCC to the board's 5V pin, SIGNAL to GPIO 4. With the board on USB, 5V carries the cable's 5 V, the buzzer's rated voltage. GPIO 4 is not a strapping pin and nothing pulls it HIGH at reset, so the buzzer stays quiet while the ESP32-S3 starts, and the pin gives only about 2.6 mA.

GND to GND. VCC to your board's 5V pin: 5V on an Uno or an ESP32 board, VBUS on a Pico. SIGNAL to the pin this book uses throughout. NC stays unconnected. Peel the paper seal off the buzzer.

The pins are the same as the XL LED and vibration motor books': GPIO 4 on an ESP32-S3 or ESP32, D9 on an Uno, GP15 on a Pico. None of them is pulled HIGH while its board starts, which on this block is the property that matters; quiet at boot shows what the others do.

The sketch

pinMode(BUZZER_PIN, OUTPUT) makes the pin drive SIGNAL, and the digitalWrite(BUZZER_PIN, LOW) straight after it makes sure it drives it LOW. Both come first in setup(), before Serial.begin, because until they run nothing on the board holds SIGNAL down.

In loop(), digitalWrite(BUZZER_PIN, HIGH) puts your board's logic voltage on the 1 kΩ, the transistor turns on and the buzzer sounds; LOW turns it off. There is no tone(): the note is made inside the buzzer.

What you should hear

A short beep every second, with the red LED lighting for each one. At 115200 the serial monitor prints beep each time.

The sketch compiles for an ESP32-S3 and an Uno. Here is the same block beeping on an Uno and on a Pico-format board, with its seal still on:

The TK36 on a LEGO baseplate, wired to an Arduino Uno by a four-wire cable, seal still on. The red LED lights with each beep.
The same block on a Pico-format board, seal still on, beeping with its LED.

The code

buzzer_first_beep.ino

No library. digitalWrite HIGH turns the transistor on and the buzzer sounds; LOW turns it off. Change BUZZER_PIN to the pin you wired SIGNAL to.

/*
  Active Buzzer - first beep                            TK36 / /p/tk36

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V: an Uno's 5V, the 5V pin of an ESP32 or ESP32-S3
              board on USB, a Pico's VBUS (3V3 works, quieter)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> GPIO 4 on an ESP32-S3 or ESP32, D9 on an Uno,
              GP15 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.

  Peel the paper seal off the buzzer first: it covers the sound hole.
*/

// The pin SIGNAL is wired to.
// Uno: 9. ESP32: 4. ESP32-S3: 4. Pico: 15.
const int BUZZER_PIN = 4;

void setup() {
  // First, before anything else: the board has no pull-down, so
  // SIGNAL is whatever this pin is until the sketch drives it.
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  Serial.begin(115200);
}

void loop() {
  digitalWrite(BUZZER_PIN, HIGH);   // transistor on: beep
  Serial.println("beep");
  delay(200);

  digitalWrite(BUZZER_PIN, LOW);    // off: silence
  delay(800);
}

The first two lines of setup() matter on this board. There is no pull-down on SIGNAL, so until the sketch drives the pin, the buzzer does whatever the pin does. The sketch compiles for an ESP32-S3 and an Uno.

When it does not work

The LED blinks but I hear nothing.

Then SIGNAL, VCC and the transistor are working and the fault is at the buzzer. Check its seal is the only thing over the sound hole, then look at its two leads under the board. The schematic notes that an active buzzer fitted backwards is silent.

Nothing happens, not even the LED.

Either VCC is missing or SIGNAL never goes HIGH. Count from the square pad: GND, VCC, NC, SIGNAL. Check that BUZZER_PIN names the pin SIGNAL is really on, and that GND goes to your board's GND.

It beeps before the sketch starts.

Your pin is HIGH or pulled up while the board boots, and with no pull-down on the block the buzzer follows it. GPIO 0 on an ESP32-S3 dev board does this. Use GPIO 4, or the pin this book gives for your board.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print.

Where this goes next

Why the pin you choose matters on a board with no pull-down.

Quiet at boot

Edit this page — content/books/active-buzzer/the-first-beep.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Active Buzzer

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum