Raspberry Pi cases, Raspberry Pi, Arduino, micro:bit, robots

Lesson 2 - Buzzer

Lesson 2 - Buzzer

Introduction 

In this chapter, we will learn how to use BreadPi buzzer and how to write a program for it in Scratch, Python and C for Raspberry Pi. 

Components Required 

  1. Raspberry Pi
  2. BreadPi

What is Buzzer?

 

 

A buzzer is a small yet efficient component to add sound features to our project/system. It is very small and compact 2-pin structure hence can be easily used on breadboard, Perf Board and even on PCBs which makes this a widely used component in most electronic applications.

This buzzer can be used by simply powering it using a DC power supply ranging from 4V to 9V. A simple 9V battery can also be used, but it is recommended to use a regulated +5V or +6V DC supply. The buzzer is normally associated with a switching circuit to turn ON or turn OFF the buzzer at the required time and require interval.

The SB Components BreadPi have an onboard buzzer on it to have a better study on it. How to stack a BreadPi on Raspberry Pi is already covered in the previous chapter in this we will be seeing how to control with Raspberry Pi. 

Make the complete connection first and then begin with the programming part.

Before starting lets see the pin numbers for buzzer.


ONBOARD

BCM

WIRING PI

BUZZER

37

26

25

  


Scratch 


In Scratch program, we will make the buzzer beep for 10 times. In scratch you can find many blocks in the control section and for buzzer we have picked the repeat block. Again the program will be executed once you click the green flag and stop on taping on the red button. Add the set GPIO block and set the pin number to 26 ( BCM ) and the make the output high for the next 2 seconds by adding wait block. For make the buzzer stop set the output to LOW again for 2 seconds. This program will be on repeat for the next 10 times. At last save the file and run it.


PYTHON

In Python, import BreadPi and sleep. Make a function for Buzzer named buzz, make the buzzer on for 1 second and off for the next second. Call this function. 

C PROGRAMMING

In C programming, include the header file with hash-include. Wiringpi is a library which is a package of Raspberry Pi GPIO registers and for the input/ output we use the stdio library. 

The WiringPi pin number for Buzzer is 25. 

Each function takes zero or more arguments and returns a single value. A function definition consists of a specification of what the function returns (in this case, an int ), a function name (in this case, main ), and a list of arguments enclosed in round brackets (again, a void ).

Next will check any failure in the wiringpi setup. If any failure occurs the program will stop there and print “ setup wiringpi failed!”. 

After that set the pin mode of buzzer to output. In the while loop make the buzzer beep for 2 second and stop for the next 2 seconds. This will repeat till you stop the program manually.

Next article Lesson 1- LED Blinking


Net Orders Checkout

Item Price Qty Total
Subtotal £0.00 GBP
Shipping
Total

Shipping Address

Shipping Methods

x