Difference between revisions of "BakeBit - Buzzer"

From FriendlyELEC WiKi
Jump to: navigation, search
(Source Code)
Line 41: Line 41:
  
 
=== Source Code ===
 
=== 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
 
+
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]
  
 
=== Run Code Sample ===
 
=== Run Code Sample ===

Revision as of 09:27, 12 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
 
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 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 users press the button the buzzer will beep. When the button is released 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"