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 6

Active buzzer

Play active-buzzer beeps with shortening intervals, followed by a five-second sound.

Switch a self-oscillating buzzer on and off.

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.
  • An active buzzer contains an oscillator and makes a tone when supplied with its rated DC drive. Round sounder with two leads and often a polarity mark. 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.

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. Connect USB.

The original connects the buzzer directly to GPIO. Use the corrected PN2222 driver, 330 ? base resistor and diode; band toward buzzer positive.

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

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
Code experimentsBuild once, try another program with the same wiring.
main.cpp
#include <Arduino.h>

// The Mega sends a control signal to a transistor driver; the buzzer is not powered directly from the pin.
const uint8_t BUZZER_PIN = 12;  // Drives the added transistor, not the load directly.

// Tell the Mega that the buzzer-control pin is an OUTPUT.
void setup() {
  pinMode(BUZZER_PIN, OUTPUT);
}

// Repeat the beep pattern forever.
void loop() {
  // Count 20 beeps, from beep 0 through beep 19.
  for (int beep = 0; beep < 20; ++beep) {
    // The ?: operator chooses a duration: first 5 at 500 ms, next 5 at 300 ms, last 10 at 100 ms.
    const int duration = beep < 5 ? 500 : (beep < 10 ? 300 : 100);
    // HIGH drives the transistor input and activates the buzzer.
    digitalWrite(BUZZER_PIN, HIGH);
    delay(duration);
    // LOW switches the buzzer off between beeps.
    digitalWrite(BUZZER_PIN, LOW);
    delay(duration);
  }
  // After the sequence, leave the buzzer on for five seconds.
  digitalWrite(BUZZER_PIN, HIGH);
  // The next loop starts with the output still HIGH; there is no separate silent gap here.
  delay(5000);
}

Expected behavior

Five 500 ms beeps, five 300 ms beeps and ten 100 ms beeps, then a 5 s HIGH hold; the repeating loop adds no extra LOW gap before the next beep.

Ode to Joy: Run Ode to Joy's note sequence at 120 BPM on D12, then pause for three seconds.

Experiments & Challenges

  • Change the three beep durations.

Troubleshooting

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