Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

šŸ‘‰ Ready to explore and create with us? Click [here] and join today!

3.1 Make the Buzzer Beep!ļƒ

In this lesson, we will learn how to make a buzzer beep using the Raspberry Pi Pico 2. A buzzer is a digital output device, just like an LED, and itā€™s very simple to control. Weā€™ll use an active buzzer for this project, which generates sound when it receives a signal.

What is an Active Buzzer?

An active buzzer has an internal oscillator that makes it easier to use. You only need to send a signal to the buzzer to make it beepā€”no complex frequency control is required. This is different from a passive buzzer, which requires an external signal to generate sound.

img_buzzer

What Youā€™ll Need

In this project, we need the following components.

Itā€™s definitely convenient to buy a whole kit, hereā€™s the link:

Name

ITEMS IN THIS KIT

LINK

Newton Lab Kit

450+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Transistor

1(S8050)

BUY

6

Resistor

1(1KĪ©)

BUY

7

Active Buzzer

1

Circuit Diagram

sch_buzzer

In this circuit, the buzzer is powered through a transistor (S8050 NPN). The transistor amplifies the current, making the buzzer sound louder than if it were connected directly to the Pico.

Hereā€™s what happens:

  • GP15 outputs a high signal to control the transistor.

  • When the transistor is activated, it allows current to flow through the buzzer, making it beep.

A 1kĪ© resistor is used to limit the current to protect the transistor.

Wiring Diagram

Make sure you are using the active buzzer. You can tell itā€™s the correct one by looking for the sealed back (as opposed to the exposed PCB, which is a passive buzzer).

img_buzzer

wiring_beep

Writing the Code

Note

  • You can open the file 3.1_beep.ino from newton-lab-kit/arduino/3.1_beep.

  • Or copy this code into Arduino IDE.

  • Select the Raspberry Pi Pico 2 board and the correct port, then click ā€œUploadā€.

const int buzzerPin = 15;  // GPIO pin connected to the transistor base

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  digitalWrite(buzzerPin, HIGH);  // Turn the buzzer on
  delay(1000);                    // Wait for 1 second
  digitalWrite(buzzerPin, LOW);   // Turn the buzzer off
  delay(1000);                    // Wait for 1 second
}

After uploading the code: The buzzer should beep for 1 second, then stay silent for 1 second, and repeat this pattern continuously. If you do not hear the buzzer, check the wiring to ensure all connections are correct. Make sure you are using an active buzzer.

Understanding the Code

  1. Defining the Buzzer Pin:

    Assigns buzzerPin to GPIO 15, which controls the transistor and thus the buzzer.

    const int buzzerPin = 15;  // GPIO pin connected to the transistor base
    
  2. Setting Up the Pin Mode:

    Configures buzzerPin as an output.

    void setup() {
      pinMode(buzzerPin, OUTPUT);
    }
    
  3. Controlling the Buzzer: The loop() function repeats this process indefinitely, making the buzzer beep every second.

    • digitalWrite(buzzerPin, HIGH): Sets buzzerPin HIGH, turning on the transistor, which allows current to flow through the buzzer, making it beep.

    • delay(1000): Pauses the program for 1000 milliseconds (1 second).

    • digitalWrite(buzzerPin, LOW): Sets buzzerPin LOW, turning off the transistor, stopping the current flow, and silencing the buzzer.

    void loop() {
      digitalWrite(buzzerPin, HIGH);  // Turn the buzzer on
      delay(1000);                    // Wait for 1 second
      digitalWrite(buzzerPin, LOW);   // Turn the buzzer off
      delay(1000);                    // Wait for 1 second
    }
    

Further Exploration

  • Varying the Beep Duration:

    • Modify the delay() values to change how long the buzzer stays on and off.

    • Experiment with shorter or longer durations.

  • Creating Patterns:

    • Create more complex patterns by adjusting the timing in the loop() function.

    • For example, create an SOS signal in Morse code.

  • Using a Passive Buzzer:

    • Try using a passive buzzer and the tone() function to generate different frequencies.

    • Note that the wiring and code will be different for a passive buzzer.

Conclusion

In this lesson, youā€™ve learned how to make an active buzzer beep using the Raspberry Pi Pico and a transistor. By controlling the transistor with a GPIO pin, you can safely switch the buzzer on and off without overloading the Picoā€™s GPIO pins. This basic concept can be expanded upon to create more complex sounds or to use buzzers in alarms, notifications, and interactive projects.