Passive buzzer alarm on pin 9, through a transistor, on an Arduino Uno
A passive buzzer, switched by an NPN transistor from pin 9, wails like an alarm: the pitch rises and falls along a sine wave around 2 kHz. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $31. On Mokxi they're free.
What you'll learn
- Switching a load with a transistor
- Passive versus active buzzers
- Making a changing pitch with
tone - Using a sine wave to sweep a value
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A pin on the Arduino can give only a few milliamps, which is not enough for a buzzer. So pin 9 goes through a 1k resistor to the base of an NPN transistor. The transistor's emitter goes to ground, and the buzzer sits between 5V and the collector. The small signal from the pin switches the larger buzzer current.
A passive buzzer makes whatever pitch you send it. The sketch steps through 360 degrees, finds the sine of each one, and sets the tone to 2000 Hz plus 500 times that. The pitch swings between 1500 and 2500 Hz, so it wails like an alarm.
Parts list
5 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
Kits that can build it
These 6 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Transistor (NPN or PNP) pin E | Arduino Uno R3 pin GND |
| Resistor, 1k Ω pin 1 | Arduino Uno R3 pin 9 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 5V |
| Transistor (NPN or PNP) pin B | Resistor, 1k Ω pin 2 |
| Piezo speaker pin 2 | Transistor (NPN or PNP) pin C |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// An alarm: a passive buzzer whose pitch follows a sine wave.
//
// pin 9 -> 1k -> the NPN transistor's base; emitter -> GND
// 5V -> the buzzer's + leg; the buzzer's - leg -> the transistor's collector
//
// A board pin can only give a few milliamps, so it switches a transistor and
// the transistor carries the buzzer's current from 5 V.
//
// For each degree from 0 to 360, sin() gives a number from -1 to 1. The tone
// is 2000 Hz plus 500 times that, so it swings between 1500 and 2500 Hz.
const int BUZZER = 9;
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: alarm on the passive buzzer on pin 9");
}
void loop() {
for (int x = 0; x < 360; x++) {
float s = sin(x * (PI / 180.0));
tone(BUZZER, 2000 + (int)(s * 500));
delay(2);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 9600 and prints a short message. The buzzer pin needs no pin setup here, because tone sets up the pin itself.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: alarm on the passive buzzer on pin 9");
}loop sketch.ino
Counts x from 0 to 359. For each step it computes sin of that angle in radians, scales it by 500 and adds 2000, and passes that to tone(BUZZER, ...). A 2 ms delay between steps makes the pitch glide up and down.
void loop() {
for (int x = 0; x < 360; x++) {
float s = sin(x * (PI / 180.0));
tone(BUZZER, 2000 + (int)(s * 500));
delay(2);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- No sound at all This project
Check the transistor legs: emitter to GND, base through the 1k to pin 9, collector to the buzzer's second leg. - The buzzer is wired straight to pin 9 This project
Keep the transistor in between. The pin cannot supply enough current on its own. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong. - The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.