Skip to content

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.

Required components:

  • SODAQ Moja
  • 0.5W Solar Panel
  • 1aH battery pack
  • Grove – Buzzer
  • Grove – Button

First, plug the buzzer into the D4/D5 pin in the always on row. Then, plug the button int the D2/D3 pin in the always on row.

Next, plug the 0.5W solar panel and the 1A LiPo battery into the respective sockets.

Turn on the SODAQ board, compile and upload the following sketch from the arduino IDE onto the SODAQ Board, and then unplug the USB cable from the computer when it is working:

const int buttonPin = 2; //set pin 2 for the button const
int buzzerPin = 4; //set pin 4 for the buzzer
int buttonState = 0; //buzzer starts off

void setup()
 //define the output and input in the  equation here
 pinMode(buzzerPin, OUTPUT);
 pinMode(buttonPin, INPUT);

void void loop()
 buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH) {
           // turn buzzer on:
        digitalWrite(buzzerPin, HIGH);
else {
           // turn buzzer off:
        digitalWrite(buzzerPin, LOW);