Buzzer Button Module

This documentation includes hardware info, installation guidelines and sample code for your hardware.

Overall Info

Turta Buzzer Button Module carries a large button and a piezo sounder. It's ideal for Internet button applications.

Illustration

Top
Bottom

Module Connections

  • Button Input: INT, Active Low.

  • Piezo Input: PWM.

  • POWER: +3.3V and GND.

Specifications

Module's technical specifications are as follows:

  • Button operating force: 4N.

  • Button travel: 0.9mm.

  • Button diameter: 4.3mm.

  • Piezo sounder recommended frequency range: 2 to 10kHz.

  • Piezo sounder sound pressure range: 65 to 85dB.

  • Piezo sounder peak dB frequency: 7.5kHz.

Prerequisites

To get maximum performance from your hardware, please make sure you'll meet the following requirements.

Hardware

Buzzer Button Module is an add-in board for Turta Modular System. You'll need a compatible host device for your module. The following devices support Buzzer Button Module:

  • Turta IoT Node - ESP32

Software

For ESP32 based systems, we're currently supporting Arduino core. Please make sure you've installed the following:

  • Arduino IDE.

  • ESP32 Hardware libraries for the Arduino IDE.

  • Python for the compilation of Arduino projects.

  • Drivers for your host device, e.g., USB Virtual Serial Port driver.

Installation

You'll need to install a library to make Arduino IDE support the sensor. This library includes helpers for the module and sample code for you to jumpstart your project.

Libraries

Please download the library archive from https://github.com/Turta-io/Modular

Then, copy its contents to your Arduino Libraries folder.

You can find the Arduino Libraries folder inside your Arduino sketch directory. It is generally located inside the My Documents folder.

Samples

The library comes with included samples. There is no need to download additional files for the examples.

Source Code

The hardware, its drivers and the sample code are open source. You can use them under the terms of the MIT license. Please feel free to customize and prototype them for your projects.

Arduino Library

Turta_BuzzerButton_Module library is responsible for configuring interrupt and PWM pins, reading button state and generating a frequency for the piezo sounder.

To use the library on Arduino IDE, add the following #include statement to the top of your sketch.

#include <Turta_BuzzerButton_Module.h>

Then, create an instance of the Turta_BuzzerButton_Module class.

Turta_BuzzerButton_Module bb

Now you're ready to access the library by calling the bb instance.

Initialization

To initialize the sensor, call the begin method.

begin()

This method configures the interrupt and PWM pins.

Basic Members

Read Button State

Returns the button pressed state.

bool readButton()
  • Parameters

    • None

  • Returns

    • Bool: Button state

Buzzer Tone

Plays a tone on the buzzer.

void buzzerTone(int frequency, short dutyCycle)
  • Parameters

    • Int: frequency

    • Short dutyCycle

  • Returns

    • None

Buzzer Stop

Stops the tone.

void buzzerStop()
  • Parameters

    • None

  • Returns

    • None

Buzzer Tone Period

Plays a tone on the buzzer for a duration.

void buzzerTonePeriod(int frequency, short dutyCycle, int durationMs)
  • Parameters

    • Int: frequency

    • Short: dutyCycle

    • Int: durationMs

  • Returns

    • None

Examples

You can open the example from Arduino IDE > File > Examples > Examples from Custom Libraries > Turta Buzzer Button Module. There is one example of this sensor.

Buzzer Button

This application demonstrates button reading and tone playing functions of the module.

To run the example:

  1. Open the IoT Node Buzzer Button sketch from the examples menu.

  2. Select Turta IoT Node from Tools > Board menu.

  3. Select your device's COM port from Tools > Port menu.

  4. Open Serial Monitor from Tools > Serial Monitor.

  5. Select 115.200 baud from the Serial Monitor's status bar.

  6. Upload the code to your device.

Sample Code

IoT_Node_Buzzer_Button.ino
#include <Turta_BuzzerButton_Module.h>
#define LED 13
// Global variables.
bool buttonPressed = false;
bool oldButtonState = false;
// Create Buzzer Button instance.
Turta_BuzzerButton_Module bb;
void setup() {
// Initialize onboard LED.
pinMode(LED, OUTPUT);
// Initialize Buzzer Button.
bb.begin();
// Configure serial port.
Serial.begin(115200);
delay(200);
}
void loop() {
// Read button state.
buttonPressed = bb.readButton();
// On button pressed / released.
if (oldButtonState != buttonPressed) {
// Set LED state.
digitalWrite(LED, buttonPressed ? HIGH : LOW);
// Write button state to the serial monitor.
Serial.println(buttonPressed ? "Button pressed." : "Button released.");
// Play tone if button is pressed.
if (buttonPressed) {
// Buzzer tone alternative 1:
// Play 5000Hz tone in 192/255 duty cycle for 100ms.
bb.buzzerTonePeriod(5000, 192, 100);
}
// Play tone if button is released.
else {
// Buzzer tone alternative 2:
// Play 2000Hz tone in 64/255 duty cycle until stopped.
bb.buzzerTone(2000, 64);
// Delay 100ms.
delay(100);
// Stop the buzzer.
bb.buzzerStop();
}
}
// Store last button state.
oldButtonState = buttonPressed;
// Delay 10ms.
delay(10);
}

Result

After the application is uploaded to the device, it generates different tones when the button is pressed and released. The application also writes the following output to the serial monitor:

Button pressed.
Button released.

The application runs forever until you clear it from memory.

Troubleshooting

If you're experiencing difficulties while working with your device, please try the following steps.

Problem: You're pressing the button, but the application does not recognize it. Cause: There are two press levels of the button. You probably stop pushing to it after the first level. Solution: Please push the button stronger.

Problem: The buzzer does not produce sound or produces incorrect tones. Cause: The PWM pin drives the buzzer uses PWM channel 15. You probably use this channel with another component. Solution: Please do not assign PWM channel 15 to any pin.