User Tools

Site Tools

한국어

comfilepi:controlling_the_piezo_buzzer:index

Controlling the Piezo Buzzer

The ComfilePi has a built-in Piezo 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:

sudo systemctl stop touch-beep.service
sudo systemctl disable touch-beep.service

To enable the touch beep, simply execute the following commands:

sudo systemctl enable touch-beep.service
sudo systemctl start touch-beep.service

Programming the Piezo Buzzer

The piezo buzzer is tied to GPIO pin 30. One way to program it is to use the pigpio library. The following examples illustrate how to use a programming language to play a beep.

C++ Example

#include <pigpiod_if2.h>
#include <thread>
 
using namespace std;
 
#define PIN 30
 
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;
}

Compile with:

g++ playbeep.cpp -lpigpiod_if2 -o playbeep

Python Example

import time
import pigpio
 
PIN = 30
 
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()
comfilepi/controlling_the_piezo_buzzer/index.txt · Last modified: 2021/06/24 11:36 by COMFILE Technology