PROJECT Nº 3: Control the buzzer

Learn to create a program to control the sound of the buzzer by doing these three exercises.

EXERCISES INDEX:

  • Exercise 3.1: Turn the buzzer on and off by using the button
  • Exercise 3.2: Turn on the buzzer and two LEDs by using the potentiometer
  • Exercise 3.3: Control the entire system by using the potentiometer

Exercise 3.1: Turn the buzzer on and off by using the button

Create a program so that, when pressing the button, the buzzer with make a sound and when you stop pressing the button the buzzer stops making the sound. 

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 20 min.

MATERIALS:

  • 1 Buzzer
  • 1 Button
  • 1 USB – Micro USB cable
  • Computer

The Mini Lab will have to be built according to the instructions manual.

What is a buzzer?

A buzzer is an electroacoustic transducer that makes a continuous or intermittent sound, usually treble. It is used as a signaling mechanism o warning and can be applied to multiple systems, such as cars and household appliances, including wake up alarms. 

CONNECTIONS:

  1. Connect the button to the digital port 4 of the Build&Code 4in1 board.
  2. Connect the buzzer to the digital port 5 of the Build&Code 4in1 board.

PROGRAMMING CODE:

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code 

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    int valuepuls = 0, pinpuls = 4; // BUTTON VARIABLE AND PIN 
    int pinBuzzer = 5; // BUZZER PIN
    
    void setup() {
      // put your setup code here, to run once:  
    //BUTTON CONFIGURATION
    pinMode (pinpuls, INPUT);
    }
    void loop() {
      // put your main code here, to run repeatedly:
    // WHEN PRESSING THE BUTTON, THE BUZZER WILL MAKE A SOUND
    valuepuls = digitalRead (pinpuls); 
    if (valuepuls == HIGH) // BUTTON NOT PRESSED
    {
      noTone(pinBuzzer);
    }
    else
    {
      tone(pinBuzzer, 600); // tone (BUZZER PIN, FREQUENCY);
      delay (100);  // DURATION OF THE TONE
      tone(pinBuzzer, 294); 
      delay (100);  
      tone(pinBuzzer, 494); 
      delay (100);  
    }
    }
    
    
    
    3.	Configure and upload the code, following the indications on the Mini Lab First Steps guide.
    4.	Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code 

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware
    • Software
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE:

When pressing the button, the buzzer will make a sound and when you stop pressing the buzzer, the button will stop making the sound.

 

Exercise 3.2: Turn on the buzzer and two LEDs by using the potentiometer

Learn to create a program to control how long are the buzzer and two LEDs on and off by using the buzzer.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 green LED
  • 1 red LED
  • 1 Potentiometer
  • 1 Buzzer
  • 1 USB – Micro USB cable
  • Computer

The Mini Lab will have to be built according to the instructions manual.

CONNECTIONS:

  1. Connect the buzzer to the digital port 5 of the Build&Code 4in1 board.
  2. Connect the potentiometer to the analog port A1 of the Build&Code 4in1 board.
  3. Connect the LEDs to the digital ports 10 and 3 of the Build&Code 4in1 board.

PROGRAMMING CODE:

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code 

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    int led10 = 10, led3 = 3, brightness; // // PIN AND BRIGHTNESS VARIABLE FOR LED 10
    int pot = 0, pinpot = A1; //POTENCIOMETER PINS VARIABLES 
    int pinBuzzer = 5; // BUZZER PIN 
    
    void setup() { 
      // put your setup code here, to run once: 
    // LEDs 10 AND 3 CONFIGURATION
    pinMode ( led10, OUTPUT); 
    pinMode ( led3, OUTPUT); 
    
    } 
    
    void loop() { 
      // put your main code here, to run repeatedly: 
      pot = analogRead (pinpot); // POTENTIOMETER VALUE READING
      digitalWrite (led10, HIGH); // LED 10 = ON 
      digitalWrite (led3, LOW); // LED 3 = OFF 
      tone (pinBuzzer, 600); // BUZZER NOTE 1 
      delay ((pot)); // STANDY BY TIME ACCORDING TO THE READING OF THE POTENTIOMETER
      digitalWrite (led10, LOW); // LED 10 = OFF 
      digitalWrite (led3, HIGH); // LED 3 = ON 
      tone (pinBuzzer, 294); // BUZZER NOTE 2
      delay ((pot)); // STANDY BY TIME ACCORDING TO THE READING OF THE POTENTIOMETER
    }
    
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware

    • Software

  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE:

