Difference between revisions of "BakeBit - Buzzer"

From FriendlyELEC WiKi
Jump to: navigation, search
(Observation)
 
(16 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
==Introduction==
 
==Introduction==
 
[[File:BakeBit - Buzzer.jpg|thumb|Buzzer]]
 
[[File:BakeBit - Buzzer.jpg|thumb|Buzzer]]
* BakeBit - Buzzer是一个无源蜂鸣器模块,无源蜂鸣器内部有音圈和钼片,输入2.7KHz的PWM信号时,PWM信号通过绕在支架上的线包在支架的芯柱上产生一交变的磁通,交变的磁通和磁环恒定磁通进行叠加,使钼片以给定的PWM信号频率振动并配合共振腔发声。
+
* The BakeBit-Buzzer is a passive buzzer. To drive this buzzer an oscillating source generating 2.7K square waves is needed.
*通过调整输入PWM信号的频率即可使无源蜂鸣器发出频率不同的声音。
+
* PWM input signals with different frequencies generate different sounds.
* 广泛应用于计算机、报警器、电子玩具、汽车电子设备、定时器等电子产品中作发声器件。
+
* It can be used in electronic devices that need sound generators.
  
==特性==
+
==Hardware Spec==
* 使用标准的2.0mm 4 Pin BakeBit接口
+
* Standard 2.0mm pitch 4 Pin BakeBit interface
* PWM信号输入
+
* PWM input
* PCB尺寸(mm):20x24
+
* PCB dimension(mm): 20 x 24
[[File:BakeBit - Buzzerpcb.png|frameless|300px|无源蜂鸣器PCB]]
+
[[File:BakeBit - Buzzerpcb.png|frameless|300px| Buzzer PCB]]
  
* 引脚说明:
+
* Pin Description:
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|名称 || 描述
+
|Pin || Description
 
|-
 
|-
|GND    || 电源5V
+
|GND    || Supply Voltage 5V
 
|-
 
|-
|5V    ||
+
|5V    || Gound
 
|-  
 
|-  
|NC    ||
+
|NC    || Not Connected
 
|-  
 
|-  
|SIG    || 输入,接PWM
+
|SIG    || PWM Input
 
|}
 
|}
== 示例程序:Button And Buzzer ==
 
  
本示例需要配合[[BakeBit - Button]]使用。
+
== Code Sample: Button And Buzzer ==
  
=== 硬件连接 ===
+
A [[BakeBit - Button]] module is needed in this test case.
简单的将 蜂鸣器 模块插入 D3接口,将 按键 插入 D4 接口,如下面这样:
+
 
 +
=== Hardware Setup ===
 +
Connect the buzzer module to the NanoHat Hub's D3 and the button module to the NanoHat Hub's D4:
  
 
::{| class="wikitable"
 
::{| class="wikitable"
Line 39: Line 40:
 
|}
 
|}
  
=== 示例源代码 ===
+
=== Source Code ===
 
+
 
<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
 +
old_button_status = -1
 +
pinMode(buzzer_pin,"OUTPUT") # Assign mode for buzzer as output
 +
pinMode(button,"INPUT") # Assign mode for Button as input
  
bakebit.pinMode(led,"OUTPUT")
+
buzzer_on = False
light = 0
+
old_buzzer_on = not buzzer_on
 +
button_pressed = False
  
 
while True:
 
while True:
    try:
+
try:
        # Read distance value from Ultrasonic
+
while True:
distance = bakebit.ultrasonicRead(ultrasonic_ranger)
+
button_status = digitalRead(button) #Read the Button status
        print(distance)
+
# print button_status
if distance > 0:
+
if old_button_status < 0:  
if distance<10:  
+
break
if light == 0:
+
print("\ton")
+
bakebit.digitalWrite(led,1)
+
light = 1
+
else:
+
if light == 1:
+
print("\toff")
+
bakebit.digitalWrite(led,0)
+
light = 0
+
time.sleep(.2)
+
  
    except KeyboardInterrupt:
+
if button_status != old_button_status:
bakebit.digitalWrite(led,0)
+
break
        break
+
  
    except TypeError:
+
time.sleep(0.2)
        print ("Error")
+
old_button_status = button_status
    except IOError:
+
 
        print ("Error")
+
if button_status == 0:
 +
button_pressed = True
 +
else:
 +
if button_pressed:
 +
buzzer_on = not buzzer_on
 +
button_pressed = False
 +
 
 +
if old_buzzer_on != buzzer_on:
 +
old_buzzer_on = buzzer_on
 +
