Push Button Buzzer

Push button Buzzer

In this example we will demonstrate how to use a Button Sensor and how to create a sound with a Buzzer. This example is a good demonstration of the using the digital pins for both input and output. A digital signal is received from the Button Sensor and is used to activate and deactivate the Buzzer.
grovebuttonp_01

Required Components

  • SODAQ Mbili Board
  • 0.5W Solar Panel
  • 1aH Battery Pack
  • Grove – Buzzer
  • Grove – Button
  • 2x Grove Cable (any length)

Required Libraries

  • none

Hardware Setup

You should refer to both the board diagram and Grove sockets page for additional information.

  1. First, plug the Buzzer into the socket for the digital pins D4 D5.
  2. Then, plug the Button Sensor into the Grove socket for the digital pins D20 D21.
  3. Next, plug the 0.5W solar panel and the 1A LiPo battery into their respective sockets.

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the SODAQ Mbili board, and then unplug the USB cable from the computer when it has completed the upload.

Sketch Code

#define BUTTON_PIN 20 // Use digital pin 20 for the button
#define BUZZER_PIN 4 // Use digital pin 4 for the buzzer
int buttonState = 0; 

void setup()
{
        //Set the digital pin modes
        pinMode(BUZZER_PIN, OUTPUT);
        pinMode(BUTTON_PIN, INPUT);
}

void loop()
{
        //Read the current state of the button
        buttonState = digitalRead(BUTTON_PIN);
        if (buttonState == HIGH) 
        {
                //If the button is pressed, turn the buzzer on
                digitalWrite(BUZZER_PIN, HIGH);
        }
        else 
        {
                //If not, turn the buzzer off
                digitalWrite(BUZZER_PIN, LOW);
        }
}

When you press the button, you should hear the buzzer sound.

Sketch Code Breakdown

Globals

Here we specify the digital pins we are using for both the Button Sensor and the Buzzer. Additionally, we declare a global variable buttonState and initialise it to 0.

Note: If you use other Grove sockets for either the button or buzzer you must update these values to the first digital pin listed for that Grove socket.

#define BUTTON_PIN 20 //Use digital pin 20 for the button
#define BUZZER_PIN 4 //Use digital pin 4 for the buzzer
int buttonState = 0;

setup()

Here we set the pin mode for the two digital pins we are using. This is done through a call topinMode()В with the first parameter specifying the pin to be set and the second parameter specifying the mode for that pin. The Buzzer pin is set to OUTPUT mode while the Button pin is set to INPUT mode.

void setup()
{
        //Set the digital pin modes
        pinMode(BUZZER_PIN, OUTPUT);
        pinMode(BUTTON_PIN, INPUT);
}

loop()

Here we read the state of the Button Sensor by reading a value from the digital pin that it is connected to. This is done using the digitalRead() method which returns a value matching the built in constants of HIGH or LOW. We then modify the state of the output pin connected to the Buzzer using the digitalWrite() method, passing the value returned from the Button Sensor. An if/else conditional is used to determine whether to switch the Buzzer on or off.

void loop()
{
        //Read the current state of the button
        buttonState = digitalRead(BUTTON_PIN);
        if (buttonState == HIGH) 
        {
                //If the button is pressed, turn the buzzer on
                digitalWrite(BUZZER_PIN, HIGH);
        }
        else 
        {
                //If not, turn the buzzer off
                digitalWrite(BUZZER_PIN, LOW);
        }
}