When rotating the potentiometer, the LEDs and the buzzer will turn on. The buzzer will make a different sound for each LED, with an interval determined by the rotation of the potentiometer.

 

Exercise 3.3: Control the entire system by using the potentiometer

This exercise is similar to exercise 3.2 but now, when pressing the button, you will activate the entire system. The buzzer will make a sound when the green LED turns on and a different sound when the red LED turns on, with an interval determined by the potentiometer. When you stop pressing the button, the LEDs and the buzzer will turn off.

DIFFICULTY LEVEL: Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 green LED
  • 1 red LED
  • 1 Buzzer
  • 1 Button
  • 1 Potentiometer
  • 1 USB – Micro USB cable
  • Computer

The Mini Lab will have to be built according to the instructions manual.

CONNECTIONS:

  1. Connect the button to the digital port 4 of the Build&Code 4in1 board.
  2. Connect the buzzer to the digital port 5 of the Build&Code 4in1 board.
  3. Connect the potentiometer to the analog port A1 of the Build&Code 4in1 board.
  4. Connect the LEDs to the digital ports 10 and 3 of the Build&Code 4in1 board.

PROGRAMMING CODE

You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.

Arduino Code 

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    int led10 = 10, led3 = 3, brightness; // PINs AND LED 10 AND 3 BRIGHTNESS VARIABLES 
    int pot = 0, pinpot = A1; // POTENTIOMETER PIN AND VARIABLE
    int pinBuzzer = 5; // BUZZER PIN 
    int valuepuls = 0, pinpuls = 4; // BUTTON PIN AND VARIABLE 
    void setup() { // put your setup code here, to run once: 
    
      // LEDs 10 AND 3 CONFIGURATION
    pinMode ( led10, OUTPUT); 
    pinMode ( led3, OUTPUT); 
    // BUTTON CONFIGURATION 
    pinMode ( pinpuls, INPUT); 
    
    } 
    
    void loop() { 
      // put your main code here, to run repeatedly: 
      pot = analogRead (pinpot); // POTENTIOMETER VALUES READING 
      // IF WE PRESS THE BUTTON THE LEDS WILL BLINK AND THE BUZZER WILL MAKE A SOUND. WITH THE POTENTIOMETER YOU WILL REGULATE THE BLINKING SPEED
    valuepuls = digitalRead (pinpuls); 
    if ( valuepuls == HIGH) // BUTTON NOT PRESSED 
    {
      noTone(pinBuzzer); // THE BUZZER DOESN´T MAKE A SOUND 
      digitalWrite (led3, LOW); // LED 3 -> OFF 
      digitalWrite (led10, LOW); // LED 10 -> OFF 
    }
    else
    {
        digitalWrite (led10, HIGH); // LED 10 = ON 
        digitalWrite (led3, LOW); // LED 3 = OFF 
        tone (pinBuzzer, 600); // BUZZER = NOTE 1 
        delay ((pot)); // STANDY BY TIME ACCORDING TO THE POTENTIOMETER READING 
        digitalWrite (led10, LOW); // LED 10 = OFF 
        digitalWrite (led3, HIGH); // LED 3 = ON 
        tone (pinBuzzer, 294); // BUZZER = NOTE 1 
        delay ((pot)); // STANDY BY TIME ACCORDING TO THE POTENTIOMETER READING
      }
    }
    
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Code for the visual programming software by blocks compatible

  1. Download and install the program.
  2. Open the software and copy the following code. Use the following image as a guide:
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

Bitbloq code

  1. Download Bitbloq and install the Web2board app.
  2. Open the software and copy the following code.
    • Hardware

    • Software
  3. Configure and upload the code, following the indications on the Mini Lab First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE:

When pressing the button, the LEDs and the buzzer will turn on. The buzzer will make a different sound for each LED and they will turn on intermittently. All the components will be on with an interval determined by the rotation of the potentiometer.

 

1 0

0