Difference between revisions of "BakeBit - Buzzer/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(示例源代码)
(示例源代码)
Line 41: Line 41:
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
import bakebit
 
 
import time
 
import time
# Connect the BakeBit Ultrasonic Ranger to digital port D4
+
from bakebit import *
# SIG,NC,VCC,GND
+
import math
ultrasonic_ranger = 4
+
  
# Connect the BakeBit LED to digital port D3
+
buzzer_pin = 3 #Port for buzzer
led = 3                                                                    
+
button = 4 #Port for Button
 
+
bakebit.pinMode(led,"OUTPUT")
+
light = 0
+
  
 +
pinMode(buzzer_pin,"OUTPUT") # Assign mode for buzzer as output
 +
pinMode(button,"INPUT") # Assign mode for Button as input
 
while True:
 
while True:
    try:
+
try:
        # Read distance value from Ultrasonic
+
button_status= digitalRead(button) #Read the Button status
distance = bakebit.ultrasonicRead(ultrasonic_ranger)
+
if button_status: #If the Button is in HIGH position, run the program
        print(distance)
+
analogWrite(buzzer_pin,0)
if distance > 0:
+
print "\tOff"
if distance<10:  
+
else: #If Button is in Off position, print "Off" on the screen
if light == 0:
+
analogWrite(buzzer_pin,127)
print("\ton")
+
print "Buzzing"
bakebit.digitalWrite(led,1)
+
except KeyboardInterrupt: # Stop the buzzer before stopping
light = 1
+
digitalWrite(buzzer_pin,0)
else:
+
break
if light == 1:
+
except (IOError,TypeError) as e:
print("\toff")
+
print("Error")
bakebit.digitalWrite(led,0)
+
light = 0
+
time.sleep(.2)
+
 
+
    except KeyboardInterrupt:
+
bakebit.digitalWrite(led,0)
+
        break
+
 
+
    except TypeError:
+
        print ("Error")
+
    except IOError:
+
        print ("Error")
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Button_And_Buzzer.py.py Github]
+
[https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Button_And_Buzzer.py Github]
  
 
=== 运行示例 ===
 
=== 运行示例 ===

Revision as of 09:28, 12 April 2017

English

1 介绍

Buzzer
  • BakeBit - Buzzer是一个无源蜂鸣器模块,无源蜂鸣器内部有音圈和钼片,输入2.7KHz的PWM信号时,PWM信号通过绕在支架上的线包在支架的芯柱上产生一交变的磁通,交变的磁通和磁环恒定磁通进行叠加,使钼片以给定的PWM信号频率振动并配合共振腔发声。
  • 通过调整输入PWM信号的频率即可使无源蜂鸣器发出频率不同的声音。
  • 广泛应用于计算机、报警器、电子玩具、汽车电子设备、定时器等电子产品中作发声器件。

2 特性

  • 使用标准的2.0mm 4 Pin BakeBit接口
  • PWM信号输入
  • PCB尺寸(mm):20x24

无源蜂鸣器PCB

  • 引脚说明:
名称 描述
GND 电源5V
5V
NC
SIG 输入,接PWM

3 示例程序:Button And Buzzer

本示例需要配合BakeBit - Button使用。

3.1 硬件连接

简单的将 蜂鸣器 模块插入 D3接口,将 按键 插入 D4 接口,如下面这样:

Button And Buzzer-1.jpg Button And Buzzer-2.jpg

3.2 示例源代码

import time
from bakebit import *
import math
 
buzzer_pin = 3		#Port for buzzer
button = 4		#Port for Button
 
pinMode(buzzer_pin,"OUTPUT")	# Assign mode for buzzer as output
pinMode(button,"INPUT")		# Assign mode for Button as input
while True:
	try:
		button_status= digitalRead(button)	#Read the Button status
		if button_status:	#If the Button is in HIGH position, run the program
			analogWrite(buzzer_pin,0)						
			print "\tOff"			
		else:		#If Button is in Off position, print "Off" on the screen
			analogWrite(buzzer_pin,127)
			print "Buzzing"			
	except KeyboardInterrupt:	# Stop the buzzer before stopping
		digitalWrite(buzzer_pin,0)
		break
	except (IOError,TypeError) as e:
print("Error")

Github

3.3 运行示例

假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Button_And_Buzzer.py:

cd ~/BakeBit/Software/Python
sudo python bakebit_prj_Button_And_Buzzer.py

3.4 运行结果

当按下按键时,蜂鸣器会响起,松开按键时,蜂鸣器停止。

4 相关资料