nebrius / raspi-soft-pwm

Provides access to Software PWM on the Raspberry Pi from Node.js through pigpio
MIT License
5 stars 0 forks source link

Controlling a Buzzer #2

Open besrabasant opened 4 years ago

besrabasant commented 4 years ago

I have a module for controlling a buzzer.

var raspi = require("raspi");
var pwm = require("raspi-soft-pwm");

const BUZZER_GPIO_PIN_NO = "GPIO13";
const BUZZER_FREQUENCY = 500;
const BUZZER_DUTYCYCLE = 0.8;

function buzzerConstructor(frequency = BUZZER_FREQUENCY, dutyCycle = BUZZER_DUTYCYCLE) {
    var buzzer = null;

    raspi.init(() => {
        buzzer = new pwm.SoftPWM({pin: BUZZER_GPIO_PIN_NO, frequency: frequency});
        console.log("Buzzer initialized!");
    });

    const on = () => {
        if(buzzer){
            console.log('Buzzer on');
            buzzer.write(dutyCycle);
        }
        else
            console.log("Buzzer isn't initialized, please wait until it finishes initialization");
    };
    const off = () => {
        if(buzzer) {
            console.log('Buzzer off');
            buzzer.write(0);
        }
        else
            console.log("Buzzer isn't initialized, please wait until it finishes initialization");
    }

    return {
        frequency: frequency,
        dutyCycle: dutyCycle,
        soundFor: (duration = 3000) => {
            on();

            setTimeout(() => {
                off();
            }, duration);
        },
        on: on,
        off: off
    }
}

var buzzer = new buzzerConstructor();

module.exports = buzzer;

And then I use the Modules somewhere else

var b = require('./controllers/Buzzer');

b.soundFor();

But the buzzer does not make a sound.

How do I control a buzzer?

nebrius commented 4 years ago

Which buzzer are you using, and what sort of PWM signal does it expect?

I haven't worked with buzzers before, but off the cuff I'd guess that we'd want a PWM with a 50% duty cycle, not 80%.

besrabasant commented 4 years ago

@nebrius A passive buzzer.

nebrius commented 4 years ago

@nebrius A passive buzzer.

Can you give me more information, such as a link to the product? IIRC there are multiple types of passive buzzers.