1#!/usr/bin/env python
2#-------------------------------------------------------------------------------
3# qwiic_buzzer_ex2_buzz_frequency.py
4#
5# This example shows how to adjust the frequency of the buzzer.
6#
7# This example shows how to turn the buzzer on and off.
8# Much like the classic "blink LED sketch" this will buzz
9# the buzzer once every second.
10#-------------------------------------------------------------------------------
11# Written by SparkFun Electronics, January 2024
12#
13# This python library supports the SparkFun Electroncis Qwiic ecosystem
14#
15# More information on Qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19# https://www.sparkfun.com/products/24474
20#
21#===============================================================================
22# Copyright (c) 2023 SparkFun Electronics
23#
24# Permission is hereby granted, free of charge, to any person obtaining a copy
25# of this software and associated documentation files (the "Software"), to deal
26# in the Software without restriction, including without limitation the rights
27# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28# copies of the Software, and to permit persons to whom the Software is
29# furnished to do so, subject to the following conditions:
30#
31# The above copyright notice and this permission notice shall be included in all
32# copies or substantial portions of the Software.
33#
34# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40# SOFTWARE.
41#===============================================================================
42
43import qwiic_buzzer
44import sys
45import time
46
47def runExample():
48 print("\nQwiic Buzzer Example 2 - Buzz Frequency\n")
49
50 # Create instance of device
51 my_buzzer = qwiic_buzzer.QwiicBuzzer()
52
53 # Initialize the device
54 if my_buzzer.begin() == False:
55 print("The device isn't connected to the system. Please check your connection", \
56 file=sys.stderr)
57 return
58
59 print("\nQwiic Buzzer ready!")
60
61 # Loop forever
62 while True:
63 # Configure with desired settings
64 # Resonant frequency is 2.73KHz
65 my_buzzer.configure(2730)
66 my_buzzer.on()
67 time.sleep(0.1)
68 my_buzzer.off()
69 time.sleep(1)
70
71 my_buzzer.configure(1000) # set frequency to 1KHz
72 my_buzzer.on()
73 time.sleep(0.1)
74 my_buzzer.off()
75 time.sleep(1)
76
77if __name__ == '__main__':
78 try:
79 runExample()
80 except (KeyboardInterrupt, SystemExit) as exErr:
81 print("\nEnding Example")
82 sys.exit(0)