How to Interface Buzzer with PIC16F877A Primer

Buzzer Interfacing With PIC16F Primer

Tags: Working with Buzzer with PIC16F877A, Buzzer with PIC16F877A, Buzzer with PIC, Interfacing Buzzer with PIC, Sample code for Buzzer in PIC, Interfacing Buzzer,
Overall rating
PIC16F/18F Primer Board

The PIC16F/18F Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port.

Microchip’s PIC (PIC16F877A), PIC16F/18F Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.


Buzzer is an electrical device, which is similar to a bell that makes a buzzing noise and is used for signaling. Typical uses of buzzers and beepers include alarm devices, timers and confirmation of user input such as a mouse click or keystroke.

Interfacing Buzzer

Fig. 1 shows how to interface the Buzzer to microcontroller. A piezoelectric element may be driven by an oscillating electronic circuit or other audio signal source, driven with a piezoelectric audio amplifier. Sounds commonly used to indicate that a button has been pressed are a click, a ring or a beep. When the input port pin from microcontroller is changed, the sound wave is changed in Buzzer.


Fig. 1 Interfacing Buzzer to Microcontroller

Interfacing Buzzer with PIC16F877A

A small piezoelectric buzzer on the PIC16F/18F Primer Kit, by pulling pin PORTB.0 low, current will flow through the buzzer and a relatively sharp, single-tone frequency will be heard.

The alternative INT feature of pin PORTB.0 (the INT signal) can be used to modulate the buzzer to oscillate around different frequencies. Then the volume of the sound will be changed by alternating the pulse width. The buzzer can be disconnected by removing jumper JP5, and this is also the default position for this jumper since the buzzer sound can be quite annoying if always left on.

Pin Assignment with PIC16F877A


Buzzer Selection




* Connect PORTB with JP5 (E-MECH) via FRC Cable

* Place a Jumper Between INT and +5V of J6 (J6 is very near to BUZZER).

* Turn ON TXD and RXD Pins of CONFIG switch SW1. Connect Serial  

   cable between USART Section in the Board and PC and open HyperTerminal

* Press RESET Once.

Output: Choose Any one from 1 to 6. The Corresponding Action Will Be carried out.

Circuit Diagram to Interface Buzzer with PIC16F877A

Source Code

The Interfacing Buzzer with PIC16F877A program is very simple and straight forward. We now want to generate a sound in PIC16F/18F Primer Board by using a buzzer. The C programs are developed in Mplab software & it execute with Hi-Tech C Compiler.

C Program to generate sound in PIC16F using Buzzer



#include<pic.h>       //Define PIC Registers

#include<stdio.h>     //Define I/O Function


#define Buz   RB0     //RB0 interfaced to Buzzer


__CONFIG(0x3f72); //Select HS oscillator, Enable (PWRTE,BOREN),

                 //Disable (CPD,CP,WDTEN,In-circuit Debugger)


#define FOSC   10000   //10Mhz==>10000Khz

#define BAUD_RATE 9.6 //9600 Baudrate

#define BAUD_VAL   ((char)(FOSC/ (16 * BAUD_RATE )) – 1)    

//Calculation For 9600 Baudrate @10Mhz


void Serial_init(void);//Serial Communication Initialization

void DelayMs(unsigned int);


void main()


   unsigned char ReceivChar;

   TRISB = 0x00;  

   PORTB = 0x04;     //PORTB configured as O/P




   printf("Press 1: Buzzer ON \n\r\t2: Buzzer OFF");




     while(RCIF==0); //Wait until the reception is over

     RCIF=0;         //Clear the flag for next reception

     ReceivChar=RCREG; //Store the received char to a variable

     printf(" %c",ReceivChar);//Dislpay the character received






         case '1':   //If '1' is received Buzzer turned ON




         case '2':   //If '2' is received Buzzer turned OFF








void Serial_init()


   TRISC=0xc0;       //RC7,RC6 set to usart mode(INPUT)  

   TXSTA=0x24;       //Enable (Serial Transmission,

                     //Asynchronous mode, High Speed mode)

   SPBRG=BAUD_VAL;   //9600 baud at 10Mhz

   RCSTA=0x90;       //Usart Enable, Continuous receive enable

   TXIF=1;           //Start Transmit



void putch(unsigned char data)



   TXREG=data;       //Transmit the data serially



void DelayMs(unsigned int Ms)


   int delay_cnst;




     for(delay_cnst = 0;delay_cnst <220;delay_cnst++);





To compile the above C code you must need the Mplab software & Hi-Tech C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without PIC16F/18F Primer Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.

Testing the Buzzer with PIC16F877A

Give +12V power supply to PIC16F/18F Primer Board; the Buzzer is connected with PIC16F/18F Primer Board. When the program is downloading into PIC16F877A in Primer Board, the Buzzer output is working that the Buzzer is creating some sound.

If you are not getting any sound from Buzzer, then you just check the jumper connections & check the Buzzer is working or not. Otherwise you just check it with debugging mode in Mplab. If you want to see more details about debugging just see the videos in below link.

General Information

Join the World's Largest Technical Community

we respect your privacy.