Lesson 10 Active Buzzer

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

A buzzer is a great tool in your experiments whenever you want to make sounds.

Newly AddedComponents

Principle

As a type of electronic buzzer with an integrated structure, buzzers, which are supplied by DC power, are widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronic devices, telephones, timers and other electronic products or voice devices. Buzzers can be categorized as active and passive ones (see the following picture). Turn the buzzer so that its pins are facing up, and the buzzers with a green circuit board is a passive buzzer, while the one enclosed with a black tape is an active one.

The difference between an active buzzer and a passive buzzer:

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not tweet if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.

In this experiment, we use an active buzzer.

Schematic Diagram

When Pin11 is input into high voltage, the transistor will be switched on, and the collector will output low level. When there is a level difference between the two pins of the buzzer, the buzzer is ringing. When Pin11 inputs low power level, the transistor is cut off and the collector is at high level, here, both ends of the buzzer are at high level, the buzzer will be silent.

Build the Circuit

  • For C Language Users

Command 

1.Go to the folder of the code.

cd /home/pi/electronic-kit/for-raspberry-pi/c/Lesson_10_ActiveBuzzer

2. Compile the code.

gcc 10_ActiveBuzzer.c -lwiringPi 

3. Run the executable file.

sudo ./a.out

Now, you may hear the buzzer beep.

Code

1.import RPi.GPIO as GPIO  
2.import time  
3.  
4.BeepPin = 17  
5.BtnPin = 27  
6.  
7.def setup():  
8.    GPIO.setmode(GPIO.BCM)  
9.    GPIO.setup(BtnPin, GPIO.IN)  
10.    GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.LOW)  
11.      
12.def main():  
13.    while True:  
14.        if GPIO.input(BtnPin) == 0:  
15.            #Buzzer off  
16.            print ('Buzzer Off')  
17.            GPIO.output(BeepPin, GPIO.LOW)  
18.            time.sleep(0.1)  
19.        if GPIO.input(BtnPin) == 1:  
20.            #Buzzer on  
21.            print ('Buzzer On')  
22.            GPIO.output(BeepPin, GPIO.HIGH)  
23.            time.sleep(0.1)  
24.  
25.def destroy():  
26.    # Turn off buzzer  
27.    GPIO.output(BeepPin, GPIO.LOW)  
28.    # Release resource  
29.    GPIO.cleanup()      
30.  
31.# If run this script directly, do:  
32.if __name__ == '__main__':  
33.    setup()  
34.    try:  
35.        main()  
36.    # When 'Ctrl+C' is pressed, the child program   
37.    # destroy() will be  executed.  
38.    except KeyboardInterrupt:  
39.        destroy()  

Code Explanation

12.   pinMode(BeepPin, OUTPUT);

Set the pin connected to the buzzer to OUTPUT mode.

16.        digitalWrite(BeepPin, HIGH);

When BeepPin is at high level, the base pin(b pin) of the connected transistor inputs high level and the collector pin(c pin) output low level. That is, when the cathode of the buzzer is at low level and the anode of the buzzer is connected to a 5V high level, the buzzer sounds.

19.   digitalWrite(BeepPin, LOW);

The BeepPin is connected to the transistor and then to the cathode of the buzzer. When BeepPin is low level, the base pin (b pin) of the connected transistor inputs low level, then the collector pin(c pin) outputs high level; that is, when the level at both ends of the connected buzzer is high, the buzzer is silent.

  • For Python Language Users

Command

1.Go to the folder of the code.

cd /home/pi/electronic-kit/for-raspberry-pi/python

2. Run the code.

sudo python3 10_ActiveBuzzer.py

Now, you should hear the buzzer beep.

Code

1.import RPi.GPIO as GPIO  
2.import time  
3.  
4.BeepPin = 17  
5.  
6.def setup():  
7.    GPIO.setmode(GPIO.BCM)  
8.    GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.LOW)  
9.  
10.def main():  
11.    while True:  
12.        # Buzzer on (Beep)  
13.        GPIO.output(BeepPin, GPIO.HIGH)  
14.        time.sleep(0.1)  
15.        # Buzzer off  
16.        GPIO.output(BeepPin, GPIO.LOW)  
17.        time.sleep(0.1)  
18.  
19.def destroy():  
20.    # Turn off buzzer  
21.    GPIO.output(BeepPin, GPIO.LOW)  
22.    # Release resource  
23.    GPIO.cleanup()      
24.  
25.# If run this script directly, do:  
26.if __name__ == '__main__':  
27.    setup()  
28.    try:  
29.        main()  
30.    # When 'Ctrl+C' is pressed, the child program   
31.    # destroy() will be  executed.  
32.    except KeyboardInterrupt:  
33.        destroy()  

8.       GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.LOW)

Initialize the pin connected to the buzzer to output mode and set it to the default low level.

13.    GPIO.output(BeepPin, GPIO.HIGH)

The BeepPin is connected to the transistor and then to the cathode of the buzzer. When BeepPin is at high level, the base pin(b pin) of the connected transistor inputs high level, then the collector pin(c pin) outputs low level; that is, when the cathode of the buzzer is at low level and the anode of the buzzer is connected to a 5V high level, the buzzer sounds.

16.        GPIO.output(BeepPin, GPIO.LOW)

When BeepPin is at low level, the base pin(b pin) of the connected transistor inputs low level, then the collector pin(c pin) outputs high level; that is, when the level at both ends of the connected buzzer is high, the buzzer is silent.