Files
esp32-learning/activebuzzer_npn_lowside_switching/activebuzzer_npn_lowside_switching.ino

32 lines
696 B
Arduino
Raw Normal View History

2026-05-28 08:13:57 -04:00
#define PIN_BUZZER_SWITCH 14
#define PIN_BUTTON 21
#define PWM_CHANNEL 0
void setup() {
pinMode(PIN_BUZZER_SWITCH, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
// Tutorial had a bug here. Needs to be freq=1000, not freq=1.
ledcAttachChannel(PIN_BUZZER_SWITCH, 1000, 10, PWM_CHANNEL);
ledcWriteTone(PIN_BUZZER_SWITCH, 2000);
delay(300);
}
void alarm() {
float sine;
int tone;
for ( int i = 0; i < 360 ; i += 10 ) {
sine = sin(i * (PI / 180));
tone = 2000 + sine * 500;
ledcWriteTone(PIN_BUZZER_SWITCH, tone);
delay(50);
}
}
void loop() {
if ( digitalRead(PIN_BUTTON) == LOW ) {
alarm();
} else {
ledcWriteTone(PIN_BUZZER_SWITCH, 0);
}
}