Welcome to K230 Vision Module repository
3.Buzzer
buzzer
Introduction
In this section, we will learn how to use the code to control the buzzer on YAHBOOM K230
A buzzer is an electronic component that can make a buzzing sound.
It produces sound through electromagnetic or piezoelectric principles and is often used in various electronic devices, such as alarms, electronic toys, and electronic signal devices.
Quick Start
We open CanMV IDE and connect K230 to the computer.
Press Ctrl + N to create a new code and delete all automatically generated code content
Copy the following code and paste it into the IDE [Source Code/02.Basic/03.1_buzzer.py]
x
# 导入蜂鸣器库 (Import buzzer library)from ybUtils.YbBuzzer import YbBuzzer# 导入时间库 (Import time library)import time# 创建蜂鸣器实例 (Create buzzer instance)buzzer = YbBuzzer()# 示例1:短鸣一声 (Example 1: Short beep)buzzer.beep() # 使用默认参数发出蜂鸣声 (Make a beep with default parameters)# 等待3秒 (Wait for 3 seconds)time.sleep(3)# 示例2:自定义频率和持续时间 (Example 2: Custom frequency and duration)buzzer.on(2000, 50, 0.5) # 2000Hz,音量50%,持续0.5秒 (2000Hz, volume 50%, duration 0.5 seconds)# 等待3秒 (Wait for 3 seconds)time.sleep(3)# 示例3:警报声效果 (Example 3: Alarm sound effect)for i in range(3): # 循环3次 (Loop 3 times) buzzer.on(1000, 50, 0.1) # 1000Hz,音量50%,持续0.1秒 (1000Hz, volume 50%, duration 0.1 seconds) time.sleep(0.1) # 短暂停顿0.1秒 (Brief pause for 0.1 seconds)Click the Run button and you can hear different sounds coming from the K230 buzzer.
Play melody
The buzzer on the K230 module is a passive buzzer, which is connected to a PWM output channel of the K230.
We will explain PWM related content in detail in subsequent tutorials
We can make the buzzer emit sounds of different frequencies by modifying the PWM output value.
The following is the code for simulating playing notes with a passive buzzer [Source Code/02.Basic/03.2_twinkle_twinkle.py]
xxxxxxxxxx# Import buzzer libraryfrom ybUtils . YbBuzzer import YbBuzzer# Import time libraryimport time# Create buzzer instancebuzzer = YbBuzzer ()# Define note frequencies in HzC5 = 523 # 1 - Middle CD5 = 587 # 2 - Middle DE5 = 659 #3 - Middle EF5 = 698 # 4 - Middle FG5 = 784 # 5 - Middle GA5 = 880 # 6 - Middle AB5 = 988 # 7 - Middle B# Define note durationBEAT = 0.3 # Duration of one beat in seconds# Play melodydef play_twinkle (): """ Play the melody of Twinkle Twinkle Little Star (Play the melody of "Twinkle Twinkle Little Star") """ # Note sequence for "Twinkle Twinkle Little Star" notes = [ ( C5 , BEAT ), ( C5 , BEAT ), ( G5 , BEAT ), ( G5 , BEAT ), # 1 1 5 5 (Music score: Twinkle Twinkle Little Star) ( A5 , BEAT ), ( A5 , BEAT ), ( G5 , BEAT * 2 ), # 6 6 5- (Shiny) ( F5 , BEAT ), ( F5 , BEAT ), ( E5 , BEAT ), ( E5 , BEAT ), # 4 4 3 3 (all over the sky) ( D5 , BEAT ), ( D5 , BEAT ), ( C5 , BEAT * 2 ), # 2 2 1- (Little Star) ] # Iterate through the notes list and play for freq , duration in notes : # Play current note # Parameters: frequency, volume 50%, duration buzzer . on ( freq , 50 , duration ) # Brief pause between notes for clarity time . sleep ( 0.1 ) # Turn off the buzzer after playing buzzer . off ()#Program entry pointif __name__ == "__main__" : # Call function to play the melody play_twinkle ()
Buzzer Classification
We mentioned passive buzzers above. Here we will briefly introduce the classification of buzzers.
In terms of driving mode, we can divide buzzers into active and passive types.
An active buzzer is a buzzer with an internal oscillation circuit. It does not require an external drive circuit and can directly emit sound by simply inputting a DC power supply. The structure of an active buzzer is relatively simple and suitable for miniaturization. It is commonly used in electronic toys, alarms, and other occasions that have requirements on size and power consumption.
Passive Buzzer is a buzzer that does not contain an oscillation circuit and requires an external drive circuit to provide an AC signal to work. It is suitable for high-power and loud applications, such as car horns, industrial sirens, etc. The external drive circuit can be a single-chip microcomputer, a dedicated drive chip, etc., designed according to application requirements.
The advantage of using a passive buzzer is that richer tone and volume control can be achieved by adjusting the frequency and duty cycle of the external signal.