Ollie Smeenk
Published

Using a Button to Activate a Buzzer

In this tutorial, you learn how to create an alarm noise on a buzzer and how to activate it with a button.

Full instructions provided1,168
Using a Button to Activate a Buzzer

Things used in this project

Hardware components

0.5W Solar Panel
×1
1aH battery pack
×1
Grove - Buzzer
×1
Grove - Button
×1
Grove w/o Buckle Cable (any length)
×2
SODAQ Moja
×1

Story

Read more

Code

code.c

C/C++
code.c
const int buttonPin = 2; //set pin 2 for the buttonconst
int buzzerPin = 4; //set pin 4 for the buzzer
int buttonState = 0; //buzzer starts off
 
void setup()
{
 pinMode(buzzerPin, OUTPUT);
   //define the output and input in the  equation here
 pinMode(buttonPin, INPUT);
}
 
void loop()
{
 buttonState = digitalRead(buttonPin);
 
 if (buttonState == HIGH) {
            // turn buzzer on:
        digitalWrite(buzzerPin, HIGH);
 }
else {
           // turn buzzer off:
        digitalWrite(buzzerPin, LOW);
 }
}

Credits

Ollie Smeenk
8 projects • 6 followers
Business-oriented Tinkerer! Ready to engage in any project for- and not for profit!

Comments