Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 7

Passive Buzzer

Play eight ascending notes through a passive buzzer.

Set sound frequency with tone() instead of a steady output.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • A passive sounder needs an alternating drive waveform; software frequency selects the audible pitch. Round two-lead sounder; appearance alone may not distinguish active from passive. Active and passive cans can look identical; use markings or a safe rated-voltage test.
  • A PN2222 NPN transistor uses base drive to control a larger collector current for switching or amplification. TO-92 body marked PN2222; confirm pinout from the supplied part. TO-92 outlines do not establish emitter/base/collector order; read the exact manufacturer suffix or verify with a transistor tester.
  • 330 Ω resistor provides a fixed opposition to current. Lower-current LED indication is a common use when its unverified power and tolerance limits fit the circuit. Small axial body with one wire at each end. Find the labeled value compartment and measure an isolated loose part with a meter; the shared inventory image is a form reference.
  • 1N4007 is a general-purpose silicon rectifier that conducts mainly from anode to cathode for steering or rectification. Axial black diode with a cathode band and 1N4007 marking. The band marks cathode; diode-test mode verifies forward behavior but not maximum ratings.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.
  • ?
    1 ×Regulated 5.0 V DC supply, at least 1 A, with output lead pair
    Unconfirmed · Availability and exact identity unconfirmed.

Assembly

  1. With all power disconnected, build the transistor stage; confirm PN2222 lead 1=E, 2=B, 3=C against its supplied datasheet and actual part marking.
  2. Orient the diode band toward buzzer positive; the buzzer’s marked positive lead goes to the 5 V source.
  3. Set the external supply to 5.0 V before connection; join its negative to Mega GND and keep its positive off the Mega 5 V pin.
  4. Connect USB and then the external supply.

The original omits the driver and separate supply. Use the corrected transistor/diode circuit and regulated 5.0 V supply rated at least 1 A. Share ground; never connect supply positive to Mega 5 V.

Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: D8Base resistor330Ω: input
Base resistor330Ω: outputPN2222: B / pin2
PN2222: E / pin1Mega: GND
PN2222: C / pin3Buzzer: negative (-)
External5V supply: 5V (+)Buzzer: positive (+)
1N4007: K / banded cathodeBuzzer: positive (+)
1N4007: A / unbanded anodeBuzzer: negative (-)
External5V supply: negative (-)Mega: GND
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
Code experiments
main.cpp
#include <Arduino.h>

const uint8_t BUZZER_PIN = 8;  // Signal goes to the transistor driver.
// This array holds eight note frequencies in hertz (cycles per second).
const unsigned int NOTES_HZ[] = {523, 587, 659, 698, 784, 880, 988, 1047};

// No special pin configuration is needed here; tone() configures the output.
void setup() {}

// Play each frequency, then pause before starting the tune again.
void loop() {
  // The index note goes from 0 to 7, selecting one element of NOTES_HZ.
  for (uint8_t note = 0; note < 8; ++note) {
    // tone() generates the chosen pitch for about 500 milliseconds.
    tone(BUZZER_PIN, NOTES_HZ[note], 500);
    // Wait one second between note starts, leaving space between notes.
    delay(1000);
  }
  // Rest two seconds after the last note.
  delay(2000);
}

Expected behavior

The notes523,587,659,698,784,880,988 and1047 Hz play for0.5 s each, one second apart, followed by an extra2 s pause.

Ode to Joy: Play Beethoven's melody at 120 BPM on D12, then pause for three seconds.

Experiments & Challenges

  • Reverse NOTES_HZ to play a descending scale.

Troubleshooting

  • Silent: check active versus passive buzzer identity, polarity, transistor E/B/C and diode direction.