This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
comfilepi:controlling_the_piezo_buzzer:index [2022/12/01 11:46] COMFILE Technology [Python Example] |
comfilepi:controlling_the_piezo_buzzer:index [2023/06/26 08:42] COMFILE Technology [Python Example] |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Controlling the Electro-magnetic Buzzer ====== | ||
+ | |||
+ | The ComfilePi has a built-in electro-magnetic buzzer. By default, it beeps when the screen is touched. | ||
+ | |||
+ | ===== Enabling/Disabling the Touch Beep ===== | ||
+ | |||
+ | To disable the touch beep, simply execute the following commands: | ||
+ | <code> | ||
+ | sudo systemctl stop touch-beep.service | ||
+ | sudo systemctl disable touch-beep.service | ||
+ | </code> | ||
+ | |||
+ | |||
+ | To enable the touch beep, simply execute the following commands: | ||
+ | <code> | ||
+ | sudo systemctl enable touch-beep.service | ||
+ | sudo systemctl start touch-beep.service | ||
+ | </code> | ||
+ | |||
+ | ===== Programming the Electro-magnetic Buzzer ===== | ||
+ | |||
+ | The electro-magnetic buzzer is tied to GPIO pin 30 on the CPi-A and CPi-B panel PCs and GPIO pin 27 on the CPi-C panel PCs. One way to program it is to use the [[http://abyz.co.uk/rpi/pigpio/|pigpio library]]. The following examples illustrate how to use a programming language to play a beep. | ||
+ | |||
+ | ==== C++ Example ==== | ||
+ | |||
+ | <code C++> | ||
+ | #include <pigpiod_if2.h> | ||
+ | #include <thread> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | #define PIN 30 // CPi-A/B/S | ||
+ | //#define PIN 27 // CPi-C | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | auto instance = pigpio_start(NULL, NULL); | ||
+ | |||
+ | // We actually can't achieve 2700Hz due to the sampling | ||
+ | // rate, but it will do the best it can | ||
+ | set_PWM_frequency(instance, PIN, 2700); | ||
+ | |||
+ | // 128/255 = 50% duty | ||
+ | set_PWM_dutycycle(instance, PIN, 128); | ||
+ | |||
+ | // play beep for 100 milliseconds | ||
+ | this_thread::sleep_for(chrono::milliseconds(100)); | ||
+ | |||
+ | // turn off beep | ||
+ | set_PWM_dutycycle(instance, PIN, 0); | ||
+ | |||
+ | pigpio_stop(instance); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Compile with: | ||
+ | <code> | ||
+ | g++ playbeep.cpp -lpigpiod_if2 -o playbeep | ||
+ | </code> | ||
+ | ==== Python Example ==== | ||
+ | |||
+ | <code python> | ||
+ | import time | ||
+ | import pigpio | ||
+ | |||
+ | PIN = 30 # CPi-A/B/S | ||
+ | # PIN = 27 # CPi-C | ||
+ | |||
+ | pi = pigpio.pi() | ||
+ | |||
+ | # We actually can't achieve 2700Hz due to the sampling | ||
+ | # rate, but it will do the best it can | ||
+ | pi.set_PWM_frequency(PIN, 2700) | ||
+ | |||
+ | # 128/255 = 50% duty | ||
+ | pi.set_PWM_dutycycle(PIN, 128) | ||
+ | |||
+ | # play beep for 100 milliseconds | ||
+ | time.sleep(0.100) | ||
+ | |||
+ | #turn off beep | ||
+ | pi.set_PWM_dutycycle(PIN, 0) | ||
+ | |||
+ | pi.stop() | ||
+ | |||
+ | </code> | ||
+ | |||
+ | [[comfilepi:index|ComfilePi - Industrial Raspberry Pi Panel PC]] | ||
+ | |||