if buzzer_on:
 +
analogWrite(buzzer_pin,127)
 +
print "Buzzing"
 +
else:
 +
analogWrite(buzzer_pin,0)
 +
print "\tOff"
 +
 +
time.sleep(0.2)
 +
except KeyboardInterrupt: # Stop the buzzer before stopping
 +
digitalWrite(buzzer_pin,0)
 +
break
 +
except (IOError,TypeError) as e:
 +
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]
  
=== 运行示例 ===
+
=== Run Code Sample ===
 
   
 
   
假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br />
+
Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to install the BakeBit package.<br />
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Button_And_Buzzer.py:
+
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Button_And_Buzzer.py" program:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
cd ~/BakeBit/Software/Python
 
cd ~/BakeBit/Software/Python
Line 93: Line 105:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== 运行结果 ===
+
=== Observation ===
  
当按下按键时,蜂鸣器会响起,松开按键时,蜂鸣器停止。
+
When the button is clicked the buzzer will beep and if it is clicked again the buzzer will be silent.
  
==相关资料==
+
==Resources==
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/5/5b/04-SCHEMATIC_Buzzer.pdf BakeBit - Buzzer.pdf])
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/5/5b/04-SCHEMATIC_Buzzer.pdf BakeBit - Buzzer.pdf])
*[BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
+
*[BakeBit Github Project Page](https://github.com/friendlyarm/BakeBit)
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)
+
*[BakeBit Starter Kit User's Manual](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_en.pdf)
 +
 
 +
==Update Log==
 +
===December-11-2016===
 +
* Released English version
 +
 
 +
===Jan-19-2017===
 +
* Renamed "NEO-Hub" to "NanoHat-Hub"
 +
 
 +
===Jan-20-2017===
 +
* Renamed "NanoHat-Hub" to "NanoHat Hub"

Latest revision as of 07:45, 17 April 2017

查看中文

1 Introduction

Buzzer
  • The BakeBit-Buzzer is a passive buzzer. To drive this buzzer an oscillating source generating 2.7K square waves is needed.
  • PWM input signals with different frequencies generate different sounds.
  • It can be used in electronic devices that need sound generators.

2 Hardware Spec

  • Standard 2.0mm pitch 4 Pin BakeBit interface
  • PWM input
  • PCB dimension(mm): 20 x 24

Buzzer PCB

  • Pin Description:
Pin Description
GND Supply Voltage 5V
5V Gound
NC Not Connected
SIG PWM Input

3 Code Sample: Button And Buzzer

A BakeBit - Button module is needed in this test case.

3.1 Hardware Setup

Connect the buzzer module to the NanoHat Hub's D3 and the button module to the NanoHat Hub's D4:

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

3.2 Source Code

import time
from bakebit import *
import math
 
buzzer_pin = 3		#Port for buzzer
button = 4		#Port for Button
old_button_status = -1
pinMode(buzzer_pin,"OUTPUT")	# Assign mode for buzzer as output
pinMode(button,"INPUT")		# Assign mode for Button as input
 
buzzer_on = False
old_buzzer_on = not buzzer_on
button_pressed = False
 
while True:
	try:
		while True:
			button_status = digitalRead(button)	#Read the Button status
			# print button_status
			if old_button_status < 0: 
				break
 
			if button_status != old_button_status:
				break
 
		time.sleep(0.2)
		old_button_status = button_status
 
		if button_status == 0:
			button_pressed = True
		else:
			if button_pressed:
				buzzer_on = not buzzer_on
			button_pressed = False 
 
		if old_buzzer_on != buzzer_on:
			old_buzzer_on = buzzer_on
			if buzzer_on:
				analogWrite(buzzer_pin,127)
				print "Buzzing"
			else:
				analogWrite(buzzer_pin,0)
				print "\tOff"			
 
		time.sleep(0.2)			
	except KeyboardInterrupt:	# Stop the buzzer before stopping
		digitalWrite(buzzer_pin,0)
		break
	except (IOError,TypeError) as e:
		print("Error")

Github

3.3 Run Code Sample

Before you run the code sample you need to follow the steps in bakebit tutorial to install the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Button_And_Buzzer.py" program:

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

3.4 Observation

When the button is clicked the buzzer will beep and if it is clicked again the buzzer will be silent.

4 Resources

5 Update Log

5.1 December-11-2016

  • Released English version

5.2 Jan-19-2017

  • Renamed "NEO-Hub" to "NanoHat-Hub"

5.3 Jan-20-2017

  • Renamed "NanoHat-Hub" to "NanoHat Hub"