passive buzzer/Playing it/06. The first beep
Playing it · 06 of 10

The first beep

Three wires to an ESP32-S3 and one call: tone(4, 2700, 200) plays a 200 ms beep at the buzzer's loudest pitch and returns at once. The sketch alternates it with a 440 Hz beep, so the difference the resonance makes is the first thing you hear.

Three wires

Three wires
VCC to
Buzzer gets
about 3.2 V
Its range
2.5 to 4.5 V
Loudness
about 4 dB less
GPIO 4 gives
2.5 mA
VCC on 3V3 gives the buzzer about 3.2 V, inside the 2.5 to 4.5 V its datasheet lists and near its rated 3.6 V. It is the quieter choice, by about 4 dB on a simple model, and the LED takes about 1.2 mA while lit. This book wires it this way.

GND to GND. SIGNAL to GPIO 4, the pin every TinkerBlock PWM book here uses on the ESP32-S3: it can make any frequency and does nothing else at boot. NC stays unconnected.

VCC is the one choice. This book wires it to 3V3, which gives the buzzer about 3.2 V, inside the 2.5 to 4.5 V its datasheet lists. 5V does no harm to your board, because VCC never reaches SIGNAL, and it is a little louder: 5 V is the voltage the maker measures loudness and current at, though it is slightly over the top of that listed range. The buzzer only draws current while it plays, up to 95 mA, so either rail copes.

Your boardVCC toSIGNAL to
ESP32-S33V3GPIO 4
ESP323V3GPIO 4
Arduino Uno5VD9
Raspberry Pi Pico3V3(OUT)GP15

The sketch

tone(BUZZER_PIN, 2700, 200) does three things. It starts a 2700 Hz square wave on the pin, arranges for it to stop 200 ms later, and returns immediately, while the note is still playing. A timer makes the wave, so your loop is free; the delay(1000) is only there to space the beeps.

When the 200 ms are up the pin is left LOW, the transistor off, and the coil without current. That is the state to finish in, every time.

What you should hear

A sharp beep, then a second later a lower, noticeably quieter one, repeating. The LED glows faintly with each. At 115200 the Serial Monitor prints:

beep at 2700 Hz
beep at 440 Hz
beep at 2700 Hz
beep at 440 Hz

The difference in loudness is the buzzer, not the sketch: both beeps are the same 50 % square wave, and 2700 Hz is the pitch this buzzer is built to be loudest at.

An earlier version of this block, with a round buzzer, beeping on an Arduino Uno. The board you have carries the square one; wiring and sketch are the same.

The code

tk37_first_beep.ino

No library. tone() makes the square wave on a timer, in the background, stops it after the length you give, and leaves the pin LOW. Change BUZZER_PIN if SIGNAL is on another pin.

// TK37 Passive Buzzer: the first beep, on an ESP32-S3.
//
// Wiring, TK37 header left to right (parts up, pins down):
//   GND    -> ESP32 GND
//   VCC    -> ESP32 3V3   (5V works too, and is louder)
//   NC     -> nothing
//   SIGNAL -> GPIO4
//
// Arduino IDE: Tools > Board > esp32 > ESP32S3 Dev Module,
// Tools > USB CDC On Boot > Enabled, then Tools > Port.
// No library needed. Serial Monitor at 115200.

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

void setup() {
  Serial.begin(115200);
}

void loop() {
  tone(BUZZER_PIN, 2700, 200);  // 2.7 kHz for 200 ms, then LOW
  Serial.println("beep at 2700 Hz");
  delay(1000);                  // tone() returns at once

  tone(BUZZER_PIN, 440, 200);   // A4: lower, and quieter
  Serial.println("beep at 440 Hz");
  delay(1000);
}

tone() returns as soon as the note starts, so the delay() after it is what keeps the beeps apart. The 440 Hz beep is quieter on purpose: it is far from the buzzer's 2.7 kHz resonance. On an Uno, set BUZZER_PIN to 9 and wire VCC to 5V; the rest is the same.

When it does not work

No sound and the LED never lights.

The transistor never switches, so check the three wires against the square pad: GND, VCC, NC, SIGNAL from the left. Then check BUZZER_PIN is the GPIO SIGNAL is actually on. With the wiring right the LED glows faintly during each beep.

The upload works but nothing is printed.

On an ESP32-S3 the sketch prints over the USB port only with Tools > USB CDC On Boot set to Enabled. Set it, upload again, and open the Serial Monitor at 115200. On a board with two USB sockets, use the one marked USB.

Both beeps sound the same loudness.

They should not: 440 Hz is more than 15 dB under the peak on the datasheet's curve. If they match, check that the two numbers in the sketch are 2700 and 440, and that nothing is pressed against the side of the buzzer, where its sound port is.

It beeps once and then holds a steady tone.

A tone() call without the third number, the length, plays until noTone() is called. Keep the 200 in every call, or add noTone(BUZZER_PIN) where the note should stop.

Where this goes next

A tune as a list of pitches and lengths, with a gap that keeps repeated notes apart.

A melody is notes and lengths

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

Community

Questions about this product

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

Ask a question ↗

Passive 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