Built-in project · Arduino Uno R3

Transistor doorbell: button on 12, buzzer on 9, on an Arduino Uno

Hold the push button on pin 12 (10k pull-up) and an active buzzer sounds, switched by an NPN transistor whose base is driven from pin 9. The whole build, an Arduino Uno and 5 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.

Transistor doorbell: button on 12, buzzer on 9 · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Switching a load with a transistor
  • Why a base resistor is needed
  • A pull-up resistor on a button input
  • Active-low buttons
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

A board pin can only give a few milliamps, which is not enough for a buzzer. So pin 9 drives the base of a transistor through a 1k resistor, and the transistor carries the buzzer's current from 5V to ground.

The pushbutton on pin 12 goes to ground, with a 10k resistor pulling the pin up to 5V. Pressed, the pin reads LOW. The sketch turns that around: pin 9 goes HIGH while the button is held, and the buzzer sounds.

There is no delay and no state. The loop just copies the button onto the buzzer as fast as it can.

Kits that can build it

These 5 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 pinConnects to
Pushbutton pin 1aArduino Uno R3 pin GND
Transistor (NPN or PNP) pin EArduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin 12
Resistor, 10k Ω pin 1Arduino Uno R3 pin 12
Resistor, 1k Ω pin 1Arduino Uno R3 pin 9
Resistor, 10k Ω pin 2Arduino Uno R3 pin 5V
Buzzer pin 1Arduino Uno R3 pin 5V
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
Buzzer pin 2Transistor (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.

sketch.ino · Arduino Uno R3
// A doorbell: hold the button and the buzzer sounds.
//
//   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.
//   10k from 5V -> pin 12; the push button from pin 12 -> GND
//
// Hold the button on the canvas.

const int BUTTON = 12;
const int BUZZER = 9;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: hold the button on pin 12 to ring");
}

void loop() {
  digitalWrite(BUZZER, digitalRead(BUTTON) == LOW ? HIGH : LOW);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets BUTTON as an input and BUZZER as an output, then starts serial at 9600 baud. The pull-up is the external 10k resistor, so the button pin uses plain INPUT.

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: hold the button on pin 12 to ring");
}

loop sketch.ino

One line does it all: if digitalRead(BUTTON) is LOW, the button is pressed, so it writes HIGH to the buzzer pin. Otherwise it writes LOW.

void loop() {
  digitalWrite(BUZZER, digitalRead(BUTTON) == LOW ? HIGH : LOW);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The buzzer is always on or never on This project
    Check the transistor's pins: emitter to GND, collector to the buzzer's minus leg, base to pin 9 through the 1k resistor.
  • The button reads randomly This project
    The 10k resistor must connect pin 12 to 5V. Without it the pin floats.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • 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.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.